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
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @internal |
20
|
|
|
*/ |
21
|
|
|
final class Settings |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $settings; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string[] |
30
|
|
|
*/ |
31
|
|
|
private static $ignore = [ |
32
|
|
|
'version', |
33
|
|
|
'userData', |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The default options. |
38
|
|
|
* |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
private $defaults; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Settings constructor. |
45
|
|
|
* |
46
|
|
|
* @param array $settings |
47
|
|
|
* @param array $defaults |
48
|
|
|
* |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
12 |
|
public function __construct(array $settings, array $defaults) |
52
|
|
|
{ |
53
|
12 |
|
$this->settings = $settings; |
54
|
12 |
|
$this->defaults = $defaults; |
55
|
12 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get all of the items in the settings. |
59
|
|
|
* |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
12 |
|
public function all(): array |
63
|
|
|
{ |
64
|
12 |
|
$settings = $this->settings; |
65
|
12 |
|
foreach (Compiler::getViewVariables() as $key) { |
66
|
12 |
|
if (array_key_exists($key, $this->settings)) { |
67
|
11 |
|
$settings[$key] = $this->settings[$key]; |
68
|
3 |
|
} elseif (array_key_exists($key, $this->defaults)) { |
69
|
|
|
$settings[$key] = $this->defaults[$key]; |
70
|
|
|
} else { |
71
|
12 |
|
$settings[$key] = null; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return array_filter($settings, function ($value, $setting) { |
76
|
12 |
|
return ! in_array($setting, self::$ignore, true); |
77
|
12 |
|
}, ARRAY_FILTER_USE_BOTH); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the changed items in the settings. |
82
|
|
|
* |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
12 |
|
public function changed(): array |
86
|
|
|
{ |
87
|
|
|
return array_filter($this->all(), function ($value, $setting) { |
88
|
12 |
|
return ! array_key_exists($setting, $this->defaults) || $value !== $this->defaults[$setting]; |
89
|
12 |
|
}, ARRAY_FILTER_USE_BOTH); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get the changed items in the settings. |
94
|
|
|
* |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
12 |
|
public function compiled(): array |
98
|
|
|
{ |
99
|
12 |
|
$viewVariables = Compiler::getViewVariables(); |
100
|
12 |
|
$changed = $this->changed(); |
101
|
|
|
|
102
|
|
|
$compiled = array_filter($this->all(), function ($value, $setting) use ($viewVariables, $changed) { |
103
|
12 |
|
return in_array($setting, $viewVariables, true) || array_key_exists($setting, $changed); |
104
|
12 |
|
}, ARRAY_FILTER_USE_BOTH); |
105
|
|
|
|
106
|
12 |
|
ksort($compiled); |
107
|
|
|
|
108
|
12 |
|
return $compiled; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get the hash. |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
public function previousHash(): string |
117
|
|
|
{ |
118
|
|
|
return $this->settings['userData'] ?? ''; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|