Skip to content

Commit 75f07f2

Browse files
committed
Add hasMimeType method to MimeType value object
Introduces a hasMimeType method to check if a MIME type starts with a given type, refactors isImage, isAudio, isVideo, and isText to use this new method, and adds corresponding unit tests.
1 parent e964a1d commit 75f07f2

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/Files/ValueObjects/MimeType.php

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

172+
/**
173+
* Checks if this MIME type has a specific MIME type.
174+
*
175+
* This method checks if the MIME type starts with the specified MIME type.
176+
*
177+
* @since n.e.x.t
178+
*
179+
* @param string $mimeType The MIME type to check.
180+
* @return bool True if the MIME type has the specified MIME type.
181+
*/
182+
public function hasMimeType(string $mimeType): bool
183+
{
184+
return strpos($this->value, $mimeType . '/') === 0;
185+
}
186+
172187
/**
173188
* Checks if this is an image MIME type.
174189
*
@@ -178,7 +193,7 @@ public static function isValid(string $mimeType): bool
178193
*/
179194
public function isImage(): bool
180195
{
181-
return strpos($this->value, 'image/') === 0;
196+
return $this->hasMimeType('image');
182197
}
183198

184199
/**
@@ -190,7 +205,7 @@ public function isImage(): bool
190205
*/
191206
public function isAudio(): bool
192207
{
193-
return strpos($this->value, 'audio/') === 0;
208+
return $this->hasMimeType('audio');
194209
}
195210

196211
/**
@@ -202,7 +217,7 @@ public function isAudio(): bool
202217
*/
203218
public function isVideo(): bool
204219
{
205-
return strpos($this->value, 'video/') === 0;
220+
return $this->hasMimeType('video');
206221
}
207222

208223
/**
@@ -214,7 +229,7 @@ public function isVideo(): bool
214229
*/
215230
public function isText(): bool
216231
{
217-
return strpos($this->value, 'text/') === 0;
232+
return $this->hasMimeType('text');
218233
}
219234

220235
/**

tests/unit/Files/ValueObjects/MimeTypeTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,16 @@ public function testNormalizesValues(): void
282282
$mimeType = new MimeType('IMAGE/JPEG');
283283
$this->assertEquals('image/jpeg', (string) $mimeType);
284284
}
285+
286+
public function testHasMimeType(): void
287+
{
288+
$mimeType = new MimeType('image/jpeg');
289+
$fontType = new MimeType('font/ttf');
290+
291+
$this->assertTrue($mimeType->hasMimeType('image'));
292+
$this->assertFalse($mimeType->hasMimeType('video'));
293+
294+
$this->assertTrue($fontType->hasMimeType('font'));
295+
$this->assertFalse($fontType->hasMimeType('image'));
296+
}
285297
}

0 commit comments

Comments
 (0)