@@ -274,26 +274,26 @@ public function toTexts(): array
274
274
}
275
275
276
276
/**
277
- * Converts all candidates to image files.
277
+ * Converts all candidates to files.
278
278
*
279
279
* @since n.e.x.t
280
280
*
281
- * @return FileInterface[] Array of image files.
281
+ * @return FileInterface[] Array of files.
282
282
*/
283
- public function toImageFiles (): array
283
+ public function toFiles (): array
284
284
{
285
285
$ files = [];
286
286
foreach ($ this ->candidates as $ candidate ) {
287
287
$ message = $ candidate ->getMessage ();
288
288
foreach ($ message ->getParts () as $ part ) {
289
289
$ inlineFile = $ part ->getInlineFile ();
290
- if ($ inlineFile !== null && $ inlineFile -> getMimeType ()-> isImage () ) {
290
+ if ($ inlineFile !== null ) {
291
291
$ files [] = $ inlineFile ;
292
292
break ;
293
293
}
294
294
295
295
$ remoteFile = $ part ->getRemoteFile ();
296
- if ($ remoteFile !== null && $ remoteFile -> getMimeType ()-> isImage () ) {
296
+ if ($ remoteFile !== null ) {
297
297
$ files [] = $ remoteFile ;
298
298
break ;
299
299
}
@@ -302,6 +302,21 @@ public function toImageFiles(): array
302
302
return $ files ;
303
303
}
304
304
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
+
305
320
/**
306
321
* Converts all candidates to audio files.
307
322
*
@@ -311,24 +326,10 @@ public function toImageFiles(): array
311
326
*/
312
327
public function toAudioFiles (): array
313
328
{
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
+ );
332
333
}
333
334
334
335
/**
@@ -340,24 +341,10 @@ public function toAudioFiles(): array
340
341
*/
341
342
public function toVideoFiles (): array
342
343
{
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
+ );
361
348
}
362
349
363
350
/**
0 commit comments