Skip to content

Commit 0540ea8

Browse files
committed
feat: adds toFiles() method
1 parent c5b632e commit 0540ea8

File tree

1 file changed

+28
-41
lines changed

1 file changed

+28
-41
lines changed

src/Results/DTO/GenerativeAiResult.php

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -274,26 +274,26 @@ public function toTexts(): array
274274
}
275275

276276
/**
277-
* Converts all candidates to image files.
277+
* Converts all candidates to files.
278278
*
279279
* @since n.e.x.t
280280
*
281-
* @return FileInterface[] Array of image files.
281+
* @return FileInterface[] Array of files.
282282
*/
283-
public function toImageFiles(): array
283+
public function toFiles(): array
284284
{
285285
$files = [];
286286
foreach ($this->candidates as $candidate) {
287287
$message = $candidate->getMessage();
288288
foreach ($message->getParts() as $part) {
289289
$inlineFile = $part->getInlineFile();
290-
if ($inlineFile !== null && $inlineFile->getMimeType()->isImage()) {
290+
if ($inlineFile !== null) {
291291
$files[] = $inlineFile;
292292
break;
293293
}
294294

295295
$remoteFile = $part->getRemoteFile();
296-
if ($remoteFile !== null && $remoteFile->getMimeType()->isImage()) {
296+
if ($remoteFile !== null) {
297297
$files[] = $remoteFile;
298298
break;
299299
}
@@ -302,6 +302,21 @@ public function toImageFiles(): array
302302
return $files;
303303
}
304304

305+
/**
306+
* Converts all candidates to image files.
307+
*
308+
* @since n.e.x.t
309+
*
310+
* @return FileInterface[] Array of image files.
311+
*/
312+
public function toImageFiles(): array
313+
{
314+
return array_filter(
315+
$this->toFiles(),
316+
fn(FileInterface $file) => $file->getMimeType()->isImage()
317+
);
318+
}
319+
305320
/**
306321
* Converts all candidates to audio files.
307322
*
@@ -311,24 +326,10 @@ public function toImageFiles(): array
311326
*/
312327
public function toAudioFiles(): array
313328
{
314-
$files = [];
315-
foreach ($this->candidates as $candidate) {
316-
$message = $candidate->getMessage();
317-
foreach ($message->getParts() as $part) {
318-
$inlineFile = $part->getInlineFile();
319-
if ($inlineFile !== null && $inlineFile->getMimeType()->isAudio()) {
320-
$files[] = $inlineFile;
321-
break;
322-
}
323-
324-
$remoteFile = $part->getRemoteFile();
325-
if ($remoteFile !== null && $remoteFile->getMimeType()->isAudio()) {
326-
$files[] = $remoteFile;
327-
break;
328-
}
329-
}
330-
}
331-
return $files;
329+
return array_filter(
330+
$this->toFiles(),
331+
fn(FileInterface $file) => $file->getMimeType()->isAudio()
332+
);
332333
}
333334

334335
/**
@@ -340,24 +341,10 @@ public function toAudioFiles(): array
340341
*/
341342
public function toVideoFiles(): array
342343
{
343-
$files = [];
344-
foreach ($this->candidates as $candidate) {
345-
$message = $candidate->getMessage();
346-
foreach ($message->getParts() as $part) {
347-
$inlineFile = $part->getInlineFile();
348-
if ($inlineFile !== null && $inlineFile->getMimeType()->isVideo()) {
349-
$files[] = $inlineFile;
350-
break;
351-
}
352-
353-
$remoteFile = $part->getRemoteFile();
354-
if ($remoteFile !== null && $remoteFile->getMimeType()->isVideo()) {
355-
$files[] = $remoteFile;
356-
break;
357-
}
358-
}
359-
}
360-
return $files;
344+
return array_filter(
345+
$this->toFiles(),
346+
fn(FileInterface $file) => $file->getMimeType()->isVideo()
347+
);
361348
}
362349

363350
/**

0 commit comments

Comments
 (0)