mirror of
https://framagit.org/tykayn/date-poll-api
synced 2023-08-25 08:23:11 +02:00
add timestamp trait
This commit is contained in:
parent
30c99568dd
commit
56c8b0a5c4
@ -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,
|
||||
];
|
||||
|
@ -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;
|
||||
|
@ -1,124 +1,130 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
namespace App\Entity;
|
||||
|
||||
use DateTimeInterface;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use JMS\Serializer\Annotation as Serializer;
|
||||
|
||||
/**
|
||||
* @ORM\Entity(repositoryClass="App\Repository\VoteRepository")
|
||||
* @Serializer\ExclusionPolicy("all")
|
||||
*/
|
||||
class Vote {
|
||||
/**
|
||||
* @ORM\Entity(repositoryClass="App\Repository\VoteRepository")
|
||||
* @Serializer\ExclusionPolicy("all")
|
||||
* for a text kind of choice: could be "yes" "no" "maybe" and empty.
|
||||
* for a date kind, the choice linked is equivalent to the value selected
|
||||
* @ORM\Column(type="string", length=255, nullable=true)
|
||||
* @Serializer\Type("string")
|
||||
* @Serializer\Expose()
|
||||
*/
|
||||
class Vote {
|
||||
/**
|
||||
* for a text kind of choice: could be "yes" "no" "maybe" and empty.
|
||||
* for a date kind, the choice linked is equivalent to the value selected
|
||||
* @ORM\Column(type="string", length=255, nullable=true)
|
||||
* @Serializer\Type("string")
|
||||
* @Serializer\Expose()
|
||||
*/
|
||||
public $value;
|
||||
/**
|
||||
* @ORM\Column(type="datetime" , options={"default"="CURRENT_TIMESTAMP"})
|
||||
* @Serializer\Type("datetime")
|
||||
* @Serializer\Expose()
|
||||
*/
|
||||
public $creationDate;
|
||||
/**
|
||||
* @ORM\ManyToOne(targetEntity="App\Entity\Choice", inversedBy="votes", cascade={"persist"})
|
||||
* @ORM\JoinColumn(nullable=false)
|
||||
* @Serializer\Type("App\Entity\choice")
|
||||
* @Serializer\Expose()
|
||||
*/
|
||||
public $choice;
|
||||
/**
|
||||
* @ORM\Id()
|
||||
* @ORM\GeneratedValue()
|
||||
* @ORM\Column(type="integer")
|
||||
* @Serializer\Type("integer")
|
||||
* @Serializer\Expose()
|
||||
*/
|
||||
private $id;
|
||||
/**
|
||||
* @ORM\ManyToOne(targetEntity="App\Entity\Poll", inversedBy="votes", cascade={"persist"})
|
||||
* @ORM\JoinColumn(nullable=false)
|
||||
*/
|
||||
private $poll;
|
||||
/**
|
||||
* @ORM\ManyToOne(targetEntity="App\Entity\StackOfVotes", inversedBy="votes", cascade={"persist"})
|
||||
* @ORM\JoinColumn(nullable=false)
|
||||
* @Serializer\Type("App\Entity\StackOfVotes")
|
||||
*/
|
||||
private $stacksOfVotes;
|
||||
public $value;
|
||||
/**
|
||||
* @ORM\Column(type="datetime" , options={"default"="CURRENT_TIMESTAMP"})
|
||||
* @Serializer\Type("datetime")
|
||||
* @Serializer\Expose()
|
||||
*/
|
||||
public $creationDate;
|
||||
/**
|
||||
* @ORM\ManyToOne(targetEntity="App\Entity\Choice", inversedBy="votes", cascade={"persist"})
|
||||
* @ORM\JoinColumn(nullable=false)
|
||||
* @Serializer\Type("App\Entity\choice")
|
||||
* @Serializer\Expose()
|
||||
*/
|
||||
public $choice;
|
||||
/**
|
||||
* @ORM\Id()
|
||||
* @ORM\GeneratedValue()
|
||||
* @ORM\Column(type="integer")
|
||||
* @Serializer\Type("integer")
|
||||
* @Serializer\Expose()
|
||||
*/
|
||||
private $id;
|
||||
/**
|
||||
* @ORM\ManyToOne(targetEntity="App\Entity\Poll", inversedBy="votes", cascade={"persist"})
|
||||
* @ORM\JoinColumn(nullable=false)
|
||||
*/
|
||||
private $poll;
|
||||
/**
|
||||
* @ORM\ManyToOne(targetEntity="App\Entity\StackOfVotes", inversedBy="votes", cascade={"persist"})
|
||||
* @ORM\JoinColumn(nullable=false)
|
||||
* @Serializer\Type("App\Entity\StackOfVotes")
|
||||
*/
|
||||
private $stacksOfVotes;
|
||||
|
||||
public function display() {
|
||||
$value = $this->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;
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
trait Timed {
|
||||
/**
|
||||
* @ORM\Column(type="datetime" , options={"default"="CURRENT_TIMESTAMP"})
|
||||
*/
|
||||
private $createdAt;
|
||||
}
|
28
src/Traits/TimeStampableTraitTrait.php
Executable file
28
src/Traits/TimeStampableTraitTrait.php
Executable file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use DateTimeInterface;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
|
||||
trait TimeStampableTrait {
|
||||
/**
|
||||
* @ORM\Column(type="datetime" , options={"default"="CURRENT_TIMESTAMP"})
|
||||
*/
|
||||
private $createdAt;
|
||||
|
||||
public function getCreatedAt(): ?DateTimeInterface {
|
||||
return $this->createdAt;
|
||||
}
|
||||
public function getCreatedAtAsString(): string {
|
||||
return $this->createdAt->format('c');
|
||||
}
|
||||
|
||||
public function setCreatedAt( DateTimeInterface $createdAt ): self {
|
||||
$this->createdAt = $createdAt;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user