27
27
28
28
use function array_column ;
29
29
use function array_flip ;
30
- use function array_key_exists ;
31
30
use function array_map ;
32
31
use function array_merge ;
33
32
use function assert ;
44
43
use function method_exists ;
45
44
use function sleep ;
46
45
use function sprintf ;
47
- use function substr ;
48
46
use function time ;
49
47
50
48
/**
@@ -313,40 +311,67 @@ public function mapIds($results): Collection
313
311
*
314
312
* @see Engine::map()
315
313
*
316
- * @param array $results
317
- * @param Model $model
314
+ * @param Builder $builder
315
+ * @param array|Cursor $results
316
+ * @param Model $model
317
+ *
318
+ * @return Collection
318
319
*/
319
320
#[Override]
320
321
public function map (Builder $ builder , $ results , $ model ): Collection
321
322
{
322
- assert (is_array ($ results ), new TypeError (sprintf ('Argument #2 ($results) must be of type array, %s given ' , get_debug_type ($ results ))));
323
- assert ($ model instanceof Model, new TypeError (sprintf ('Argument #3 ($model) must be of type %s, %s given ' , Model::class, get_debug_type ($ model ))));
323
+ return $ this ->doMap ($ builder , $ results , $ model , false );
324
+ }
325
+
326
+ /**
327
+ * Map the given results to instances of the given model via a lazy collection.
328
+ *
329
+ * @see Engine::lazyMap()
330
+ *
331
+ * @param Builder $builder
332
+ * @param array|Cursor $results
333
+ * @param Model $model
334
+ *
335
+ * @return LazyCollection
336
+ */
337
+ #[Override]
338
+ public function lazyMap (Builder $ builder , $ results , $ model ): LazyCollection
339
+ {
340
+ return $ this ->doMap ($ builder , $ results , $ model , true );
341
+ }
324
342
343
+ /** @return ($lazy is true ? LazyCollection : Collection)<mixed> */
344
+ private function doMap (Builder $ builder , array $ results , Model $ model , bool $ lazy ): Collection |LazyCollection
345
+ {
325
346
if (! $ results ) {
326
- return $ model ->newCollection ();
347
+ $ collection = $ model ->newCollection ();
348
+
349
+ return $ lazy ? LazyCollection::make ($ collection ) : $ collection ;
327
350
}
328
351
329
352
$ objectIds = array_column ($ results , '_id ' );
330
353
$ objectIdPositions = array_flip ($ objectIds );
331
354
332
- return $ model ->getScoutModelsByIds (
333
- $ builder ,
334
- $ objectIds ,
335
- )->filter (function ($ model ) use ($ objectIdPositions ) {
336
- return array_key_exists ($ model ->getScoutKey (), $ objectIdPositions );
337
- })->map (function ($ model ) use ($ results , $ objectIdPositions ) {
338
- $ result = $ results [$ objectIdPositions [$ model ->getScoutKey ()]] ?? [];
339
-
340
- foreach ($ result as $ key => $ value ) {
341
- if ($ key [0 ] === '_ ' ) {
342
- $ model ->withScoutMetadata ($ key , $ value );
355
+ return $ model ->queryScoutModelsByIds ($ builder , $ objectIds )
356
+ ->{$ lazy ? 'cursor ' : 'get ' }()
357
+ ->filter (function ($ model ) use ($ objectIds ) {
358
+ return in_array ($ model ->getScoutKey (), $ objectIds );
359
+ })
360
+ ->map (function ($ model ) use ($ results , $ objectIdPositions ) {
361
+ $ result = $ results [$ objectIdPositions [$ model ->getScoutKey ()]] ?? [];
362
+
363
+ foreach ($ result as $ key => $ value ) {
364
+ if ($ key [0 ] === '_ ' && $ key !== '_id ' ) {
365
+ $ model ->withScoutMetadata ($ key , $ value );
366
+ }
343
367
}
344
- }
345
368
346
- return $ model ;
347
- })->sortBy (function ($ model ) use ($ objectIdPositions ) {
348
- return $ objectIdPositions [$ model ->getScoutKey ()];
349
- })->values ();
369
+ return $ model ;
370
+ })
371
+ ->sortBy (function ($ model ) use ($ objectIdPositions ) {
372
+ return $ objectIdPositions [$ model ->getScoutKey ()];
373
+ })
374
+ ->values ();
350
375
}
351
376
352
377
/**
@@ -364,7 +389,7 @@ public function getTotalCount($results): int
364
389
return 0 ;
365
390
}
366
391
367
- return $ results [0 ][ ' __count ' ] ;
392
+ return $ results [0 ]-> __count ;
368
393
}
369
394
370
395
/**
@@ -384,50 +409,6 @@ public function flush($model): void
384
409
$ collection ->deleteMany ([]);
385
410
}
386
411
387
- /**
388
- * Map the given results to instances of the given model via a lazy collection.
389
- *
390
- * @see Engine::lazyMap()
391
- *
392
- * @param Builder $builder
393
- * @param array|Cursor $results
394
- * @param Model $model
395
- *
396
- * @return LazyCollection
397
- */
398
- #[Override]
399
- public function lazyMap (Builder $ builder , $ results , $ model ): LazyCollection
400
- {
401
- assert ($ results instanceof Cursor || is_array ($ results ), new TypeError (sprintf ('Argument #2 ($results) must be of type %s|array, %s given ' , Cursor::class, get_debug_type ($ results ))));
402
- assert ($ model instanceof Model, new TypeError (sprintf ('Argument #3 ($model) must be of type %s, %s given ' , Model::class, get_debug_type ($ model ))));
403
-
404
- if (! $ results ) {
405
- return LazyCollection::make ($ model ->newCollection ());
406
- }
407
-
408
- $ objectIds = array_column ($ results , '_id ' );
409
- $ objectIdPositions = array_flip ($ objectIds );
410
-
411
- return $ model ->queryScoutModelsByIds (
412
- $ builder ,
413
- $ objectIds ,
414
- )->cursor ()->filter (function ($ model ) use ($ objectIds ) {
415
- return in_array ($ model ->getScoutKey (), $ objectIds );
416
- })->map (function ($ model ) use ($ results , $ objectIdPositions ) {
417
- $ result = $ results [$ objectIdPositions [$ model ->getScoutKey ()]] ?? [];
418
-
419
- foreach ($ result as $ key => $ value ) {
420
- if (substr ($ key , 0 , 1 ) === '_ ' ) {
421
- $ model ->withScoutMetadata ($ key , $ value );
422
- }
423
- }
424
-
425
- return $ model ;
426
- })->sortBy (function ($ model ) use ($ objectIdPositions ) {
427
- return $ objectIdPositions [$ model ->getScoutKey ()];
428
- })->values ();
429
- }
430
-
431
412
/**
432
413
* Create the MongoDB Atlas Search index.
433
414
*
@@ -475,7 +456,7 @@ public function deleteIndex($name): void
475
456
{
476
457
assert (is_string ($ name ), new TypeError (sprintf ('Argument #1 ($name) must be of type string, %s given ' , get_debug_type ($ name ))));
477
458
478
- $ this ->database ->selectCollection ($ name)-> drop ( );
459
+ $ this ->database ->dropCollection ($ name );
479
460
}
480
461
481
462
/** Get the MongoDB collection used to search for the provided model */
0 commit comments