44
55use DirectoryTree \OpenSearchAdapter \Search \Hit ;
66use DirectoryTree \OpenSearchAdapter \Search \SearchResponse ;
7- use Illuminate \Database \Eloquent \Collection ;
7+ use Illuminate \Database \Eloquent \Collection as EloquentCollection ;
88use Illuminate \Database \Eloquent \Model ;
9+ use Illuminate \Support \Collection as SupportCollection ;
910use Illuminate \Support \LazyCollection ;
1011use Laravel \Scout \Builder ;
1112
@@ -17,15 +18,15 @@ class ModelFactory implements ModelFactoryInterface
1718 /**
1819 * Create an Eloquent collection from an OpenSearch response.
1920 */
20- public function makeFromSearchResponse (SearchResponse $ searchResponse , Builder $ builder ): Collection
21+ public function makeFromSearchResponse (SearchResponse $ searchResponse , Builder $ builder ): EloquentCollection
2122 {
2223 if (! $ searchResponse ->total ()) {
2324 return $ builder ->model ->newCollection ();
2425 }
2526
2627 $ documentIds = $ this ->pluckDocumentIds ($ searchResponse );
2728
28- /** @var Collection $models */
29+ /** @var EloquentCollection $models */
2930 $ models = $ builder ->model ->getScoutModelsByIds ($ builder , $ documentIds );
3031
3132 return $ this ->sortModels ($ this ->filterModels ($ models , $ documentIds ), $ documentIds );
@@ -62,32 +63,28 @@ protected function pluckDocumentIds(SearchResponse $searchResponse): array
6263 }
6364
6465 /**
65- * Remove models that are no longer present in the database.
66- *
67- * @template T
66+ * Sort models into the same order as the OpenSearch response.
6867 *
69- * @param T $models
7068 * @param array<int, string> $documentIds
71- * @return T
7269 */
73- protected function filterModels ( $ models , array $ documentIds )
70+ protected function sortModels ( EloquentCollection | LazyCollection $ models , array $ documentIds ): SupportCollection | EloquentCollection | LazyCollection
7471 {
75- return $ models ->filter (fn (Model $ model ) => in_array ((string ) $ model ->getScoutKey (), $ documentIds , true ))->values ();
72+ $ documentIdPositions = array_flip ($ documentIds );
73+
74+ return $ models ->sortBy (
75+ fn (Model $ model ) => $ documentIdPositions [(string ) $ model ->getScoutKey ()]
76+ )->values ();
7677 }
7778
7879 /**
79- * Sort models into the same order as the OpenSearch response.
80- *
81- * @template T
80+ * Remove models that are no longer present in the database.
8281 *
83- * @param T $models
8482 * @param array<int, string> $documentIds
85- * @return T
8683 */
87- protected function sortModels ( $ models , array $ documentIds )
84+ protected function filterModels ( EloquentCollection | LazyCollection $ models , array $ documentIds ): SupportCollection | EloquentCollection | LazyCollection
8885 {
89- $ documentIdPositions = array_flip ( $ documentIds );
90-
91- return $ models -> sortBy ( fn ( Model $ model ) => $ documentIdPositions [( string ) $ model -> getScoutKey ()] )->values ();
86+ return $ models -> filter (
87+ fn ( Model $ model ) => in_array (( string ) $ model -> getScoutKey (), $ documentIds , true )
88+ )->values ();
9289 }
9390}
0 commit comments