From 56c8b0a5c4cd95e469ef55f1ff8a88495fcc54b7 Mon Sep 17 00:00:00 2001 From: Tykayn Date: Tue, 27 Apr 2021 10:21:21 +0200 Subject: [PATCH] add timestamp trait --- src/Entity/Poll.php | 12 +- src/Entity/StackOfVotes.php | 31 ++-- src/Entity/Vote.php | 226 +++++++++++++------------ src/Entity/timedTrait.php | 12 -- src/Traits/TimeStampableTraitTrait.php | 28 +++ 5 files changed, 172 insertions(+), 137 deletions(-) delete mode 100755 src/Entity/timedTrait.php create mode 100755 src/Traits/TimeStampableTraitTrait.php diff --git a/src/Entity/Poll.php b/src/Entity/Poll.php index a3b913c..b57b4ba 100755 --- a/src/Entity/Poll.php +++ b/src/Entity/Poll.php @@ -233,8 +233,13 @@ class Poll { } } } + $answersWithStats = []; + foreach ( $computedArray as $choice_stat ) { + $answersWithStats[] = $choice_stat; + } return [ 'answers' => $computedArray, + 'answersWithStats' => $answersWithStats, 'max_score' => $maxScore, ]; } @@ -254,6 +259,10 @@ class Poll { foreach ( $this->getStacksOfVotes() as $stack ) { $displayedStackOfVotes[] = $stack->display(); } + $displayedChoices = []; + foreach ( $this->getChoices() as $choice ) { + $displayedChoices[] = $choice->display(); + } $displayedComments = []; foreach ( $this->getComments() as $comment ) { $displayedComments[] = $comment->display(); @@ -278,7 +287,8 @@ class Poll { , 'password_protected' => $this->getPassword() ? 'yes' : 'no', 'max_score' => $computedAnswers['max_score'], - 'choices' => $computedAnswers['answers'], + 'choices' => $displayedChoices, + 'choices_stats' => $computedAnswers['answersWithStats'], 'stacks' => $displayedStackOfVotes, 'comments' => $displayedComments, ]; diff --git a/src/Entity/StackOfVotes.php b/src/Entity/StackOfVotes.php index 1bba295..39b321f 100755 --- a/src/Entity/StackOfVotes.php +++ b/src/Entity/StackOfVotes.php @@ -2,6 +2,7 @@ namespace App\Entity; +use App\Traits\TimeStampableTrait; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; @@ -14,6 +15,9 @@ use JMS\Serializer\Annotation as Serializer; * @Serializer\ExclusionPolicy("all") */ class StackOfVotes { + + use TimeStampableTrait; + /** * @ORM\Id() * @ORM\GeneratedValue() @@ -44,26 +48,25 @@ class StackOfVotes { public function display() { + $votes = $this->getVotes(); + $tab = [ - 'id' => $this->getId(), +// 'id' => $this->getId(), // 'modifier_token' => $this->getOwner()->getModifierToken(), - 'pseudo' => '', - 'creation_date' => '', + 'pseudo' => $this->getPseudo(), + 'created_at' => $this->getCreatedAtAsString(), 'votes' => [], ]; // prefill votes with all choices ids - foreach ( $this->getPoll()->getChoices() as $choice ) { - $tab[ 'votes' ][ $choice->getId() ] = [ - 'choice_id' => $choice->getId(), - 'name' => $choice->getName(), - ]; - } +// foreach ( $this->getPoll()->getChoices() as $choice ) { +// $tab[ 'votes' ][ $choice->getId() ] = [ +// 'choice_id' => $choice->getId(), +// 'value' => null, +// ]; +// } - foreach ( $this->getVotes() as $vote ) { -// $tab[ 'votes' ][ $vote->getChoice()->getId() ] = $vote->display(); - $tab[ 'votes' ][ $vote->getChoice()->getId() ][ 'stack_id' ] = $this->getId(); - $tab[ 'pseudo' ] = $this->getPseudo(); - $tab[ 'creation_date' ] = $vote->getCreationDate(); + foreach ( $votes as $vote ) { + $tab[ 'votes' ][$vote->getChoice()->getId()] = $vote->display(); } return $tab; diff --git a/src/Entity/Vote.php b/src/Entity/Vote.php index 6134d18..c963563 100755 --- a/src/Entity/Vote.php +++ b/src/Entity/Vote.php @@ -1,124 +1,130 @@ getValue(); + if ( ! $value ) { + return null; + } else { - public function display() { return [ - 'id' => $this->getId(), - 'value' => $this->getValue(), - 'choice_id' => $this->getChoice()->getId(), - 'text' => $this->getChoice()->getName(), + 'id' => $this->getId(), + 'value' => $this->getValue(), + 'choice_id' => $this->getChoice()->getId(), + 'text' => $this->getChoice()->getName(), ]; } - - public function __construct() { - $this->setCreationDate( new \DateTime() ); - } - - public function getId(): ?int { - return $this->id; - } - - public function getPoll(): ?Poll { - return $this->poll; - } - - public function setPoll( ?Poll $poll ): self { - $this->poll = $poll; - if ( $poll ) { - $poll->addVote( $this ); - } - - return $this; - } - - public function getChoice(): ?Choice { - return $this->choice; - } - - public function setChoice( ?Choice $choice ): self { - $this->choice = $choice; - - return $this; - } - - public function getValue(): ?string { - return $this->value; - } - - public function setValue( ?string $value ): self { - $this->value = $value; - - return $this; - } - - public function getCreationDate(): ?DateTimeInterface { - return $this->creationDate; - } - - public function setCreationDate( DateTimeInterface $creationDate ): self { - $this->creationDate = $creationDate; - - return $this; - } - - public function getStacksOfVotes(): ?StackOfVotes { - return $this->stacksOfVotes; - } - - public function setStacksOfVotes( ?StackOfVotes $stacksOfVotes ): self { - $this->stacksOfVotes = $stacksOfVotes; - - return $this; - } } + + public function __construct() { + $this->setCreationDate( new \DateTime() ); + } + + public function getId(): ?int { + return $this->id; + } + + public function getPoll(): ?Poll { + return $this->poll; + } + + public function setPoll( ?Poll $poll ): self { + $this->poll = $poll; + if ( $poll ) { + $poll->addVote( $this ); + } + + return $this; + } + + public function getChoice(): ?Choice { + return $this->choice; + } + + public function setChoice( ?Choice $choice ): self { + $this->choice = $choice; + + return $this; + } + + public function getValue(): ?string { + return $this->value; + } + + public function setValue( ?string $value ): self { + $this->value = $value; + + return $this; + } + + public function getCreationDate(): ?DateTimeInterface { + return $this->creationDate; + } + + public function setCreationDate( DateTimeInterface $creationDate ): self { + $this->creationDate = $creationDate; + + return $this; + } + + public function getStacksOfVotes(): ?StackOfVotes { + return $this->stacksOfVotes; + } + + public function setStacksOfVotes( ?StackOfVotes $stacksOfVotes ): self { + $this->stacksOfVotes = $stacksOfVotes; + + return $this; + } +} diff --git a/src/Entity/timedTrait.php b/src/Entity/timedTrait.php deleted file mode 100755 index 2621137..0000000 --- a/src/Entity/timedTrait.php +++ /dev/null @@ -1,12 +0,0 @@ -createdAt; + } + public function getCreatedAtAsString(): string { + return $this->createdAt->format('c'); + } + + public function setCreatedAt( DateTimeInterface $createdAt ): self { + $this->createdAt = $createdAt; + + return $this; + } + +}