<?php
namespace App\Entity;
use App\Entity\MediaMetaTag;
use App\Repository\MediaRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
/**
* @ORM\Entity(repositoryClass=MediaRepository::class)
*/
class Media
{
/**
* @var int
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @var string
* @ORM\Column(type="string", length=255)
* @Assert\Length(min=3, max=255, minMessage="Le titre est trop court !")
*/
private $title;
/**
* @var string
* @ORM\Column(type="string", length=255)
*/
private $src;
/**
* @ORM\ManyToMany(targetEntity=VideoMarker::class, mappedBy="attachments", fetch="EAGER")
*/
private $markers;
/**
* @var string
* @ORM\Column(type="text", nullable=true)
*/
private $description;
/**
* @var string
* @ORM\Column(type="string", length=255)
*/
private $mime;
/**
* @var Collection
* @ORM\OneToMany(targetEntity=MediaMetaTag::class, mappedBy="media",cascade={"persist"}, orphanRemoval=true)
*/
private $mediaMetaTags;
/**
* @var Article
* @ORM\OneToMany(targetEntity=Article::class, mappedBy="video", orphanRemoval=true)
*/
private $articles;
/**
* @var \DateTime
* @ORM\Column(type="datetime")
*/
private $createdAt;
/**
* @var \DateTime
* @ORM\Column(type="datetime")
*/
private $updatedAt;
public function __construct()
{
$this->mediaMetaTags = new ArrayCollection();
$this->markers = new ArrayCollection();
$this->createdAt = $this->createdAt == NULl ? new \DateTime('now') : $this->createdAt;
$this->updatedAt = $this->updatedAt == NULl ? new \DateTime('now') : $this->updatedAt;
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function getMarkers(): Collection
{
return $this->markers;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getArticles()
{
return $this->articles;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(string $description): self
{
$this->description = $description;
return $this;
}
/**
* Get the value of updatedAt
*/
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
/**
* Set the value of updatedAt
*
* @return self
*/
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get the value of updatedAt
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* Set the value of updatedAt
*
* @return self
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get the value of mime
*
* @return string
*/
public function getMime(): ?string
{
return $this->mime;
}
/**
* Set the value of mime
*
* @param string $mime
*
* @return self
*/
public function setMime(string $mime)
{
$this->mime = $mime;
return $this;
}
public function isVideo(): bool
{
return strpos($this->getMime(), 'video/') === 0;
}
/**
* Get the value of fileName
*
* @return string
*/
public function getSrc()
{
return $this->src;
}
/**
* Set the value of fileName
*
* @param string $fileName
*
* @return self
*/
public function setSrc(string $src)
{
$this->src = $src;
return $this;
}
/**
* Get the value of mediaMetaTags
*
* @return ArrayCollection
*/
public function getMediaMetaTags(): ?ArrayCollection
{
if (empty($this->mediaMetaTags))
return null;
return new ArrayCollection($this->mediaMetaTags->toArray());
}
/**
* Set the value of mediaMetaTags
*
* @param MediaMetaTag $mediaMetaTags
*
* @return self
*/
public function setMediaMetaTags(array|ArrayCollection $mediaMetaTags)
{
if (is_array($mediaMetaTags))
$this->mediaMetaTags = new ArrayCollection($mediaMetaTags);
else
$this->mediaMetaTags = $mediaMetaTags;
return $this;
}
/**
* Set a specific media meta tag by key.
*
* @param MediaMetaTag $metaTag The meta tag object.
* @param string|null $key The key for the meta tag (e.g. "duration"). If null, standard incrementation is used.
*/
public function setMediaMetaTag(MediaMetaTag $metaTag, ?string $key = null): self
{
// If a key is provided
if ($key !== null) {
// Check if the meta tag with the given key already exists.
foreach ($this->mediaMetaTags as $existingMetaTag) {
if ($existingMetaTag->getKey() === $key) {
// If it exists, remove it.
$this->mediaMetaTags->removeElement($existingMetaTag);
break;
}
}
// Set the new meta tag with the provided key.
$metaTag->setKey($key);
} else {
// If no key is provided, use standard incrementation.
$key = count($this->mediaMetaTags);
$metaTag->setKey((string)$key);
}
$this->mediaMetaTags->add($metaTag);
return $this;
}
public function getMediaMetaTagByKey(string $key): ?MediaMetaTag
{
foreach ($this->mediaMetaTags as $metaTag) {
if ($metaTag->getKey() === $key) {
return $metaTag;
}
}
return null;
}
}