From c75ac16a3358cc6a95c4c004f3c730f7c35d4044 Mon Sep 17 00:00:00 2001 From: Joseph G Date: Tue, 5 Aug 2025 10:58:16 +0800 Subject: [PATCH 1/4] Add isType method to File DTO Introduces the isType(string $type): bool method to the File DTO, allowing checks for specific MIME types. Also improves comments and formatting around data URI handling. --- src/Files/DTO/File.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Files/DTO/File.php b/src/Files/DTO/File.php index d129b65e..8ad507b5 100644 --- a/src/Files/DTO/File.php +++ b/src/Files/DTO/File.php @@ -87,10 +87,11 @@ private function detectAndProcessFile(string $file, ?string $providedMimeType): return; } - // Check if it's a data URI + // Data URI pattern. $dataUriPattern = '/^data:(?:([a-zA-Z0-9][a-zA-Z0-9!#$&\-\^_+.]*\/[a-zA-Z0-9][a-zA-Z0-9!#$&\-\^_+.]*' - . '(?:;[a-zA-Z0-9\-]+=[a-zA-Z0-9\-]+)*)?;)?base64,([A-Za-z0-9+\/]*={0,2})$/'; + . '(?:;[a-zA-Z0-9\-]+=[a-zA-Z0-9\-]+)*)?;)?base64,([A-Za-z0-9+\/]*={0,2})$/'; + // Check if it's a data URI. if (preg_match($dataUriPattern, $file, $matches)) { $this->fileType = FileTypeEnum::inline(); $this->base64Data = $matches[2]; // Extract just the base64 data @@ -285,6 +286,20 @@ public function isText(): bool return $this->mimeType->isText(); } + /** + * Checks if the file is a specific MIME type. + * + * @since n.e.x.t + * + * @param string $type The type to check. + * + * @return bool True if the file is of the specified type. + */ + public function isType(string $type): bool + { + return $this->mimeType->isType($type); + } + /** * Determines the MIME type from various sources. * From 4a606f052432f6b56773956bf2e11ca67214c967 Mon Sep 17 00:00:00 2001 From: Joseph G Date: Tue, 5 Aug 2025 10:58:23 +0800 Subject: [PATCH 2/4] Add isType assertions to FileTest for video files Extended the FileTest unit test to include assertions for the isType method, specifically verifying correct type detection for 'video', 'image', 'audio', and 'text'. --- tests/unit/Files/DTO/FileTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/unit/Files/DTO/FileTest.php b/tests/unit/Files/DTO/FileTest.php index bebf21b9..c5efd879 100644 --- a/tests/unit/Files/DTO/FileTest.php +++ b/tests/unit/Files/DTO/FileTest.php @@ -207,6 +207,10 @@ public function testMimeTypeMethods(): void $this->assertFalse($file->isImage()); $this->assertFalse($file->isAudio()); $this->assertFalse($file->isText()); + $this->assertTrue($file->isType('video')); + $this->assertFalse($file->isType('image')); + $this->assertFalse($file->isType('audio')); + $this->assertFalse($file->isType('text')); } /** From 6d2f700387498bb43d81b0e61c678bcf7df95d96 Mon Sep 17 00:00:00 2001 From: Joseph G Date: Wed, 6 Aug 2025 04:27:23 +0800 Subject: [PATCH 3/4] Rename isType to isMimeType in File DTO Refactored the File DTO method isType to isMimeType for clarity. Updated all relevant unit tests to use the new method name. --- src/Files/DTO/File.php | 2 +- tests/unit/Files/DTO/FileTest.php | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Files/DTO/File.php b/src/Files/DTO/File.php index 8ad507b5..38874b16 100644 --- a/src/Files/DTO/File.php +++ b/src/Files/DTO/File.php @@ -295,7 +295,7 @@ public function isText(): bool * * @return bool True if the file is of the specified type. */ - public function isType(string $type): bool + public function isMimeType(string $type): bool { return $this->mimeType->isType($type); } diff --git a/tests/unit/Files/DTO/FileTest.php b/tests/unit/Files/DTO/FileTest.php index c5efd879..76a94155 100644 --- a/tests/unit/Files/DTO/FileTest.php +++ b/tests/unit/Files/DTO/FileTest.php @@ -207,10 +207,10 @@ public function testMimeTypeMethods(): void $this->assertFalse($file->isImage()); $this->assertFalse($file->isAudio()); $this->assertFalse($file->isText()); - $this->assertTrue($file->isType('video')); - $this->assertFalse($file->isType('image')); - $this->assertFalse($file->isType('audio')); - $this->assertFalse($file->isType('text')); + $this->assertTrue($file->isMimeType('video')); + $this->assertFalse($file->isMimeType('image')); + $this->assertFalse($file->isMimeType('audio')); + $this->assertFalse($file->isMimeType('text')); } /** From 8534df17c8982a9e44118f1ccbbf4f381fa27d4f Mon Sep 17 00:00:00 2001 From: Joseph G Date: Wed, 6 Aug 2025 04:33:20 +0800 Subject: [PATCH 4/4] Clarify mime type parameter in File DTO docblock Updated the docblock for the type parameter in the File DTO to specify that it expects a mime type (e.g. 'image', 'text', 'video', 'audio'). This improves code documentation and developer understanding. --- src/Files/DTO/File.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Files/DTO/File.php b/src/Files/DTO/File.php index 38874b16..8961b8dd 100644 --- a/src/Files/DTO/File.php +++ b/src/Files/DTO/File.php @@ -291,7 +291,7 @@ public function isText(): bool * * @since n.e.x.t * - * @param string $type The type to check. + * @param string $type The mime type to check (e.g. 'image', 'text', 'video', 'audio'). * * @return bool True if the file is of the specified type. */