Skip to content

Commit 84c36cd

Browse files
authored
Merge pull request #36 from JosephGabito/joseph/dto-file-generic-is-type-check
Adds a new `isType(string $type): bool` method to the File DTO, enabling specific MIME type checking beyond the existing predefined type methods.
2 parents 9241e03 + 8534df1 commit 84c36cd

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/Files/DTO/File.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,11 @@ private function detectAndProcessFile(string $file, ?string $providedMimeType):
8787
return;
8888
}
8989

90-
// Check if it's a data URI
90+
// Data URI pattern.
9191
$dataUriPattern = '/^data:(?:([a-zA-Z0-9][a-zA-Z0-9!#$&\-\^_+.]*\/[a-zA-Z0-9][a-zA-Z0-9!#$&\-\^_+.]*'
92-
. '(?:;[a-zA-Z0-9\-]+=[a-zA-Z0-9\-]+)*)?;)?base64,([A-Za-z0-9+\/]*={0,2})$/';
92+
. '(?:;[a-zA-Z0-9\-]+=[a-zA-Z0-9\-]+)*)?;)?base64,([A-Za-z0-9+\/]*={0,2})$/';
9393

94+
// Check if it's a data URI.
9495
if (preg_match($dataUriPattern, $file, $matches)) {
9596
$this->fileType = FileTypeEnum::inline();
9697
$this->base64Data = $matches[2]; // Extract just the base64 data
@@ -285,6 +286,20 @@ public function isText(): bool
285286
return $this->mimeType->isText();
286287
}
287288

289+
/**
290+
* Checks if the file is a specific MIME type.
291+
*
292+
* @since n.e.x.t
293+
*
294+
* @param string $type The mime type to check (e.g. 'image', 'text', 'video', 'audio').
295+
*
296+
* @return bool True if the file is of the specified type.
297+
*/
298+
public function isMimeType(string $type): bool
299+
{
300+
return $this->mimeType->isType($type);
301+
}
302+
288303
/**
289304
* Determines the MIME type from various sources.
290305
*

tests/unit/Files/DTO/FileTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ public function testMimeTypeMethods(): void
207207
$this->assertFalse($file->isImage());
208208
$this->assertFalse($file->isAudio());
209209
$this->assertFalse($file->isText());
210+
$this->assertTrue($file->isMimeType('video'));
211+
$this->assertFalse($file->isMimeType('image'));
212+
$this->assertFalse($file->isMimeType('audio'));
213+
$this->assertFalse($file->isMimeType('text'));
210214
}
211215

212216
/**

0 commit comments

Comments
 (0)