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\Database\Eloquent\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\Database\Eloquent\Collection |
36
|
|
|
*/ |
37
|
3 |
|
public function from(Builder $builder, $searchable, array $ids): Collection |
38
|
|
|
{ |
39
|
3 |
|
$instances = collect(); |
40
|
|
|
|
41
|
3 |
|
$models = []; |
42
|
3 |
|
foreach ($ids as $id) { |
43
|
3 |
|
$modelClass = ObjectIdEncrypter::decryptSearchable($id); |
44
|
3 |
|
$modelKey = ObjectIdEncrypter::decryptSearchableKey($id); |
45
|
3 |
|
if (! array_key_exists($modelClass, $models)) { |
46
|
3 |
|
$models[$modelClass] = []; |
47
|
|
|
} |
48
|
|
|
|
49
|
3 |
|
$models[$modelClass][] = $modelKey; |
50
|
|
|
} |
51
|
|
|
|
52
|
3 |
|
foreach ($models as $modelClass => $modelKeys) { |
53
|
3 |
|
$model = new $modelClass; |
54
|
|
|
|
55
|
3 |
|
if (in_array(Searchable::class, class_uses_recursive($model), true)) { |
56
|
3 |
|
if (! empty($models = $model->getScoutModelsByIds($builder, $modelKeys))) { |
57
|
3 |
|
$instances = $instances->merge($models); |
58
|
|
|
} |
59
|
|
|
} else { |
60
|
1 |
|
$query = in_array(SoftDeletes::class, class_uses_recursive($model), |
61
|
1 |
|
true) ? $model->withTrashed() : $model->newQuery(); |
62
|
|
|
|
63
|
1 |
|
if ($builder->queryCallback) { |
64
|
|
|
call_user_func($builder->queryCallback, $query); |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
$scoutKey = method_exists($model, |
68
|
1 |
|
'getScoutKeyName') ? $model->getScoutKeyName() : $model->getQualifiedKeyName(); |
69
|
1 |
|
if ($models = $query->whereIn($scoutKey, $modelKeys)->get()) { |
70
|
3 |
|
$instances = $instances->merge($models); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
3 |
|
$result = $searchable->newCollection(); |
76
|
|
|
|
77
|
3 |
|
foreach ($ids as $id) { |
78
|
3 |
|
foreach ($instances as $instance) { |
79
|
3 |
|
if (ObjectIdEncrypter::encrypt($instance) === ObjectIdEncrypter::withoutPart($id)) { |
80
|
3 |
|
$result->push($instance); |
81
|
3 |
|
break; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
3 |
|
return $result; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|