1
0
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:
Tykayn 2021-04-27 10:21:21 +02:00 committed by tykayn
parent 30c99568dd
commit 56c8b0a5c4
5 changed files with 172 additions and 137 deletions

View File

@ -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,
];

View File

@ -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;

View File

@ -53,6 +53,11 @@
private $stacksOfVotes;
public function display() {
$value = $this->getValue();
if ( ! $value ) {
return null;
} else {
return [
'id' => $this->getId(),
'value' => $this->getValue(),
@ -60,6 +65,7 @@
'text' => $this->getChoice()->getName(),
];
}
}
public function __construct() {
$this->setCreationDate( new \DateTime() );

View File

@ -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;
}

View 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;
}
}