1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Giftd\Editor; |
4
|
|
|
|
5
|
|
|
use Giftd\Editor\Contracts\HasSettings; |
6
|
|
|
|
7
|
|
|
class SettingsManager |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Resource with settings. |
11
|
|
|
* |
12
|
|
|
* @var HasSettings |
13
|
|
|
*/ |
14
|
|
|
protected $resource; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Replaced settings. |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $replaced = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* SettingsManager constructor. |
25
|
|
|
* |
26
|
|
|
* @param HasSettings $resource |
27
|
|
|
*/ |
28
|
|
|
public function __construct(HasSettings $resource) |
29
|
|
|
{ |
30
|
|
|
$this->resource = $resource; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get the resource. |
35
|
|
|
* |
36
|
|
|
* @return HasSettings |
37
|
|
|
*/ |
38
|
|
|
public function getResource() |
39
|
|
|
{ |
40
|
|
|
return $this->resource; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Returns single setting value (or default value if setting was not found or is NULL). |
45
|
|
|
* |
46
|
|
|
* @param string $setting |
47
|
|
|
* @param null $defaultValue |
48
|
|
|
* |
49
|
|
|
* @return mixed |
50
|
|
|
*/ |
51
|
|
|
public function get(string $setting, $defaultValue = null) |
52
|
|
|
{ |
53
|
|
|
$values = $this->getMany([$setting], [$setting => $defaultValue]); |
54
|
|
|
|
55
|
|
|
return $values[$setting] ?? $defaultValue; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns multiple settings values (or replaces missing settings with default values). |
60
|
|
|
* |
61
|
|
|
* @param array $settings |
62
|
|
|
* @param array $defaultValues |
63
|
|
|
* |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
public function getMany(array $settings, array $defaultValues = []) |
67
|
|
|
{ |
68
|
|
|
$values = $this->resource->getResourceSettingsValues($settings); |
69
|
|
|
$replaced = $this->getReplaced($settings); |
70
|
|
|
|
71
|
|
|
$result = array_merge($values, $replaced); |
72
|
|
|
|
73
|
|
|
if (count($result) === count($settings)) { |
74
|
|
|
return $result; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $result + $defaultValues; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns string value and performs placeholders replacement. |
82
|
|
|
* |
83
|
|
|
* @param string $setting |
84
|
|
|
* @param array $placeholders |
85
|
|
|
* |
86
|
|
|
* @throws \InvalidArgumentException |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function getWithPlaceholders(string $setting, array $placeholders = []) |
91
|
|
|
{ |
92
|
|
|
$value = $this->get($setting); |
93
|
|
|
|
94
|
|
|
if (!is_string($value)) { |
95
|
|
|
throw new \InvalidArgumentException('"'.$setting.'" setting value is not a string.'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this->replacePlaceholders($value, $placeholders); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Performs placeholders replacement in given text. |
103
|
|
|
* |
104
|
|
|
* @param string $value |
105
|
|
|
* @param array $placeholders |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
public function replacePlaceholders(string $value, array $placeholders = []) |
110
|
|
|
{ |
111
|
|
|
if (!count($placeholders)) { |
112
|
|
|
return $value; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
foreach ($placeholders as $find => $replace) { |
116
|
|
|
$value = str_replace('{'.$find.'}', $replace, $value); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $value; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Returns in-memory replaced settings. |
124
|
|
|
* |
125
|
|
|
* @param array $settings |
126
|
|
|
* |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
|
|
public function getReplaced(array $settings) |
130
|
|
|
{ |
131
|
|
|
return array_intersect_key($this->replaced, array_flip($settings)); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Determines if setting value equals to given value. |
136
|
|
|
* |
137
|
|
|
* @param string $setting |
138
|
|
|
* @param $value |
139
|
|
|
* |
140
|
|
|
* @return bool |
141
|
|
|
*/ |
142
|
|
|
public function equals(string $setting, $value): bool |
143
|
|
|
{ |
144
|
|
|
return $this->get($setting) === $value; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Updates single setting value. |
149
|
|
|
* |
150
|
|
|
* @param string $setting |
151
|
|
|
* @param $value |
152
|
|
|
* |
153
|
|
|
* @return SettingsManager |
154
|
|
|
*/ |
155
|
|
|
public function update(string $setting, $value) |
156
|
|
|
{ |
157
|
|
|
return $this->updateMany([$setting => $value]); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Updates settings values. |
162
|
|
|
* |
163
|
|
|
* @param array $settings |
164
|
|
|
* |
165
|
|
|
* @return SettingsManager |
166
|
|
|
*/ |
167
|
|
|
public function updateMany(array $settings) |
168
|
|
|
{ |
169
|
|
|
$this->resource->setResourceSettingsValues($settings); |
170
|
|
|
|
171
|
|
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Replaces given settings values in memory storage. |
176
|
|
|
* |
177
|
|
|
* @param array $settings |
178
|
|
|
* @param bool $fullReplacement |
179
|
|
|
* |
180
|
|
|
* @return SettingsManager |
181
|
|
|
*/ |
182
|
|
|
public function replace(array $settings, bool $fullReplacement = false) |
183
|
|
|
{ |
184
|
|
|
if ($fullReplacement === true) { |
185
|
|
|
$this->replaced = $settings; |
186
|
|
|
} else { |
187
|
|
|
$this->replaced = array_merge($this->replaced, $settings); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Saves replaced settings. |
195
|
|
|
* |
196
|
|
|
* @return $this |
197
|
|
|
*/ |
198
|
|
|
public function saveReplaced() |
199
|
|
|
{ |
200
|
|
|
$this->updateMany($this->replaced); |
201
|
|
|
|
202
|
|
|
return $this; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|