src/Entity/MediaObject.php line 50

  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\OpenApi\Model;
  10. use App\Controller\CreateMediaObjectAction;
  11. use Doctrine\ORM\Mapping as ORM;
  12. use Symfony\Component\HttpFoundation\File\File;
  13. use Symfony\Component\Serializer\Annotation\Groups;
  14. use Symfony\Component\Validator\Constraints as Assert;
  15. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  16. #[Vich\Uploadable]
  17. #[ORM\Entity]
  18. #[ApiResource(
  19.     normalizationContext: ['groups' => ['media_object:read']], 
  20.     types: ['https://schema.org/MediaObject'],
  21.     operations: [
  22.         new Get(),
  23.         new GetCollection(),
  24.         new Post(
  25.             controllerCreateMediaObjectAction::class, 
  26.             deserializefalse
  27.             validationContext: ['groups' => ['Default''media_object_create']], 
  28.             openapi: new Model\Operation(
  29.                 requestBody: new Model\RequestBody(
  30.                     content: new \ArrayObject([
  31.                         'multipart/form-data' => [
  32.                             'schema' => [
  33.                                 'type' => 'object'
  34.                                 'properties' => [
  35.                                     'file' => [
  36.                                         'type' => 'string'
  37.                                         'format' => 'binary'
  38.                                     ]
  39.                                 ]
  40.                             ]
  41.                         ]
  42.                     ])
  43.                 )
  44.             )
  45.         )
  46.     ]
  47. )]
  48. class MediaObject
  49. {
  50.     #[ORM\IdORM\ColumnORM\GeneratedValue]
  51.     private ?int $id null;
  52.     #[ApiProperty(types: ['https://schema.org/contentUrl'])]
  53.     #[Groups(['media_object:read'])]
  54.     public ?string $contentUrl null;
  55.     
  56.     #[Vich\UploadableField(mapping"media_object"fileNameProperty"filePath")]
  57.     #[Assert\NotNull(groups: ['media_object_create'])]
  58.     public ?File $file null;
  59.     #[ORM\Column(nullabletrue)] 
  60.     public ?string $filePath null;
  61.     public function getId(): ?int
  62.     {
  63.         return $this->id;
  64.     }
  65. }