src/Entity/Attachment.php line 18

  1. <?php
  2. namespace App\Entity;
  3. use App\Model\UploadedFileAwareInterface;
  4. use App\Repository\AttachmentRepository;
  5. use App\Trait\DateTrait;
  6. use DateTimeImmutable;
  7. use Doctrine\DBAL\Types\Types;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Stringable;
  10. use Symfony\Component\HttpFoundation\File\File;
  11. use Symfony\Component\Serializer\Annotation\Groups;
  12. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  13. #[ORM\Entity(repositoryClass: AttachmentRepository::class)]
  14. #[Vich\Uploadable]
  15. class Attachment implements Stringable, UploadedFileAwareInterface
  16. {
  17.     use DateTrait;
  18.     #[ORM\Id]
  19.     #[ORM\GeneratedValue]
  20.     #[ORM\Column]
  21.     #[Groups(['article:list', 'establishment:list'])]
  22.     protected ?int $id = null;
  23.     #[ORM\Column(length: 255, nullable: true)]
  24.     #[Groups(['article:list', 'establishment:list'])]
  25.     private ?string $name = '';
  26.     #[ORM\Column(options: ["unsigned" => true])]
  27.     #[Groups(['article:list', 'establishment:list'])]
  28.     private int $size = 0;
  29.     #[Vich\UploadableField(mapping: "attachments", fileNameProperty: "name", size: "size", mimeType: "mimeType", originalName: "originalName", dimensions: "dimensions")]
  30.     private ?File $file = null;
  31.     #[ORM\ManyToOne(inversedBy: 'attachments')]
  32.     private ?Flow $flow = null;
  33.     #[ORM\Column(length: 255, nullable: true)]
  34.     private ?string $mimeType = null;
  35.     #[ORM\Column(length: 255, nullable: true)]
  36.     private ?string $originalName = null;
  37.     #[ORM\Column(type: Types::JSON, nullable: true)]
  38.     private ?array $dimensions = null;
  39.     public function __construct()
  40.     {
  41.         if (!$this->createdAt) $this->createdAt = new DateTimeImmutable();
  42.     }
  43.     public function getId(): ?int
  44.     {
  45.         return $this->id ?: 0;
  46.     }
  47.     public function getName(): ?string
  48.     {
  49.         return $this->name;
  50.     }
  51.     public function setName(?string $name): self
  52.     {
  53.         $this->name = $name ?: '';
  54.         return $this;
  55.     }
  56.     public function getSize(): ?int
  57.     {
  58.         return $this->size;
  59.     }
  60.     public function setSize(?int $size): self
  61.     {
  62.         $this->size = $size ?: 0;
  63.         return $this;
  64.     }
  65.     public function getFile(): ?File
  66.     {
  67.         return $this->file;
  68.     }
  69.     public function setFile(?File $file = null): self
  70.     {
  71.         $this->file = $file;
  72.         $this->updatedAt = new DateTimeImmutable();
  73.         return $this;
  74.     }
  75.     public function getFlow(): ?Flow
  76.     {
  77.         return $this->flow;
  78.     }
  79.     public function setFlow(?Flow $flow): self
  80.     {
  81.         $this->flow = $flow;
  82.         return $this;
  83.     }
  84.     public function getMimeType(): ?string
  85.     {
  86.         return $this->mimeType;
  87.     }
  88.     public function setMimeType(?string $mimeType): static
  89.     {
  90.         $this->mimeType = $mimeType;
  91.         return $this;
  92.     }
  93.     public function getOriginalName(): ?string
  94.     {
  95.         return $this->originalName;
  96.     }
  97.     public function setOriginalName(?string $originalName): static
  98.     {
  99.         $this->originalName = $originalName;
  100.         return $this;
  101.     }
  102.     public function getDimensions(): ?array
  103.     {
  104.         return $this->dimensions;
  105.     }
  106.     public function setDimensions(?array $dimensions): static
  107.     {
  108.         $this->dimensions = $dimensions;
  109.         return $this;
  110.     }
  111.     public function getFilePropertyMapping()
  112.     {
  113.         return ["name" => 'file'];
  114.     }
  115.   
  116.     public function __serialize(): array
  117.     {
  118.         return [
  119.             $this->name,
  120.             $this->size,
  121.             $this->file,
  122.             $this->createdAt,
  123.             $this->updatedAt,
  124.         ];
  125.     }
  126.     public function __unserialize(array $data): void
  127.     {
  128.         list(
  129.             $this->name,
  130.             $this->size,
  131.             $this->file,
  132.             $this->createdAt,
  133.             $this->updatedAt,
  134.             ) = $data;
  135.     }
  136.     public function __toString(): string
  137.     {
  138.         return $this->name;
  139.     }
  140. }