Skip to content

Commit 9241e03

Browse files
Merge pull request #34 from JosephGabito/joseph/mime-type-matching
2 parents e964a1d + fdd1495 commit 9241e03

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

src/Files/ValueObjects/MimeType.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,22 @@ public static function isValid(string $mimeType): bool
169169
);
170170
}
171171

172+
/**
173+
* Checks if this MIME type is a specific type.
174+
*
175+
* This method returns true when the stored MIME type begins with the
176+
* given prefix. For example, `"audio"` matches `"audio/mpeg"`.
177+
*
178+
* @since n.e.x.t
179+
*
180+
* @param string $mimeType The MIME type prefix to check (e.g., "audio", "image").
181+
* @return bool True if this MIME type is of the specified type.
182+
*/
183+
public function isType(string $mimeType): bool
184+
{
185+
return strpos($this->value, strtolower($mimeType) . '/') === 0;
186+
}
187+
172188
/**
173189
* Checks if this is an image MIME type.
174190
*
@@ -178,7 +194,7 @@ public static function isValid(string $mimeType): bool
178194
*/
179195
public function isImage(): bool
180196
{
181-
return strpos($this->value, 'image/') === 0;
197+
return $this->isType('image');
182198
}
183199

184200
/**
@@ -190,7 +206,7 @@ public function isImage(): bool
190206
*/
191207
public function isAudio(): bool
192208
{
193-
return strpos($this->value, 'audio/') === 0;
209+
return $this->isType('audio');
194210
}
195211

196212
/**
@@ -202,7 +218,7 @@ public function isAudio(): bool
202218
*/
203219
public function isVideo(): bool
204220
{
205-
return strpos($this->value, 'video/') === 0;
221+
return $this->isType('video');
206222
}
207223

208224
/**
@@ -214,7 +230,7 @@ public function isVideo(): bool
214230
*/
215231
public function isText(): bool
216232
{
217-
return strpos($this->value, 'text/') === 0;
233+
return $this->isType('text');
218234
}
219235

220236
/**

tests/unit/Files/ValueObjects/MimeTypeTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,22 @@ public function testNormalizesValues(): void
282282
$mimeType = new MimeType('IMAGE/JPEG');
283283
$this->assertEquals('image/jpeg', (string) $mimeType);
284284
}
285+
286+
public function testIsType(): void
287+
{
288+
$imageType = new MimeType('image/jpeg');
289+
$fontType = new MimeType('font/ttf');
290+
291+
$this->assertTrue($imageType->isType('image'));
292+
$this->assertFalse($imageType->isType('video'));
293+
294+
$this->assertTrue($fontType->isType('font'));
295+
$this->assertFalse($fontType->isType('image'));
296+
297+
// Case insensitive.
298+
$this->assertTrue($fontType->isType('Font'));
299+
$this->assertTrue($fontType->isType('FONT'));
300+
$this->assertTrue($imageType->isType('image'));
301+
$this->assertTrue($imageType->isType('IMAGE'));
302+
}
285303
}

0 commit comments

Comments
 (0)