1
0
mirror of https://framagit.org/tykayn/date-poll-api synced 2023-08-25 08:23:11 +02:00
date-poll-api/src/Entity/Choice.php

146 lines
3.0 KiB
PHP
Raw Normal View History

<?php
2021-04-27 10:22:16 +02:00
namespace App\Entity;
use App\Traits\TimeStampableTrait;
2021-04-27 10:22:16 +02:00
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
/**
* one poll choice, could be a text or a date
* @ORM\Entity(repositoryClass="App\Repository\ChoiceRepository")
* @Serializer\ExclusionPolicy("all")
*/
class Choice {
use TimeStampableTrait;
2021-04-27 10:22:16 +02:00
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @Serializer\Expose()
*/
public $id;
2021-04-27 10:22:16 +02:00
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Serializer\Type("string")
* @Serializer\Expose()
*/
public $name;
/**
* @ORM\Column(type="string", length=1024, nullable=true)
* @Serializer\Type("string")
* @Serializer\Expose()
*/
public $url;
2021-04-27 10:22:16 +02:00
/**
* @ORM\OneToMany(targetEntity="App\Entity\Vote", mappedBy="choice", cascade={"persist"})
* @Serializer\Type("App\Entity\Vote")
*/
public $votes;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Poll", inversedBy="choices", cascade={"persist"})
* @Serializer\Type("App\Entity\Poll")
*/
private $poll;
public function __construct( $optionalName = null ) {
2021-04-27 12:51:21 +02:00
$this->setCreatedAt( new DateTime() );
2021-04-27 10:22:16 +02:00
$this->poll = new ArrayCollection();
$this->votes = new ArrayCollection();
if ( $optionalName ) {
$this->setName( $optionalName );
2020-01-22 10:22:36 +01:00
}
2021-04-27 10:22:16 +02:00
}
2021-04-30 12:28:05 +02:00
public function display( $kind = 'text' ): array {
if($kind == 'text'){
$fields = [
'id' => $this->getId(),
'created_at' => $this->getCreatedAtAsString(),
'name' => $this->getName(),
'url' => $this->getUrl(),
];
}
elseif($kind=='date'){
$fields = [
'id' => $this->getId(),
'created_at' => $this->getCreatedAtAsString(),
'name' => $this->getName(),
// 'url' => $this->getUrl(),
];
}
return $fields;
2021-04-27 10:22:16 +02:00
}
2019-11-28 14:16:56 +01:00
2021-04-27 10:22:16 +02:00
public function getId(): ?int {
return $this->id;
}
2019-11-28 14:16:56 +01:00
2021-04-27 10:22:16 +02:00
public function getName(): ?string {
return $this->name;
}
2019-11-28 14:16:56 +01:00
2021-04-27 10:22:16 +02:00
public function setName( ?string $name ): self {
$this->name = $name;
2019-11-28 14:16:56 +01:00
2021-04-27 10:22:16 +02:00
return $this;
}
2019-11-28 14:16:56 +01:00
2021-04-27 10:22:16 +02:00
public function getUrl(): ?string {
return $this->url;
}
2019-11-28 11:51:25 +01:00
2021-04-27 10:22:16 +02:00
public function setUrl( ?string $url ): self {
$this->url = $url;
2019-11-28 14:16:56 +01:00
2021-04-27 10:22:16 +02:00
return $this;
}
2019-11-28 11:51:25 +01:00
2021-04-30 12:28:05 +02:00
public function getPoll(): ?ArrayCollection {
2021-04-27 10:22:16 +02:00
return $this->poll;
}
2021-04-27 10:22:16 +02:00
public function setPoll( ?Poll $poll ): self {
$this->poll = $poll;
2019-11-28 14:16:56 +01:00
2021-04-27 10:22:16 +02:00
return $this;
}
2020-01-22 10:22:36 +01:00
2021-04-27 10:22:16 +02:00
/**
* @return Collection|Vote[]
*/
public function getVotes(): Collection {
return $this->votes;
}
2020-01-22 10:22:36 +01:00
2021-04-27 10:22:16 +02:00
public function addVote( Vote $vote ): self {
if ( ! $this->votes->contains( $vote ) ) {
$this->votes[] = $vote;
$vote->setChoice( $this );
}
2020-01-22 10:22:36 +01:00
2021-04-27 10:22:16 +02:00
return $this;
}
2021-04-27 10:22:16 +02:00
public function removeVote( Vote $vote ): self {
if ( $this->votes->contains( $vote ) ) {
$this->votes->removeElement( $vote );
// set the owning side to null (unless already changed)
if ( $vote->getChoice() === $this ) {
$vote->setChoice( null );
}
}
2021-04-27 10:22:16 +02:00
return $this;
2020-01-22 10:22:36 +01:00
}
2021-04-27 10:22:16 +02:00
}