|
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\Settings; |
|
15
|
|
|
|
|
16
|
|
|
use function in_array; |
|
17
|
|
|
use Algolia\AlgoliaSearch\SearchIndex; |
|
18
|
|
|
use Illuminate\Database\QueryException; |
|
19
|
|
|
use Algolia\ScoutExtended\Searchable\Aggregator; |
|
20
|
|
|
use Algolia\ScoutExtended\Exceptions\ModelNotFoundException; |
|
21
|
|
|
use Algolia\ScoutExtended\Repositories\RemoteSettingsRepository; |
|
22
|
|
|
use Algolia\ScoutExtended\Settings\SettingAttribute\UnsearcheableAttribute; |
|
23
|
|
|
use Algolia\ScoutExtended\Settings\SettingAttribute\FacetingAttribute; |
|
24
|
|
|
use Algolia\ScoutExtended\Settings\SettingAttribute\DisableTypoToleranceAttribute; |
|
25
|
|
|
use Algolia\ScoutExtended\Settings\SettingAttribute\UnretrievableAttribute; |
|
26
|
|
|
use Algolia\ScoutExtended\Settings\SettingAttribute\CustomRankingAttribute; |
|
27
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException as BaseModelNotFoundException; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @internal |
|
31
|
|
|
*/ |
|
32
|
|
|
final class LocalFactory |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var \Algolia\ScoutExtended\Repositories\RemoteSettingsRepository |
|
36
|
|
|
*/ |
|
37
|
|
|
private $remoteRepository; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var string[] |
|
41
|
|
|
*/ |
|
42
|
|
|
private static $settings = [ |
|
43
|
|
|
'searchableAttributes' => UnsearcheableAttribute::class, |
|
44
|
|
|
'attributesForFaceting' => FacetingAttribute::class, |
|
45
|
|
|
'customRanking' => CustomRankingAttribute::class, |
|
46
|
|
|
'disableTypoToleranceOnAttributes' => DisableTypoToleranceAttribute::class, |
|
47
|
|
|
'unretrievableAttributes' => UnretrievableAttribute::class, |
|
48
|
|
|
]; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* SettingsFactory constructor. |
|
52
|
|
|
* |
|
53
|
|
|
* @param \Algolia\ScoutExtended\Repositories\RemoteSettingsRepository $remoteRepository |
|
54
|
|
|
* |
|
55
|
|
|
* @return void |
|
56
|
|
|
*/ |
|
57
|
4 |
|
public function __construct(RemoteSettingsRepository $remoteRepository) |
|
58
|
|
|
{ |
|
59
|
4 |
|
$this->remoteRepository = $remoteRepository; |
|
60
|
4 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Creates settings for the given model. |
|
64
|
|
|
* |
|
65
|
|
|
* @param \Algolia\AlgoliaSearch\SearchIndex $index |
|
66
|
|
|
* @param string $model |
|
67
|
|
|
* |
|
68
|
|
|
* @return \Algolia\ScoutExtended\Settings\Settings |
|
69
|
|
|
*/ |
|
70
|
4 |
|
public function create(SearchIndex $index, string $model): Settings |
|
71
|
|
|
{ |
|
72
|
4 |
|
$attributes = $this->getAttributes($model); |
|
73
|
3 |
|
$attributesArray = []; |
|
74
|
3 |
|
foreach (self::$settings as $key => $value) { |
|
75
|
3 |
|
$attributesArray[$key] = []; |
|
76
|
|
|
} |
|
77
|
3 |
|
foreach ($attributes as $key => $value) { |
|
78
|
3 |
|
$key = (string) $key; |
|
79
|
3 |
|
foreach (self::$settings as $setting => $class) { |
|
80
|
3 |
|
$attributesArray[$setting] = $class::exist($key, $value, $attributesArray[$setting]); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
3 |
|
foreach ($attributesArray as $key => $value) { |
|
84
|
3 |
|
$detectedSettings[$key] = ! empty($value) ? $value : null; |
|
85
|
|
|
} |
|
86
|
3 |
|
$detectedSettings['queryLanguages'] = array_unique([config('app.locale'), config('app.fallback_locale')]); |
|
87
|
|
|
|
|
88
|
3 |
|
$settings = array_merge($this->remoteRepository->find($index)->compiled(), $detectedSettings); |
|
|
|
|
|
|
89
|
|
|
|
|
90
|
3 |
|
return new Settings($settings, $this->remoteRepository->defaults()); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Tries to get attributes from the searchable class. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $searchable |
|
97
|
|
|
* |
|
98
|
|
|
* @return array |
|
99
|
|
|
*/ |
|
100
|
4 |
|
private function getAttributes(string $searchable): array |
|
101
|
|
|
{ |
|
102
|
4 |
|
$attributes = []; |
|
103
|
|
|
|
|
104
|
4 |
|
if (in_array(Aggregator::class, class_parents($searchable), true)) { |
|
105
|
|
|
foreach (($instance = new $searchable)->getModels() as $model) { |
|
|
|
|
|
|
106
|
|
|
$attributes = array_merge($attributes, $this->getAttributes($model)); |
|
107
|
|
|
} |
|
108
|
|
|
} else { |
|
109
|
4 |
|
$instance = null; |
|
110
|
|
|
|
|
111
|
|
|
try { |
|
112
|
4 |
|
$instance = $searchable::firstOrFail(); |
|
113
|
1 |
|
} catch (QueryException | BaseModelNotFoundException $e) { |
|
114
|
1 |
|
throw tap(new ModelNotFoundException())->setModel($searchable); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
3 |
|
$attributes = method_exists($instance, 'toSearchableArray') ? $instance->toSearchableArray() : |
|
118
|
3 |
|
$instance->toArray(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
3 |
|
return $attributes; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|