@@ 197-226 (lines=30) @@ | ||
194 | * |
|
195 | * @return Collection |
|
196 | */ |
|
197 | public function map(Builder $builder, $results, $model) |
|
198 | { |
|
199 | if (empty($results['ids'])) { |
|
200 | return Collection::make(); |
|
201 | } |
|
202 | ||
203 | $keys = collect($results['ids'])->values()->all(); |
|
204 | ||
205 | $builder = $this->getBuilder($model); |
|
206 | ||
207 | if ($this->builder->queryCallback) { |
|
208 | call_user_func($this->builder->queryCallback, $builder); |
|
209 | } |
|
210 | ||
211 | $models = $builder->whereIn( |
|
212 | $model->getQualifiedKeyName(), $keys |
|
213 | )->get()->keyBy($model->getKeyName()); |
|
214 | ||
215 | // sort models by user choice |
|
216 | if (!empty($this->builder->orders)) { |
|
217 | return $models->values(); |
|
218 | } |
|
219 | ||
220 | // sort models by tnt search result set |
|
221 | return $model->newCollection($results['ids'])->map(function ($hit) use ($models) { |
|
222 | if (isset($models[$hit])) { |
|
223 | return $models[$hit]; |
|
224 | } |
|
225 | })->filter()->values(); |
|
226 | } |
|
227 | ||
228 | /** |
|
229 | * Map the given results to instances of the given model via a lazy collection. |
|
@@ 236-265 (lines=30) @@ | ||
233 | * |
|
234 | * @return LazyCollection |
|
235 | */ |
|
236 | public function lazyMap(Builder $builder, $results, $model) |
|
237 | { |
|
238 | if (empty($results['ids'])) { |
|
239 | return LazyCollection::make(); |
|
240 | } |
|
241 | ||
242 | $keys = collect($results['ids'])->values()->all(); |
|
243 | ||
244 | $builder = $this->getBuilder($model); |
|
245 | ||
246 | if ($this->builder->queryCallback) { |
|
247 | call_user_func($this->builder->queryCallback, $builder); |
|
248 | } |
|
249 | ||
250 | $models = $builder->whereIn( |
|
251 | $model->getQualifiedKeyName(), $keys |
|
252 | )->get()->keyBy($model->getKeyName()); |
|
253 | ||
254 | // sort models by user choice |
|
255 | if (!empty($this->builder->orders)) { |
|
256 | return $models->values(); |
|
257 | } |
|
258 | ||
259 | // sort models by tnt search result set |
|
260 | return $model->newCollection($results['ids'])->map(function ($hit) use ($models) { |
|
261 | if (isset($models[$hit])) { |
|
262 | return $models[$hit]; |
|
263 | } |
|
264 | })->filter()->values(); |
|
265 | } |
|
266 | ||
267 | /** |
|
268 | * Return query builder either from given constraints, or as |