|
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 Illuminate\Support\Arr; |
|
19
|
|
|
use function call_user_func; |
|
20
|
|
|
use Laravel\Scout\Searchable; |
|
21
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
22
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @internal |
|
26
|
|
|
*/ |
|
27
|
|
|
final class ModelsResolver |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var string[] |
|
31
|
|
|
*/ |
|
32
|
|
|
private static $metadata = [ |
|
33
|
|
|
'_highlightResult', |
|
34
|
|
|
'_rankingInfo' |
|
35
|
|
|
]; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Get a set of models from the provided results. |
|
39
|
|
|
* |
|
40
|
|
|
* @param \Laravel\Scout\Builder $builder |
|
41
|
|
|
* @param string|object $searchable |
|
42
|
|
|
* @param array $results |
|
43
|
|
|
* |
|
44
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
|
45
|
|
|
*/ |
|
46
|
4 |
|
public function from(Builder $builder, $searchable, array $results): Collection |
|
47
|
|
|
{ |
|
48
|
4 |
|
$instances = collect(); |
|
49
|
4 |
|
$hits = collect($results['hits'])->keyBy('objectID'); |
|
50
|
|
|
|
|
51
|
4 |
|
$models = []; |
|
52
|
4 |
|
foreach ($hits->keys() as $id) { |
|
53
|
4 |
|
$modelClass = ObjectIdEncrypter::decryptSearchable($id); |
|
54
|
3 |
|
$modelKey = ObjectIdEncrypter::decryptSearchableKey($id); |
|
55
|
3 |
|
if (! array_key_exists($modelClass, $models)) { |
|
56
|
3 |
|
$models[$modelClass] = []; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
3 |
|
$models[$modelClass][] = $modelKey; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
3 |
|
foreach ($models as $modelClass => $modelKeys) { |
|
63
|
3 |
|
$model = new $modelClass; |
|
64
|
|
|
|
|
65
|
3 |
|
if (in_array(Searchable::class, class_uses_recursive($model), true)) { |
|
66
|
3 |
|
if (! empty($models = $model->getScoutModelsByIds($builder, $modelKeys))) { |
|
67
|
3 |
|
$instances = $instances->merge($models); |
|
68
|
|
|
} |
|
69
|
|
|
} else { |
|
70
|
1 |
|
$query = in_array(SoftDeletes::class, class_uses_recursive($model), |
|
71
|
1 |
|
true) ? $model->withTrashed() : $model->newQuery(); |
|
72
|
|
|
|
|
73
|
1 |
|
if ($builder->queryCallback) { |
|
74
|
|
|
call_user_func($builder->queryCallback, $query); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
$scoutKey = method_exists($model, |
|
78
|
1 |
|
'getScoutKeyName') ? $model->getScoutKeyName() : $model->getQualifiedKeyName(); |
|
79
|
1 |
|
if ($models = $query->whereIn($scoutKey, $modelKeys)->get()) { |
|
80
|
3 |
|
$instances = $instances->merge($models); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
3 |
|
$result = $searchable->newCollection(); |
|
86
|
|
|
|
|
87
|
3 |
|
foreach ($hits as $id => $hit) { |
|
88
|
3 |
|
foreach ($instances as $instance) { |
|
89
|
3 |
|
if (ObjectIdEncrypter::encrypt($instance) === ObjectIdEncrypter::withoutPart($id)) { |
|
90
|
|
|
|
|
91
|
3 |
|
foreach (Arr::only($hit, self::$metadata) as $metadataKey => $metadataValue) { |
|
92
|
|
|
$instance->withScoutMetadata($metadataKey, $metadataValue); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
3 |
|
$result->push($instance); |
|
96
|
3 |
|
break; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
3 |
|
return $result; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|