Skip to content

Commit 4c25a86

Browse files
committed
feat: adds file type checking
1 parent cbd489d commit 4c25a86

File tree

3 files changed

+120
-16
lines changed

3 files changed

+120
-16
lines changed

src/Files/Contracts/FileInterface.php

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,54 @@
1717
interface FileInterface
1818
{
1919
/**
20-
* Gets the MIME type of the file.
20+
* Gets the MIME type of the file as a string.
2121
*
2222
* @since n.e.x.t
23-
* @return MimeType The MIME type (e.g., 'image/png', 'audio/mp3').
23+
* @return string The MIME type string (e.g., 'image/png', 'audio/mp3').
2424
*/
25-
public function getMimeType(): MimeType;
25+
public function getMimeType(): string;
26+
27+
/**
28+
* Gets the MIME type object.
29+
*
30+
* @since n.e.x.t
31+
* @return MimeType The MIME type object.
32+
*/
33+
public function getMimeTypeObject(): MimeType;
34+
35+
/**
36+
* Checks if the file is a video.
37+
*
38+
* @since n.e.x.t
39+
*
40+
* @return bool True if the file is a video.
41+
*/
42+
public function isVideo(): bool;
43+
44+
/**
45+
* Checks if the file is an image.
46+
*
47+
* @since n.e.x.t
48+
*
49+
* @return bool True if the file is an image.
50+
*/
51+
public function isImage(): bool;
52+
53+
/**
54+
* Checks if the file is audio.
55+
*
56+
* @since n.e.x.t
57+
*
58+
* @return bool True if the file is audio.
59+
*/
60+
public function isAudio(): bool;
61+
62+
/**
63+
* Checks if the file is text.
64+
*
65+
* @since n.e.x.t
66+
*
67+
* @return bool True if the file is text.
68+
*/
69+
public function isText(): bool;
2670
}

src/Files/Traits/HasMimeType.php

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* This trait can be used by any class that needs to store and retrieve
1313
* a MIME type property.
1414
*
15-
* @since 1.0.0
15+
* @since n.e.x.t
1616
*/
1717
trait HasMimeType
1818
{
@@ -24,23 +24,83 @@ trait HasMimeType
2424
protected MimeType $mimeType;
2525

2626
/**
27-
* Gets the MIME type of the file.
27+
* Gets the MIME type of the file as a string.
2828
*
29-
* @return MimeType The MIME type.
29+
* @since n.e.x.t
30+
*
31+
* @return string The MIME type string value.
32+
*/
33+
public function getMimeType(): string
34+
{
35+
return (string) $this->mimeType;
36+
}
37+
38+
/**
39+
* Gets the MIME type object.
40+
*
41+
* @since n.e.x.t
3042
*
31-
* @since 1.0.0
43+
* @return MimeType The MIME type object.
3244
*/
33-
public function getMimeType(): MimeType
45+
public function getMimeTypeObject(): MimeType
3446
{
3547
return $this->mimeType;
3648
}
3749

3850
/**
39-
* Gets the JSON schema for the MIME type property.
51+
* Checks if the file is a video.
4052
*
41-
* @return array{type: string, description: string, pattern: string} The JSON schema for the mimeType property.
53+
* @since n.e.x.t
54+
*
55+
* @return bool True if the file is a video.
56+
*/
57+
public function isVideo(): bool
58+
{
59+
return $this->mimeType->isVideo();
60+
}
61+
62+
/**
63+
* Checks if the file is an image.
4264
*
4365
* @since n.e.x.t
66+
*
67+
* @return bool True if the file is an image.
68+
*/
69+
public function isImage(): bool
70+
{
71+
return $this->mimeType->isImage();
72+
}
73+
74+
/**
75+
* Checks if the file is audio.
76+
*
77+
* @since n.e.x.t
78+
*
79+
* @return bool True if the file is audio.
80+
*/
81+
public function isAudio(): bool
82+
{
83+
return $this->mimeType->isAudio();
84+
}
85+
86+
/**
87+
* Checks if the file is text.
88+
*
89+
* @since n.e.x.t
90+
*
91+
* @return bool True if the file is text.
92+
*/
93+
public function isText(): bool
94+
{
95+
return $this->mimeType->isText();
96+
}
97+
98+
/**
99+
* Gets the JSON schema for the MIME type property.
100+
*
101+
* @since n.e.x.t
102+
*
103+
* @return array{type: string, description: string, pattern: string} The JSON schema for the mimeType property.
44104
*/
45105
protected static function getMimeTypePropertySchema(): array
46106
{

src/Results/DTO/GenerativeAiResult.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public function toImageFile(): FileInterface
187187
{
188188
$file = $this->toFile();
189189

190-
if (!$file->getMimeType()->isImage()) {
190+
if (!$file->isImage()) {
191191
throw new \RuntimeException(
192192
sprintf('File is not an image. MIME type: %s', $file->getMimeType())
193193
);
@@ -208,7 +208,7 @@ public function toAudioFile(): FileInterface
208208
{
209209
$file = $this->toFile();
210210

211-
if (!$file->getMimeType()->isAudio()) {
211+
if (!$file->isAudio()) {
212212
throw new \RuntimeException(
213213
sprintf('File is not an audio file. MIME type: %s', $file->getMimeType())
214214
);
@@ -229,7 +229,7 @@ public function toVideoFile(): FileInterface
229229
{
230230
$file = $this->toFile();
231231

232-
if (!$file->getMimeType()->isVideo()) {
232+
if (!$file->isVideo()) {
233233
throw new \RuntimeException(
234234
sprintf('File is not a video file. MIME type: %s', $file->getMimeType())
235235
);
@@ -313,7 +313,7 @@ public function toImageFiles(): array
313313
{
314314
return array_filter(
315315
$this->toFiles(),
316-
fn(FileInterface $file) => $file->getMimeType()->isImage()
316+
fn(FileInterface $file) => $file->isImage()
317317
);
318318
}
319319

@@ -328,7 +328,7 @@ public function toAudioFiles(): array
328328
{
329329
return array_filter(
330330
$this->toFiles(),
331-
fn(FileInterface $file) => $file->getMimeType()->isAudio()
331+
fn(FileInterface $file) => $file->isAudio()
332332
);
333333
}
334334

@@ -343,7 +343,7 @@ public function toVideoFiles(): array
343343
{
344344
return array_filter(
345345
$this->toFiles(),
346-
fn(FileInterface $file) => $file->getMimeType()->isVideo()
346+
fn(FileInterface $file) => $file->isVideo()
347347
);
348348
}
349349

0 commit comments

Comments
 (0)