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,16 +1,16 @@
|
||||
<?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 {
|
||||
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
|
||||
@ -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() );
|
||||
@ -121,4 +127,4 @@
|
||||
|
||||
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…
Reference in New Issue
Block a user