diff --git a/CHANGELOG.md b/CHANGELOG.md index 532ecc8..edbc7d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ CHANGELOG for 1.x =================== +## v1.9.0 - (2024-06-XX) +### Added +- `ImageableInterface`, `PdfInterface` and their trait to demonstrate how to properly configure VichUploader annotations/attributes +- `MimeTypesUtils` helper for constraints mimeTypes check + ## v1.8.0 - (2024-06-25) **Add Entity json History feature** diff --git a/src/Entity/ImageableInterface.php b/src/Entity/ImageableInterface.php new file mode 100644 index 0000000..8e5a721 --- /dev/null +++ b/src/Entity/ImageableInterface.php @@ -0,0 +1,36 @@ + + */ +interface ImageableInterface +{ + public const IMAGE_MAPPING = 'image'; + public const IMAGE_MAX_SIZE = '8M'; + public const IMAGE_MAX_WIDTH = '1000'; + public const IMAGE_MAX_HEIGHT = '1000'; + + public function hasImage(): bool; + + public function getFormattedImageSize(): ?string; + + public function setImageFile(File|false|null $file = null): void; + + public function getImageFile(): File|false|null; + + public function getImageOriginalName(): ?string; + + public function setImageOriginalName(?string $name): self; + + public function getImageName(): ?string; + + public function setImageName(?string $name): self; + + public function getImageSize(): ?float; + + public function setImageSize(?float $size): self; +} diff --git a/src/Entity/ImageableTrait.php b/src/Entity/ImageableTrait.php new file mode 100644 index 0000000..bbfa699 --- /dev/null +++ b/src/Entity/ImageableTrait.php @@ -0,0 +1,129 @@ +imageName !== null; + } + + public function getFormattedImageSize(): ?string + { + $size = $this->getImageSize(); + if ($size === null) { + return null; + } + + return MathUtils::formatBytes($size); + } + + /** + * If manually uploading a file (i.e. not using Symfony Form) ensure an instance + * of 'UploadedFile' is injected into this setter to trigger the update. If this + * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter + * must be able to accept an instance of 'File' as the bundle will inject one here + * during Doctrine hydration. + */ + public function setImageFile(File|false|null $file = null): void + { + $this->imageFile = $file; + + if ($file instanceof UploadedFile) { + $this->imageOriginalName = $file->getClientOriginalName(); + // It is required that at least one field changes if you are using doctrine + // otherwise the event listeners won't be called and the file is lost + $this->imageUpdatedAt = new \DateTimeImmutable(); + if ($this instanceof UuidInterface && $this->getUuid() === null) { + $this->setUuid(Uuid::v7()); + } + } + } + + public function getImageFile(): File|false|null + { + return $this->imageFile; + } + + public function getImageOriginalName(): ?string + { + return $this->imageOriginalName; + } + + public function setImageOriginalName(?string $name): self + { + $this->imageOriginalName = $name; + + return $this; + } + + public function getImageName(): ?string + { + return $this->imageName; + } + + public function setImageName(?string $name): self + { + $this->imageName = $name; + + return $this; + } + + public function getImageSize(): ?float + { + return $this->imageSize; + } + + public function setImageSize(?float $size): self + { + $this->imageSize = $size; + + return $this; + } +} diff --git a/src/Entity/PdfInterface.php b/src/Entity/PdfInterface.php new file mode 100644 index 0000000..5634bad --- /dev/null +++ b/src/Entity/PdfInterface.php @@ -0,0 +1,34 @@ + + */ +interface PdfInterface +{ + public const PDF_MAPPING = 'pdf'; + public const PDF_MAX_SIZE = '20M'; + + public function hasPdf(): bool; + + public function getFormattedPdfSize(): ?string; + + public function setPdfFile(File|false|null $file = null): void; + + public function getPdfFile(): File|false|null; + + public function getPdfOriginalName(): ?string; + + public function setPdfOriginalName(?string $name): self; + + public function getPdfName(): ?string; + + public function setPdfName(?string $name): self; + + public function getPdfSize(): ?float; + + public function setPdfSize(?float $size): self; +} diff --git a/src/Entity/PdfTrait.php b/src/Entity/PdfTrait.php new file mode 100644 index 0000000..9268ba9 --- /dev/null +++ b/src/Entity/PdfTrait.php @@ -0,0 +1,130 @@ +pdfName !== null; + } + + public function getFormattedPdfSize(): ?string + { + $size = $this->getPdfSize(); + if ($size === null) { + return null; + } + + return MathUtils::formatBytes($size); + } + + /** + * If manually uploading a file (i.e. not using Symfony Form) ensure an instance + * of 'UploadedFile' is injected into this setter to trigger the update. If this + * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter + * must be able to accept an instance of 'File' as the bundle will inject one here + * during Doctrine hydration. + */ + public function setPdfFile(File|false|null $file = null): void + { + $this->pdfFile = $file; + + if ($file instanceof UploadedFile) { + $this->pdfOriginalName = $file->getClientOriginalName(); + // It is required that at least one field changes if you are using doctrine + // otherwise the event listeners won't be called and the file is lost + $this->pdfUpdatedAt = new \DateTimeImmutable(); + if ($this instanceof UuidInterface && $this->getUuid() === null) { + $this->setUuid(Uuid::v7()); + } + } + } + + public function getPdfFile(): File|false|null + { + return $this->pdfFile; + } + + public function getPdfOriginalName(): ?string + { + return $this->pdfOriginalName; + } + + public function setPdfOriginalName(?string $name): self + { + $this->pdfOriginalName = $name; + + return $this; + } + + public function getPdfName(): ?string + { + return $this->pdfName; + } + + public function setPdfName(?string $name): self + { + $this->pdfName = $name; + + return $this; + } + + public function getPdfSize(): ?float + { + return $this->pdfSize; + } + + public function setPdfSize(?float $size): self + { + $this->pdfSize = $size; + + return $this; + } +} diff --git a/src/Entity/UuidInterface.php b/src/Entity/UuidInterface.php new file mode 100644 index 0000000..6f67d50 --- /dev/null +++ b/src/Entity/UuidInterface.php @@ -0,0 +1,12 @@ +uuid; + } + + public function setUuid(?Uuid $uuid): static + { + $this->uuid = $uuid; + + return $this; + } +} diff --git a/src/Utils/MimeTypesUtils.php b/src/Utils/MimeTypesUtils.php new file mode 100644 index 0000000..d01b8ec --- /dev/null +++ b/src/Utils/MimeTypesUtils.php @@ -0,0 +1,11 @@ + + */ +class MimeTypesUtils +{ + public const PDF = ['application/pdf', 'application/acrobat', 'application/nappdf', 'application/x-pdf', 'image/pdf']; +}