src/Entity/MediaObject.php line 65

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiProperty;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use ApiPlatform\Core\Annotation\ApiFilter;
  6. use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
  7. use App\Repository\MediaObjectRepository;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Gedmo\Mapping\Annotation as Gedmo;
  10. use Symfony\Component\HttpFoundation\File\File;
  11. use Symfony\Component\Serializer\Annotation\Groups;
  12. use Symfony\Component\Validator\Constraints as Assert;
  13. use Symfony\Component\HttpFoundation\File\UploadedFile;
  14. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  15. /**
  16.  * @ORM\Entity(repositoryClass=MediaObjectRepository::class)
  17.  * @ApiResource(
  18.  *     normalizationContext={
  19.  *         "groups"={"media_object:read"},
  20.  *     },
  21.  *      denormalizationContext={
  22.  *          "groups"={"media_object:write"}
  23.  *      },
  24.  *     collectionOperations={
  25.  *         "get",
  26.  *         "post"={
  27.  *             "controller"="App\Controller\Api\CreateMediaObjectAction::postImage",
  28.  *             "deserialize"=false,
  29.  *             "security"="is_granted('ROLE_USER')",
  30.  *             "validation_groups"={"Default", "media_object_create"},
  31.  *             "openapi_context"={
  32.  *                 "requestBody"={
  33.  *                     "content"={
  34.  *                         "multipart/form-data"={
  35.  *                             "schema"={
  36.  *                                 "type"="object",
  37.  *                                 "properties"={
  38.  *                                     "file"={
  39.  *                                         "type"="string",
  40.  *                                         "format"="binary"
  41.  *                                     }
  42.  *                                 }
  43.  *                             }
  44.  *                         }
  45.  *                     }
  46.  *                 }
  47.  *             }
  48.  *         }
  49.  *     },
  50.  *     itemOperations={
  51.  *         "get"
  52.  *     }
  53.  * )
  54.  * @ApiFilter(PropertyFilter::class, 
  55.  *      arguments={
  56.  *          "parameterName"="fields", 
  57.  *          "overrideDefaultProperties"=true
  58.  *     }
  59.  * )
  60.  * @Vich\Uploadable
  61.  */
  62. class MediaObject
  63. {
  64.     /**
  65.      * @ORM\Id
  66.      * @ORM\GeneratedValue
  67.      * @ORM\Column(type="integer")
  68.      * @Groups({
  69.      *      "media_object:read", "media_object:read:id", "article:read", "association:read", "award:read", "award_config:read", 
  70.      *      "category:read", "channel:read", "company:read", "expert:read", "objective:read", "program:read", "slide:read", 
  71.      *      "team:read", "teamplay:read", "teamplay_challenge:read", "video:read", "client:read", "chat_message:read", "chat:read",
  72.      *      "program_list", "program_express_list"
  73.      * })
  74.      */
  75.     private $id;
  76.     /**
  77.      * @var string|null
  78.      *
  79.      * @ApiProperty(iri="http://schema.org/contentUrl")
  80.      * @Groups({
  81.      *      "media_object:read", "media_object:read:contentUrl", "article:read", "association:read", "award:read", "award_config:read", 
  82.      *      "category:read", "channel:read", "company:read", "expert:read", "objective:read", "program:read", "slide:read", 
  83.      *      "team:read", "teamplay:read", "teamplay_challenge:read", "video:read", "client:read", "chat_message:read", "chat:read", "team_user:read",
  84.      *      "program_list", "program_express_list", "stats"
  85.      * })
  86.      */
  87.     private $contentUrl "https://placehold.co/600x300";
  88.     /**
  89.      * @var File|null
  90.      *
  91.      * @Assert\NotNull(groups={"media_object_create"})
  92.      * @Vich\UploadableField(mapping="media_object", fileNameProperty="filePath")
  93.      * @Groups({"private"})
  94.      */
  95.     private $file null;
  96.     /**
  97.      * @ORM\Column(type="string", length=255, nullable=true)
  98.      */
  99.     private $filePath;
  100.     /**
  101.      * @ORM\Column(type="string", length=255, nullable=true)
  102.      * @Groups({"media_object:read", "video:read", "media_object:read:mimeType", "article:read"})
  103.      */
  104.     private $mimeType;
  105.     /**
  106.      * @ORM\Column(type="integer", nullable=true)
  107.      * @Groups({"media_object:read", "video:read", "media_object:read:size"})
  108.      */
  109.     private $size;
  110.     /**
  111.      * @ORM\Column(type="array", nullable=true)
  112.      * @Groups({"media_object:read", "video:read", "media_object:read:extra"})
  113.      */
  114.     private $extra = [];
  115.     /**
  116.      * @ORM\Column(type="datetime")
  117.      * @Gedmo\Timestampable(on="create")
  118.      * @Groups({"media_object:read", "video:read", "media_object:read:createdAt"})
  119.      */
  120.     private $createdAt;
  121.     /**
  122.      * @ORM\Column(type="string", length=255, nullable=true)
  123.      * @Groups({
  124.      *      "media_object:read", "media_object:read:name", "article:read", "association:read", "award:read", "award_config:read", 
  125.      *      "category:read", "channel:read", "company:read", "expert:read", "objective:read", "program:read", "slide:read", 
  126.      *      "team:read", "teamplay:read", "teamplay_challenge:read", "video:read", "client:read", "chat_message:read", "chat:read", "team_user:read"
  127.      * })
  128.      */
  129.     private $name;
  130.     public function __toString(): string
  131.     {
  132.         $toString "";
  133.         if(!empty($this->id)) {
  134.             $toString .= "#".$this->id;
  135.         }
  136.         if(!empty($this->getName())) {
  137.             $toString .= " - ".substr($this->getName(),0,30);
  138.         }
  139.         
  140.         return $toString;
  141.     }
  142.     public function serialize()
  143.     {
  144.         $this->file base64_encode($this->file);
  145.         return serialize([
  146.             $this->id,
  147.             $this->contentUrl,
  148.             $this->file,
  149.             $this->filePath,
  150.             $this->mimeType,
  151.             $this->size,
  152.             $this->createdAt,
  153.             $this->name,
  154.         ]);
  155.     }
  156.     public function unserialize($serialized)
  157.     {
  158.         $this->file base64_decode($this->file);
  159.         list (
  160.             $this->id,
  161.             $this->contentUrl,
  162.             $this->file,
  163.             $this->filePath,
  164.             $this->mimeType,
  165.             $this->size,
  166.             $this->createdAt,
  167.             $this->name
  168.         ) = unserialize($serialized, ['allowed_classes' => false]);
  169.     }
  170.     public function __clone()
  171.     {
  172.         $this->id null;
  173.         $this->createdAt = new \DateTime();
  174.     }
  175.     public function getId(): ?int
  176.     {
  177.         return $this->id;
  178.     }
  179.     public function setId(?int $id): self
  180.     {
  181.         $this->id $id;
  182.         return $this;
  183.     }
  184.     /**
  185.      * @Groups({"media_object:read"})
  186.      * @return string|null
  187.      */
  188.     public function getContentUrl(): ?string
  189.     {
  190.         return $this->contentUrl;
  191.     }
  192.     public function setContentUrl(?string $contentUrl): self
  193.     {
  194.         $this->contentUrl $contentUrl;
  195.         return $this;
  196.     }
  197.     public function getFile()
  198.     {
  199.         return $this->file;
  200.     }
  201.     /**
  202.      * @Groups({"media_object:write"})
  203.      */
  204.     public function setFile($file null): self
  205.     {
  206.         if($file instanceof File) {
  207.             $this->file $file;
  208.             $this->setMimeType($this->file->getMimeType());
  209.             $this->setSize($this->file->getSize());
  210.             if($this->file instanceof UploadedFile) {
  211.                 $this->setFilePath($this->file->getClientOriginalName());
  212.                 $this->setName($this->file->getClientOriginalName());
  213.             }
  214.         }
  215.         return $this;
  216.     }
  217.     public function getFilePath(): ?string
  218.     {
  219.         return $this->filePath;
  220.     }
  221.     public function setFilePath(?string $filePath): self
  222.     {
  223.         $this->filePath $filePath;
  224.         return $this;
  225.     }
  226.     public function getMimeType(): ?string
  227.     {
  228.         return $this->mimeType;
  229.     }
  230.     public function setMimeType(string $mimeType): self
  231.     {
  232.         $this->mimeType $mimeType;
  233.         return $this;
  234.     }
  235.     public function getSize(): ?int
  236.     {
  237.         return $this->size;
  238.     }
  239.     public function setSize(int $size): self
  240.     {
  241.         $this->size $size;
  242.         return $this;
  243.     }
  244.     public function getExtra(): ?array
  245.     {
  246.         return $this->extra;
  247.     }
  248.     public function setExtra(?array $extra): self
  249.     {
  250.         $this->extra $extra;
  251.         return $this;
  252.     }
  253.     public function getCreatedAt(): ?\DateTimeInterface
  254.     {
  255.         return $this->createdAt;
  256.     }
  257.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  258.     {
  259.         $this->createdAt $createdAt;
  260.         return $this;
  261.     }
  262.     public function getName(): ?string
  263.     {
  264.         return $this->name;
  265.     }
  266.     public function setName(string $name): self
  267.     {
  268.         $this->name $name;
  269.         return $this;
  270.     }
  271. }