src/Entity/Media.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\MediaMetaTag;
  4. use App\Repository\MediaRepository;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Symfony\Component\Validator\Constraints as Assert;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. /**
  10.  * @ORM\Entity(repositoryClass=MediaRepository::class)
  11.  */
  12. class Media
  13. {
  14.     /**
  15.      * @var int
  16.      * @ORM\Id
  17.      * @ORM\GeneratedValue
  18.      * @ORM\Column(type="integer")
  19.      */
  20.     private $id;
  21.     /**
  22.      * @var string
  23.      * @ORM\Column(type="string", length=255)
  24.      * @Assert\Length(min=3, max=255, minMessage="Le titre est trop court !")
  25.      */
  26.     private $title;
  27.     /**
  28.      * @var string
  29.      * @ORM\Column(type="string", length=255)
  30.      */
  31.     private $src;
  32.     /**
  33.      * @ORM\ManyToMany(targetEntity=VideoMarker::class, mappedBy="attachments", fetch="EAGER")
  34.      */
  35.     private $markers;
  36.     /**
  37.      * @var string
  38.      * @ORM\Column(type="text", nullable=true)
  39.      */
  40.     private $description;
  41.     /**
  42.      * @var string
  43.      * @ORM\Column(type="string", length=255)
  44.      */
  45.     private $mime;
  46.     /**
  47.      * @var Collection
  48.      * @ORM\OneToMany(targetEntity=MediaMetaTag::class, mappedBy="media",cascade={"persist"}, orphanRemoval=true)
  49.      */
  50.     private $mediaMetaTags;
  51.     /**
  52.      * @var Article
  53.      * @ORM\OneToMany(targetEntity=Article::class, mappedBy="video", orphanRemoval=true)
  54.      */
  55.     private $articles;
  56.     /**
  57.      * @var \DateTime
  58.      * @ORM\Column(type="datetime")
  59.      */
  60.     private $createdAt;
  61.     /**
  62.      * @var \DateTime
  63.      * @ORM\Column(type="datetime")
  64.      */
  65.     private $updatedAt;
  66.     public function __construct()
  67.     {
  68.         $this->mediaMetaTags = new ArrayCollection();
  69.         $this->markers = new ArrayCollection();
  70.         $this->createdAt $this->createdAt == NULl ? new \DateTime('now') : $this->createdAt;
  71.         $this->updatedAt $this->updatedAt == NULl ? new \DateTime('now') : $this->updatedAt;
  72.     }
  73.     public function getId(): ?int
  74.     {
  75.         return $this->id;
  76.     }
  77.     public function getTitle(): ?string
  78.     {
  79.         return $this->title;
  80.     }
  81.     public function getMarkers(): Collection
  82.     {
  83.         return $this->markers;
  84.     }
  85.     public function setTitle(string $title): self
  86.     {
  87.         $this->title $title;
  88.         return $this;
  89.     }
  90.     public function getArticles()
  91.     {
  92.         return $this->articles;
  93.     }
  94.     public function getDescription(): ?string
  95.     {
  96.         return $this->description;
  97.     }
  98.     public function setDescription(string $description): self
  99.     {
  100.         $this->description $description;
  101.         return $this;
  102.     }
  103.     /**
  104.      * Get the value of updatedAt
  105.      */
  106.     public function getCreatedAt(): ?\DateTimeInterface
  107.     {
  108.         return $this->createdAt;
  109.     }
  110.     /**
  111.      * Set the value of updatedAt
  112.      *
  113.      * @return  self
  114.      */
  115.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  116.     {
  117.         $this->createdAt $createdAt;
  118.         return $this;
  119.     }
  120.     /**
  121.      * Get the value of updatedAt
  122.      */
  123.     public function getUpdatedAt()
  124.     {
  125.         return $this->updatedAt;
  126.     }
  127.     /**
  128.      * Set the value of updatedAt
  129.      *
  130.      * @return  self
  131.      */
  132.     public function setUpdatedAt($updatedAt)
  133.     {
  134.         $this->updatedAt $updatedAt;
  135.         return $this;
  136.     }
  137.     /**
  138.      * Get the value of mime
  139.      *
  140.      * @return  string
  141.      */
  142.     public function getMime(): ?string
  143.     {
  144.         return $this->mime;
  145.     }
  146.     /**
  147.      * Set the value of mime
  148.      *
  149.      * @param  string  $mime
  150.      *
  151.      * @return  self
  152.      */
  153.     public function setMime(string $mime)
  154.     {
  155.         $this->mime $mime;
  156.         return $this;
  157.     }
  158.     public function isVideo(): bool
  159.     {
  160.         return strpos($this->getMime(), 'video/') === 0;
  161.     }
  162.     /**
  163.      * Get the value of fileName
  164.      *
  165.      * @return  string
  166.      */
  167.     public function getSrc()
  168.     {
  169.         return $this->src;
  170.     }
  171.     /**
  172.      * Set the value of fileName
  173.      *
  174.      * @param  string  $fileName
  175.      *
  176.      * @return  self
  177.      */
  178.     public function setSrc(string $src)
  179.     {
  180.         $this->src $src;
  181.         return $this;
  182.     }
  183.     /**
  184.      * Get the value of mediaMetaTags
  185.      *
  186.      * @return  ArrayCollection
  187.      */
  188.     public function getMediaMetaTags(): ?ArrayCollection
  189.     {
  190.         if (empty($this->mediaMetaTags))
  191.             return null;
  192.         return new ArrayCollection($this->mediaMetaTags->toArray());
  193.     }
  194.     /**
  195.      * Set the value of mediaMetaTags
  196.      *
  197.      * @param  MediaMetaTag  $mediaMetaTags
  198.      *
  199.      * @return  self
  200.      */
  201.     public function setMediaMetaTags(array|ArrayCollection $mediaMetaTags)
  202.     {
  203.         if (is_array($mediaMetaTags))
  204.             $this->mediaMetaTags = new ArrayCollection($mediaMetaTags);
  205.         else
  206.             $this->mediaMetaTags $mediaMetaTags;
  207.         return $this;
  208.     }
  209.     /**
  210.      * Set a specific media meta tag by key.
  211.      *
  212.      * @param MediaMetaTag $metaTag The meta tag object.
  213.      * @param string|null $key The key for the meta tag (e.g. "duration"). If null, standard incrementation is used.
  214.      */
  215.     public function setMediaMetaTag(MediaMetaTag $metaTag, ?string $key null): self
  216.     {
  217.         // If a key is provided
  218.         if ($key !== null) {
  219.             // Check if the meta tag with the given key already exists.
  220.             foreach ($this->mediaMetaTags as $existingMetaTag) {
  221.                 if ($existingMetaTag->getKey() === $key) {
  222.                     // If it exists, remove it.
  223.                     $this->mediaMetaTags->removeElement($existingMetaTag);
  224.                     break;
  225.                 }
  226.             }
  227.             // Set the new meta tag with the provided key.
  228.             $metaTag->setKey($key);
  229.         } else {
  230.             // If no key is provided, use standard incrementation.
  231.             $key count($this->mediaMetaTags);
  232.             $metaTag->setKey((string)$key);
  233.         }
  234.         $this->mediaMetaTags->add($metaTag);
  235.         return $this;
  236.     }
  237.     public function getMediaMetaTagByKey(string $key): ?MediaMetaTag
  238.     {
  239.         foreach ($this->mediaMetaTags as $metaTag) {
  240.             if ($metaTag->getKey() === $key) {
  241.                 return $metaTag;
  242.             }
  243.         }
  244.         return null;
  245.     }
  246. }