src/Entity/MediaObject.php line 69

Open in your IDE?
  1. <?php
  2. // api/src/Entity/MediaObject.php
  3. namespace App\Entity;
  4. use ApiPlatform\Metadata\ApiProperty;
  5. use ApiPlatform\Metadata\ApiResource;
  6. use ApiPlatform\Metadata\Get;
  7. use ApiPlatform\Metadata\GetCollection;
  8. use ApiPlatform\Metadata\Post;
  9. use ApiPlatform\Metadata\Delete;
  10. use App\Controller\CreateMediaObjectAction;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Doctrine\Common\Collections\Collection;
  13. use Doctrine\ORM\Mapping as ORM;
  14. use Symfony\Component\HttpFoundation\File\File;
  15. use Symfony\Component\Serializer\Annotation\Groups;
  16. use Symfony\Component\Validator\Constraints as Assert;
  17. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  18. use ApiPlatform\Metadata\ApiFilter;
  19. use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
  20. use Doctrine\DBAL\Types\Types;
  21. use Symfony\Component\Serializer\Annotation\MaxDepth;
  22. use ApiPlatform\Doctrine\Orm\Filter\DateFilter;
  23. #[Vich\Uploadable]
  24. #[ORM\Entity]
  25. #[ApiResource(
  26.     normalizationContext: ['groups' => ['media_object:read']], 
  27.     types: ['https://schema.org/MediaObject'],
  28.     operations: [
  29.         new Get(),
  30.         new GetCollection(),
  31.         new Delete(),
  32.         new Post(
  33.             controllerCreateMediaObjectAction::class, 
  34.             deserializefalse
  35.             validationContext: ['groups' => ['Default''media_object_create']], 
  36.             openapiContext: [
  37.                 'requestBody' => [
  38.                     'content' => [
  39.                         'multipart/form-data' => [
  40.                             'schema' => [
  41.                                 'type' => 'object'
  42.                                 'properties' => [
  43.                                     'file' => [
  44.                                         'type' => 'string'
  45.                                         'format' => 'binary'
  46.                                     ]
  47.                                 ]
  48.                             ]
  49.                         ]
  50.                     ]
  51.                 ]
  52.             ]
  53.         )
  54.     ],
  55.     order: ['id' => 'DESC'],
  56. )]
  57. #[ApiFilter(DateFilter::class, properties: ['uploadTimestamp'])]
  58. #[ORM\HasLifecycleCallbacks]
  59. class MediaObject
  60. {
  61.     #[Groups(['media_object:read''PwAccident:read'])]
  62.     #[ORM\IdORM\ColumnORM\GeneratedValue]
  63.     private ?int $id null;
  64.     #[ApiProperty(types: ['https://schema.org/contentUrl'])]
  65.     #[Groups(['media_object:read''PwAccident:read'])]
  66.     public ?string $contentUrl null;
  67.     
  68.     #[Vich\UploadableField(mapping"media_object"fileNameProperty"filePath")]
  69.     #[Assert\NotNull(groups: ['media_object_create'])]
  70.     public ?File $file null;
  71.     #[Groups(['media_object:read''PwAccident:read'])]
  72.     #[ORM\Column(typeTypes::DATETIME_MUTABLEnullabletrue)]
  73.     public $uploadTimestamp;
  74.     #[ORM\Column(nullabletrue)] 
  75.     #[Groups(['media_object:read''PwAccident:read'])]
  76.     public ?string $filePath null;
  77.     public function getId(): ?int
  78.     
  79.         return $this->id;
  80.     }
  81.     public function getUploadTimestamp(): ?\DateTimeInterface
  82.     {
  83.         return $this->uploadTimestamp;
  84.     }
  85.    
  86.     
  87.  
  88.     #[ORM\PrePersist]
  89.     public function setCreatedAtValue(): void
  90.     {
  91.         $this->uploadTimestamp = new \DateTime(); 
  92.     }
  93.    
  94.    
  95. }