date-poll-api/src/Entity/Choice.php

178 lines
3.8 KiB
PHP
Raw Normal View History

<?php
namespace App\Entity;
2019-11-12 11:26:19 +01:00
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
2019-11-06 14:54:04 +01:00
use JMS\Serializer\Annotation as Serializer;
/**
2019-11-12 11:26:19 +01:00
* one poll choice, could be a text or a date
* @ORM\Entity(repositoryClass="App\Repository\ChoiceRepository")
*/
class Choice {
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255, nullable=true)
2019-11-06 14:54:04 +01:00
* @Serializer\Type("string")
*/
private $name;
/**
* @ORM\Column(type="datetime", nullable=true)
2019-11-06 14:54:04 +01:00
* @Serializer\Type("datetime")
*/
private $dateTime;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Poll", mappedBy="choices")
2019-11-06 14:54:04 +01:00
* @Serializer\Type("App\Entity\Poll")
*/
private $poll;
/**
2019-11-12 11:26:19 +01:00
* @ORM\OneToMany(targetEntity="App\Entity\StackOfVotes", mappedBy="choice")
* @Serializer\Type("App\Entity\StackOfVotes")
*/
2019-11-12 11:26:19 +01:00
private $stackOfVotes;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Vote", mappedBy="choice")
* @Serializer\Type("App\Entity\Vote")
*/
private $votes;
2019-11-06 14:54:04 +01:00
public function __construct() {
2019-11-27 10:57:06 +01:00
$this->poll = new ArrayCollection();
$this->stackOfVotes = new ArrayCollection();
$this->votes = new ArrayCollection();
2019-11-27 10:57:06 +01:00
$this->setDateTime( new \DateTime() );
}
public function getId(): ?int {
2019-11-27 10:57:06 +01:00
return $this->id;
}
public function getName(): ?string {
2019-11-27 10:57:06 +01:00
return $this->name;
}
2019-11-27 16:14:49 +01:00
public function setName( $name ): self {
if ( is_a( $name, 'DateTime' ) ) {
$this->setDateTime( $name );
$name = $name->format( 'D Y-m-d' );
}
2019-11-27 10:57:06 +01:00
$this->name = $name;
return $this;
}
2019-11-12 11:26:19 +01:00
public function getDateTime(): ?DateTimeInterface {
2019-11-27 10:57:06 +01:00
return $this->dateTime;
}
2019-11-12 11:26:19 +01:00
public function setDateTime( ?DateTimeInterface $dateTime ): self {
2019-11-27 10:57:06 +01:00
$this->dateTime = $dateTime;
return $this;
}
2019-11-06 14:54:04 +01:00
/**
* @return Collection|Poll[]
*/
public function getPoll(): Collection {
2019-11-27 10:57:06 +01:00
return $this->poll;
}
2019-11-06 14:54:04 +01:00
public function setPoll( Poll $poll ): self {
$this->poll = [ $poll ];
return $this;
}
2019-11-06 14:54:04 +01:00
public function addPoll( Poll $poll ): self {
2019-11-27 10:57:06 +01:00
if ( ! $this->poll->contains( $poll ) ) {
$this->poll[] = $poll;
$poll->setChoices( $this );
}
return $this;
}
2019-11-06 14:54:04 +01:00
public function removePoll( Poll $poll ): self {
2019-11-27 10:57:06 +01:00
if ( $this->poll->contains( $poll ) ) {
$this->poll->removeElement( $poll );
// set the owning side to null (unless already changed)
if ( $poll->getChoices() === $this ) {
$poll->setChoices( null );
}
}
return $this;
}
2019-11-06 14:54:04 +01:00
/**
* @return Collection|Choice[]
*/
2019-11-12 11:26:19 +01:00
public function getStackOfVotes(): Collection {
2019-11-27 10:57:06 +01:00
return $this->stackOfVotes;
}
2019-11-06 14:54:04 +01:00
public function addVote( Choice $vote ): self {
2019-11-27 10:57:06 +01:00
if ( ! $this->stackOfVotes->contains( $vote ) ) {
$this->stackOfVotes[] = $vote;
$vote->setChoice( $this );
}
return $this;
}
2019-11-06 14:54:04 +01:00
public function removeVote( Choice $vote ): self {
2019-11-27 10:57:06 +01:00
if ( $this->stackOfVotes->contains( $vote ) ) {
$this->stackOfVotes->removeElement( $vote );
// set the owning side to null (unless already changed)
if ( $vote->getChoice() === $this ) {
$vote->setChoice( null );
}
}
return $this;
}
public function addStackOfVote( StackOfVotes $stackOfVote ): self {
if ( ! $this->stackOfVotes->contains( $stackOfVote ) ) {
$this->stackOfVotes[] = $stackOfVote;
$stackOfVote->setChoice( $this );
}
return $this;
}
public function removeStackOfVote( StackOfVotes $stackOfVote ): self {
if ( $this->stackOfVotes->contains( $stackOfVote ) ) {
$this->stackOfVotes->removeElement( $stackOfVote );
// set the owning side to null (unless already changed)
if ( $stackOfVote->getChoice() === $this ) {
$stackOfVote->setChoice( null );
}
}
return $this;
}
/**
* @return Collection|Vote[]
*/
public function getVotes(): Collection {
return $this->votes;
2019-11-27 10:57:06 +01:00
}
}