src/Entity/Gallery.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\DBAL\Types\Types;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\HttpFoundation\File\File;
  8. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  9. /**
  10.  * @ORM\Entity()
  11.  * @ORM\HasLifecycleCallbacks
  12.  * @Vich\Uploadable()
  13.  */
  14. class Gallery
  15. {
  16.     /**
  17.      * @var int
  18.      *
  19.      * @ORM\Id
  20.      * @ORM\GeneratedValue
  21.      * @ORM\Column
  22.      */
  23.     private ?int $id null;
  24.     /**
  25.      * @var string
  26.      *
  27.      * @ORM\Column(length=8)
  28.      */
  29.     private ?string $slug null;
  30.     /**
  31.      * @var string
  32.      *
  33.      * @ORM\Column(length=255, nullable=true)
  34.      */
  35.     private ?string $filename null;
  36.     /**
  37.      * @var string
  38.      *
  39.      * @ORM\Column(nullable=true)
  40.      */
  41.     private ?int $filesize null;
  42.     /**
  43.      * @var \DateTime
  44.      *
  45.      * @ORM\Column(type=Types::DATETIME_IMMUTABLE, nullable=true)
  46.      */
  47.     private ?\DateTimeImmutable $updated null;
  48.     /**
  49.      * Unmapped property to handle file uploads
  50.      *
  51.      * @Vich\UploadableField(mapping="gallery_photo", fileNameProperty="filename", size="filesize")
  52.      */
  53.     private ?File $file null;
  54.     /**
  55.      * @var bool
  56.      *
  57.      * @ORM\Column(type=Types::BOOLEAN)
  58.      */
  59.     private $active true;
  60.     /**
  61.      * @var \DateTime
  62.      *
  63.      * @ORM\Column(type=Types::DATETIME_MUTABLE, nullable=true)
  64.      */
  65.     private ?\DateTime $expiration null;
  66.     /**
  67.      * @var GalleryView[]|Collection
  68.      *
  69.      * @ORM\OneToMany(targetEntity=GalleryView::class, mappedBy="gallery", cascade={"remove"})
  70.      * @ORM\JoinColumn(referencedColumnName="uid")
  71.      */
  72.     private $views;
  73.     /**
  74.      * Allowed views = 0: infinite, untill active
  75.      *
  76.      * @var int
  77.      *
  78.      * @ORM\Column(type=Types::INTEGER)
  79.      */
  80.     private $allowedViews 0;
  81.     /**
  82.      * @var GalleryTag[]
  83.      *
  84.      * @ORM\OneToMany(targetEntity=GalleryGalleryTag::class, mappedBy="gallery", fetch="EXTRA_LAZY")
  85.      */
  86.     private $tags;
  87.     public function __toString()
  88.     {
  89.         return $this->getSlug();
  90.     }
  91.     public function __construct()
  92.     {
  93.         $this->views = new ArrayCollection();
  94.         $this->tags = new ArrayCollection();
  95.     }
  96.     public function getId(): ?int
  97.     {
  98.         return $this->id;
  99.     }
  100.     public function getSlug(): ?string
  101.     {
  102.         return $this->slug;
  103.     }
  104.     public function setSlug(string $slug): self
  105.     {
  106.         $this->slug $slug;
  107.         return $this;
  108.     }
  109.     public function getUpdated(): ?\DateTimeInterface
  110.     {
  111.         return $this->updated;
  112.     }
  113.     public function setUpdated(\DateTimeInterface $updated): self
  114.     {
  115.         $this->updated $updated;
  116.         return $this;
  117.     }
  118.     /**
  119.      * @return string|null
  120.      */
  121.     public function getFilename(): ?string
  122.     {
  123.         return $this->filename;
  124.     }
  125.     /**
  126.      * @param string|null $filename
  127.      * @return Photo
  128.      */
  129.     public function setFilename(?string $filename): self
  130.     {
  131.         $this->filename $filename;
  132.         return $this;
  133.     }
  134.     public function setFile(?File $file null): void
  135.     {
  136.         $this->file $file;
  137.         if (null !== $file) {
  138.             // It is required that at least one field changes if you are using doctrine
  139.             // otherwise the event listeners won't be called and the file is lost
  140.             $this->updated = new \DateTimeImmutable();
  141.         }
  142.     }
  143.     public function getFile(): ?File
  144.     {
  145.         return $this->file;
  146.     }
  147.     /**
  148.      * @return int|null
  149.      */
  150.     public function getFilesize(): ?int
  151.     {
  152.         return $this->filesize;
  153.     }
  154.     /**
  155.      * @param int|null $filesize
  156.      * @return Photo
  157.      */
  158.     public function setFilesize(?int $filesize): self
  159.     {
  160.         $this->filesize $filesize;
  161.         return $this;
  162.     }
  163.     function getRandomString(int $length 8) {
  164.         return substr(str_shuffle('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'), 0$length);
  165.         $characters '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  166.         $string '';
  167.         for ($i 0$i $length$i++) {
  168.             $string .= $characters[mt_rand(0strlen($characters) - 1)];
  169.         }
  170.         return $string;
  171.     }
  172.     public function isActive(): bool
  173.     {
  174.         return $this->active;
  175.     }
  176.     public function setActive(bool $active): Gallery
  177.     {
  178.         $this->active $active;
  179.         return $this;
  180.     }
  181.     public function getExpiration(): ?\DateTime
  182.     {
  183.         return $this->expiration;
  184.     }
  185.     public function setExpiration(?\DateTime $expiration): Gallery
  186.     {
  187.         $this->expiration $expiration;
  188.         return $this;
  189.     }
  190.     public function getViews(): Collection
  191.     {
  192.         return $this->views;
  193.     }
  194.     public function setViews(Collection $views): Gallery
  195.     {
  196.         $this->views $views;
  197.         return $this;
  198.     }
  199.     public function addView(GalleryView $view): Gallery
  200.     {
  201.         if (!$this->views->contains($view)) {
  202.             $this->views->add($view);
  203.         }
  204.         return $this;
  205.     }
  206.     public function removeView(GalleryView $view): Gallery
  207.     {
  208.         if ($this->views->contains($view)) {
  209.             $this->views->removeElement($view);
  210.         }
  211.         return $this;
  212.     }
  213.     public function getViewsCount(): int
  214.     {
  215.         return $this->views->count();
  216.     }
  217.     public function getAllowedViews(): int
  218.     {
  219.         return $this->allowedViews;
  220.     }
  221.     public function setAllowedViews(int $allowedViews): Gallery
  222.     {
  223.         $this->allowedViews $allowedViews;
  224.         return $this;
  225.     }
  226.     public function getTags(): Collection
  227.     {
  228.         return $this->tags;
  229.     }
  230.     public function setTags(Collection $tags): Gallery
  231.     {
  232.         $this->tags $tags;
  233.         return $this;
  234.     }
  235. }