1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This file is part of Scout Extended. |
7
|
|
|
* |
8
|
|
|
* (c) Algolia Team <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Algolia\ScoutExtended\Searchable; |
15
|
|
|
|
16
|
|
|
use function in_array; |
17
|
|
|
use Laravel\Scout\Builder; |
18
|
|
|
use function call_user_func; |
19
|
|
|
use Laravel\Scout\Searchable; |
20
|
|
|
use Illuminate\Support\Collection; |
21
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @internal |
25
|
|
|
*/ |
26
|
|
|
final class ModelsResolver |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Get a set of models from the provided ids. |
30
|
|
|
* |
31
|
|
|
* @param \Laravel\Scout\Builder $builder |
32
|
|
|
* @param string|object $searchable |
33
|
|
|
* @param array $ids |
34
|
|
|
* |
35
|
|
|
* @return \Illuminate\Support\Collection |
36
|
|
|
*/ |
37
|
2 |
|
public function from(Builder $builder, $searchable, array $ids): Collection |
|
|
|
|
38
|
|
|
{ |
39
|
2 |
|
$instances = collect(); |
40
|
|
|
|
41
|
2 |
|
foreach ($ids as $id) { |
42
|
2 |
|
$modelClass = ObjectIdEncrypter::decryptSearchable($id); |
43
|
2 |
|
$model = new $modelClass; |
44
|
2 |
|
$modelKey = ObjectIdEncrypter::decryptSearchableKey($id); |
45
|
|
|
|
46
|
2 |
|
if (in_array(Searchable::class, class_uses_recursive($model), true)) { |
47
|
2 |
|
if (! empty($models = $model->getScoutModelsByIds($builder, [$modelKey]))) { |
48
|
2 |
|
$instances = $instances->merge($models); |
49
|
|
|
} |
50
|
|
|
} else { |
51
|
1 |
|
$query = in_array(SoftDeletes::class, class_uses_recursive($model), |
52
|
1 |
|
true) ? $model->withTrashed() : $model->newQuery(); |
53
|
|
|
|
54
|
1 |
|
if ($builder->queryCallback) { |
55
|
|
|
call_user_func($builder->queryCallback, $query); |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
$scoutKey = method_exists($model, |
59
|
1 |
|
'getScoutKeyName') ? $model->getScoutKeyName() : $model->getQualifiedKeyName(); |
60
|
1 |
|
if ($instance = $query->where($scoutKey, $modelKey)->get()->first()) { |
61
|
2 |
|
$instances->push($instance); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
2 |
|
return $instances; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.