<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
use App\Repository\MediaObjectRepository;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity(repositoryClass=MediaObjectRepository::class)
* @ApiResource(
* normalizationContext={
* "groups"={"media_object:read"},
* },
* denormalizationContext={
* "groups"={"media_object:write"}
* },
* collectionOperations={
* "get",
* "post"={
* "controller"="App\Controller\Api\CreateMediaObjectAction::postImage",
* "deserialize"=false,
* "security"="is_granted('ROLE_USER')",
* "validation_groups"={"Default", "media_object_create"},
* "openapi_context"={
* "requestBody"={
* "content"={
* "multipart/form-data"={
* "schema"={
* "type"="object",
* "properties"={
* "file"={
* "type"="string",
* "format"="binary"
* }
* }
* }
* }
* }
* }
* }
* }
* },
* itemOperations={
* "get"
* }
* )
* @ApiFilter(PropertyFilter::class,
* arguments={
* "parameterName"="fields",
* "overrideDefaultProperties"=true
* }
* )
* @Vich\Uploadable
*/
class MediaObject
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({
* "media_object:read", "media_object:read:id", "article:read", "association:read", "award:read", "award_config:read",
* "category:read", "channel:read", "company:read", "expert:read", "objective:read", "program:read", "slide:read",
* "team:read", "teamplay:read", "teamplay_challenge:read", "video:read", "client:read", "chat_message:read", "chat:read",
* "program_list", "program_express_list"
* })
*/
private $id;
/**
* @var string|null
*
* @ApiProperty(iri="http://schema.org/contentUrl")
* @Groups({
* "media_object:read", "media_object:read:contentUrl", "article:read", "association:read", "award:read", "award_config:read",
* "category:read", "channel:read", "company:read", "expert:read", "objective:read", "program:read", "slide:read",
* "team:read", "teamplay:read", "teamplay_challenge:read", "video:read", "client:read", "chat_message:read", "chat:read", "team_user:read",
* "program_list", "program_express_list", "stats"
* })
*/
private $contentUrl = "https://placehold.co/600x300";
/**
* @var File|null
*
* @Assert\NotNull(groups={"media_object_create"})
* @Vich\UploadableField(mapping="media_object", fileNameProperty="filePath")
* @Groups({"private"})
*/
private $file = null;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $filePath;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"media_object:read", "video:read", "media_object:read:mimeType", "article:read"})
*/
private $mimeType;
/**
* @ORM\Column(type="integer", nullable=true)
* @Groups({"media_object:read", "video:read", "media_object:read:size"})
*/
private $size;
/**
* @ORM\Column(type="array", nullable=true)
* @Groups({"media_object:read", "video:read", "media_object:read:extra"})
*/
private $extra = [];
/**
* @ORM\Column(type="datetime")
* @Gedmo\Timestampable(on="create")
* @Groups({"media_object:read", "video:read", "media_object:read:createdAt"})
*/
private $createdAt;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({
* "media_object:read", "media_object:read:name", "article:read", "association:read", "award:read", "award_config:read",
* "category:read", "channel:read", "company:read", "expert:read", "objective:read", "program:read", "slide:read",
* "team:read", "teamplay:read", "teamplay_challenge:read", "video:read", "client:read", "chat_message:read", "chat:read", "team_user:read"
* })
*/
private $name;
public function __toString(): string
{
$toString = "";
if(!empty($this->id)) {
$toString .= "#".$this->id;
}
if(!empty($this->getName())) {
$toString .= " - ".substr($this->getName(),0,30);
}
return $toString;
}
public function serialize()
{
$this->file = base64_encode($this->file);
return serialize([
$this->id,
$this->contentUrl,
$this->file,
$this->filePath,
$this->mimeType,
$this->size,
$this->createdAt,
$this->name,
]);
}
public function unserialize($serialized)
{
$this->file = base64_decode($this->file);
list (
$this->id,
$this->contentUrl,
$this->file,
$this->filePath,
$this->mimeType,
$this->size,
$this->createdAt,
$this->name
) = unserialize($serialized, ['allowed_classes' => false]);
}
public function __clone()
{
$this->id = null;
$this->createdAt = new \DateTime();
}
public function getId(): ?int
{
return $this->id;
}
public function setId(?int $id): self
{
$this->id = $id;
return $this;
}
/**
* @Groups({"media_object:read"})
* @return string|null
*/
public function getContentUrl(): ?string
{
return $this->contentUrl;
}
public function setContentUrl(?string $contentUrl): self
{
$this->contentUrl = $contentUrl;
return $this;
}
public function getFile()
{
return $this->file;
}
/**
* @Groups({"media_object:write"})
*/
public function setFile($file = null): self
{
if($file instanceof File) {
$this->file = $file;
$this->setMimeType($this->file->getMimeType());
$this->setSize($this->file->getSize());
if($this->file instanceof UploadedFile) {
$this->setFilePath($this->file->getClientOriginalName());
$this->setName($this->file->getClientOriginalName());
}
}
return $this;
}
public function getFilePath(): ?string
{
return $this->filePath;
}
public function setFilePath(?string $filePath): self
{
$this->filePath = $filePath;
return $this;
}
public function getMimeType(): ?string
{
return $this->mimeType;
}
public function setMimeType(string $mimeType): self
{
$this->mimeType = $mimeType;
return $this;
}
public function getSize(): ?int
{
return $this->size;
}
public function setSize(int $size): self
{
$this->size = $size;
return $this;
}
public function getExtra(): ?array
{
return $this->extra;
}
public function setExtra(?array $extra): self
{
$this->extra = $extra;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
}