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

165 lines
3.3 KiB
PHP
Raw Normal View History

2019-11-11 10:44:19 +01:00
<?php
2020-01-30 12:25:58 +01:00
namespace App\Entity;
2021-04-27 10:21:21 +02:00
use App\Traits\TimeStampableTrait;
2021-04-27 12:51:10 +02:00
use DateTime;
2020-01-30 12:25:58 +01:00
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
/**
* contains the votes for one answer to a poll
* @ORM\Entity(repositoryClass="App\Repository\StackOfVotesRepository")
* @ORM\HasLifecycleCallbacks()
* @Serializer\ExclusionPolicy("all")
2020-01-30 12:25:58 +01:00
*/
class StackOfVotes {
2021-04-27 10:21:21 +02:00
use TimeStampableTrait;
2020-01-30 12:25:58 +01:00
/**
* @ORM\Column(type="string", length=255)
* @Serializer\Type("string")
* @Serializer\Expose()
*/
public $pseudo;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Vote", mappedBy="stacksOfVotes", orphanRemoval=true, cascade={"persist","remove"})
2020-01-30 12:25:58 +01:00
* @Serializer\Expose()
*/
public $votes;
2021-04-27 10:22:16 +02:00
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
2020-01-30 12:25:58 +01:00
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Poll", inversedBy="stacksOfVotes", cascade={"persist"})
*/
private $poll;
2019-11-12 11:26:19 +01:00
/**
2020-01-30 12:25:58 +01:00
* @ORM\ManyToOne(targetEntity="App\Entity\Owner", inversedBy="stackOfVotes", cascade={"persist"})
* @Serializer\Expose()
2019-11-12 11:26:19 +01:00
*/
2020-01-30 12:25:58 +01:00
private $owner;
2021-04-27 10:41:21 +02:00
/**
2021-04-27 12:52:17 +02:00
* @ORM\Column(type="string", length=255, nullable=true)
2021-04-27 10:41:21 +02:00
*/
private $ip;
2021-04-27 10:22:16 +02:00
public function __construct() {
2021-04-27 12:51:10 +02:00
$this->setCreatedAt( new DateTime() );
$this->votes = new ArrayCollection();
}
2020-01-30 12:25:58 +01:00
public function display() {
2021-04-27 12:51:10 +02:00
$tab = [
'id' => $this->getId(),
2021-04-27 12:51:10 +02:00
'pseudo' => $this->getPseudo(),
'created_at' => $this->getCreatedAtAsString(),
'votes' => [],
];
2021-04-29 11:41:36 +02:00
foreach ( $this->getVotes() as $vote ) {
2021-06-08 10:22:58 +02:00
$tab[ 'votes' ][] = $vote->display();
2021-04-27 12:51:10 +02:00
}
$tab[ 'owner' ] = $this->getOwner()->display();
return $tab;
}
public function displayForAdmin() {
$tab = $this->display();
$tab[ 'owner' ] = $this->getOwner()->displayForAdmin();
2021-04-27 12:51:10 +02:00
return $tab;
}
2019-11-27 17:07:11 +01:00
2020-01-30 12:25:58 +01:00
/**
* @return Collection|poll[]
*/
public function getVotes(): Collection {
2021-04-27 12:51:10 +02:00
return $this->votes;
}
2019-11-12 11:26:19 +01:00
2020-01-30 12:25:58 +01:00
public function getPseudo(): ?string {
2021-04-27 12:51:10 +02:00
return $this->pseudo;
}
2019-11-12 11:26:19 +01:00
2020-01-30 12:25:58 +01:00
public function setPseudo( ?string $pseudo ): self {
2021-04-27 12:51:10 +02:00
$this->pseudo = $pseudo;
return $this;
}
2021-04-27 10:22:16 +02:00
/**
* @ORM\PrePersist
*/
public function prePersist() {
2021-04-27 12:51:10 +02:00
$this->setPseudo( $this->getOwner()->getPseudo() );
}
2021-04-27 10:22:16 +02:00
2020-01-30 12:25:58 +01:00
public function getOwner(): ?Owner {
2021-04-27 12:51:10 +02:00
return $this->owner;
}
2020-01-29 17:26:16 +01:00
2020-01-30 12:25:58 +01:00
public function setOwner( ?Owner $owner ): self {
2021-04-27 12:51:10 +02:00
$this->owner = $owner;
return $this;
}
2021-04-27 10:22:16 +02:00
public function getId(): ?int {
2021-04-27 12:51:10 +02:00
return $this->id;
}
2021-04-27 10:22:16 +02:00
public function addVote( Vote $vote ): self {
2021-04-27 12:51:10 +02:00
if ( ! $this->votes->contains( $vote ) ) {
$vote->setPoll( $this->getPoll() );
$this->votes[] = $vote;
$vote->setStacksOfVotes( $this );
}
return $this;
}
2021-04-27 10:22:16 +02:00
2020-01-30 12:25:58 +01:00
public function getPoll(): ?Poll {
2021-04-27 12:51:10 +02:00
return $this->poll;
}
2020-01-30 12:25:58 +01:00
public function setPoll( ?Poll $poll ): self {
2021-04-27 12:51:10 +02:00
$this->poll = $poll;
return $this;
}
2021-04-27 10:22:16 +02:00
public function removeVote( Vote $vote ): self {
2021-04-27 12:51:10 +02:00
if ( $this->votes->contains( $vote ) ) {
$this->votes->removeElement( $vote );
// set the owning side to null (unless already changed)
if ( $vote->getStacksOfVotes() === $this ) {
$vote->setStacksOfVotes( null );
}
}
return $this;
}
public function getIp(): ?string {
return $this->ip;
}
public function setIp( string $ip ): self {
$this->ip = $ip;
return $this;
}
2021-06-08 10:22:58 +02:00
2020-01-30 12:25:58 +01:00
}