src/Entity/Poll.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use DateTime;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Doctrine\Common\Annotations\Annotation\IgnoreAnnotation;
  8. use Symfony\Component\HttpFoundation\File\File;
  9. use Symfony\Component\HttpFoundation\File\UploadedFile;
  10. use Symfony\Component\Validator\Mapping\ClassMetadata;
  11. use Exception;
  12. use Symfony\Component\Serializer\Annotation\Ignore;
  13. use Symfony\Component\Validator\Constraints as Assert;
  14. /**
  15.  * Poll
  16.  *
  17.  * @ORM\Table(name="poll")
  18.  * @ORM\Entity(repositoryClass="App\Repository\PollRepository")
  19.  */
  20. class Poll
  21. {
  22.     /**
  23.      * @ORM\Column(name="id", type="integer")
  24.      * @ORM\Id
  25.      * @ORM\GeneratedValue(strategy="AUTO")
  26.      */
  27.     private int $id;
  28.     /**
  29.      * @ORM\Column(name="title", type="string", length=255)
  30.      *
  31.      * @Assert\NotBlank()
  32.      */
  33.     private string $title;
  34.     /**
  35.      * @ORM\Column(name="description", type="text")
  36.      *
  37.      * @Assert\NotBlank()
  38.      */
  39.     private string $description;
  40.     /**
  41.      * @ORM\Column(name="creator", type="string", length=255)
  42.      */
  43.     private string $creator;
  44.     /**
  45.      * @ORM\Column(name="endsAt", type="datetime")
  46.      *
  47.      * @Assert\NotBlank()
  48.      */
  49.     private DateTime|null $endsAt;
  50.     /**
  51.      * @ORM\Column(name="startsAt", type="datetime")
  52.      *
  53.      * @Assert\NotBlank()
  54.      */
  55.     private DateTime|null $startsAt;
  56.     /**
  57.      *
  58.      * @ORM\ManyToOne(targetEntity="App\Entity\Building", inversedBy="polls")
  59.      * @ORM\JoinColumns({
  60.      *   @ORM\JoinColumn(name="building_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
  61.      * })
  62.      */
  63.     #[Ignore]
  64.     private Building $building;
  65.     /**
  66.      *
  67.      * @var Collection<int, PollOption>
  68.      * @ORM\OneToMany(targetEntity="App\Entity\PollOption", mappedBy="poll", cascade={"persist", "remove"}, orphanRemoval=true)
  69.      */
  70.     private Collection $pollOptions;
  71.     /**
  72.      * @ORM\Column(name="createdAt", type="datetime")
  73.      */
  74.     private DateTime|null $createdAt null;
  75.     /**
  76.      * @ORM\Column(name="updateAt", type="datetime")
  77.      */
  78.     private DateTime|null $updateAt;
  79.     /**
  80.      * @ORM\Column(type="string", nullable=true)
  81.      *
  82.      * @Assert\File(mimeTypes={ "application/pdf" })
  83.      */
  84.     private UploadedFile|File|string|null $file null;
  85.     /**
  86.      * @ORM\Column(type="string", length=255, nullable=true)
  87.      */
  88.     private ?string $fileName null;
  89.     /**
  90.      * Poll constructor.
  91.      */
  92.     public function __construct()
  93.     {
  94.         $this->pollOptions = new ArrayCollection();
  95.         $this->incrementCreatedAt();
  96.     }
  97.     /**
  98.      * Get id
  99.      *
  100.      * @return int
  101.      */
  102.     public function getId(): int
  103.     {
  104.         return $this->id;
  105.     }
  106.     /**
  107.      * @return Building
  108.      */
  109.     public function getBuilding(): Building
  110.     {
  111.         return $this->building;
  112.     }
  113.     /**
  114.      * @param Building $building
  115.      */
  116.     public function setBuilding(Building $building): void
  117.     {
  118.         $this->building $building;
  119.     }
  120.     /**
  121.      * @return string
  122.      */
  123.     public function getCreator(): string
  124.     {
  125.         return $this->creator;
  126.     }
  127.     /**
  128.      * @param string $creator
  129.      */
  130.     public function setCreator(string $creator): void
  131.     {
  132.         $this->creator $creator;
  133.     }
  134.     /**
  135.      * @return DateTime|null
  136.      */
  137.     public function getEndsAt(): ?DateTime
  138.     {
  139.         return $this->endsAt;
  140.     }
  141.     /**
  142.      * @param DateTime|null $endsAt
  143.      */
  144.     public function setEndsAt(?DateTime $endsAt): void
  145.     {
  146.         $this->endsAt $endsAt;
  147.     }
  148.     /**
  149.      *
  150.      * @return bool
  151.      * @throws Exception
  152.      */
  153.     public function hasEnded(): bool
  154.     {
  155.         return self::checkTime($this->endsAt);
  156.     }
  157.     /**
  158.      * @param DateTime $time
  159.      *
  160.      * @return bool
  161.      * @throws Exception
  162.      */
  163.     public static function checkTime(DateTime $time): bool
  164.     {
  165.         return $time < new DateTime();
  166.     }
  167.     /**
  168.      * @return DateTime|null
  169.      */
  170.     public function getCreatedAt(): ?DateTime
  171.     {
  172.         return $this->createdAt;
  173.     }
  174.     /**
  175.      * @param DateTime $createdAt
  176.      */
  177.     public function setCreatedAt(DateTime $createdAt): void
  178.     {
  179.         $this->createdAt $createdAt;
  180.     }
  181.     /**
  182.      * @return DateTime|null
  183.      */
  184.     public function getUpdateAt(): ?DateTime
  185.     {
  186.         return $this->updateAt;
  187.     }
  188.     /**
  189.      * @param DateTime|null $updateAt
  190.      */
  191.     public function setUpdateAt(DateTime|null $updateAt): void
  192.     {
  193.         $this->updateAt $updateAt;
  194.     }
  195.     /**
  196.      * @return string
  197.      */
  198.     public function getTitle(): string
  199.     {
  200.         return $this->title;
  201.     }
  202.     /**
  203.      * @param string $title
  204.      */
  205.     public function setTitle(string $title): void
  206.     {
  207.         $this->title $title;
  208.     }
  209.     /**
  210.      * @return string
  211.      */
  212.     public function getDescription(): string
  213.     {
  214.         return $this->description;
  215.     }
  216.     /**
  217.      * @param string $description
  218.      */
  219.     public function setDescription(string $description): void
  220.     {
  221.         $this->description $description;
  222.     }
  223.     /**
  224.      * @return Collection<int, PollOption>
  225.      */
  226.     public function getPollOptions(): Collection
  227.     {
  228.         return $this->pollOptions;
  229.     }
  230.     /**
  231.      * @param Collection<int, PollOption> $pollOptions
  232.      */
  233.     public function setPollOptions(Collection $pollOptions): void
  234.     {
  235.         $this->pollOptions $pollOptions;
  236.     }
  237.     public function addPollOption(PollOption $pollOption): self
  238.     {
  239.         if (!$this->pollOptions->contains($pollOption)) {
  240.             $this->pollOptions[] = $pollOption;
  241.             $pollOption->setPoll($this);
  242.         }
  243.         return $this;
  244.     }
  245.     public function removePollOption(PollOption $pollOption): self
  246.     {
  247.         if ($this->pollOptions->contains($pollOption)) {
  248.             $this->pollOptions->removeElement($pollOption);
  249.         }
  250.         return $this;
  251.     }
  252.     /**
  253.      * @ORM\PrePersist
  254.      */
  255.     public function incrementCreatedAt(): void
  256.     {
  257.         if (null === $this->createdAt) {
  258.             $this->createdAt = new DateTime();
  259.         }
  260.         $this->updateAt = new DateTime();
  261.     }
  262.     public function __toString()
  263.     {
  264.         return (string) $this->getTitle();
  265.     }
  266.     public function getStartsAt(): ?DateTime
  267.     {
  268.         return $this->startsAt;
  269.     }
  270.     public function setStartsAt(?DateTime $startsAt): void
  271.     {
  272.         $this->startsAt $startsAt;
  273.     }
  274.     public function getFile(): File|string|UploadedFile|null
  275.     {
  276.         return $this->file;
  277.     }
  278.     public function setFile(File|string|UploadedFile|null $file): void
  279.     {
  280.         $this->file $file;
  281.     }
  282.     public function getFileName(): ?string
  283.     {
  284.         return $this->fileName;
  285.     }
  286.     public function setFileName(?string $fileName): self
  287.     {
  288.         $this->fileName $fileName;
  289.         return $this;
  290.     }
  291. }