1 | <?php |
||
9 | class Settings |
||
10 | { |
||
11 | /** |
||
12 | * Settings for resource. |
||
13 | * |
||
14 | * @var LaravelPropertyBag\Settings\ResourceConfig |
||
15 | */ |
||
16 | protected $settingsConfig; |
||
17 | |||
18 | /** |
||
19 | * Resource that has settings. |
||
20 | * |
||
21 | * @var Model |
||
22 | */ |
||
23 | protected $resource; |
||
24 | |||
25 | /** |
||
26 | * Registered keys, values, and defaults. |
||
27 | * 'key' => ['allowed' => $value, 'default' => $value]. |
||
28 | * |
||
29 | * @var \Illuminate\Support\Collection |
||
30 | */ |
||
31 | protected $registered; |
||
32 | |||
33 | /** |
||
34 | * Settings saved in database. Does not include defaults. |
||
35 | * |
||
36 | * @var \Illuminate\Support\Collection |
||
37 | */ |
||
38 | protected $settings; |
||
39 | |||
40 | /** |
||
41 | * Validator for allowed rules. |
||
42 | * |
||
43 | * @var LaravelPropertyBag\Settings\Rules\RuleValidator |
||
44 | */ |
||
45 | protected $ruleValidator; |
||
46 | |||
47 | /** |
||
48 | * Construct. |
||
49 | * |
||
50 | * @param ResourceConfig $settingsConfig |
||
51 | * @param Model $resource |
||
52 | */ |
||
53 | public function __construct(ResourceConfig $settingsConfig, Model $resource) |
||
63 | |||
64 | /** |
||
65 | * Get the property bag relationshp off the resource. |
||
66 | * |
||
67 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany |
||
68 | */ |
||
69 | protected function propertyBag() |
||
73 | |||
74 | /** |
||
75 | * Get resource config. |
||
76 | * |
||
77 | * @return LaravelPropertyBag\Settings\ResourceConfig |
||
78 | */ |
||
79 | public function getResourceConfig() |
||
83 | |||
84 | /** |
||
85 | * Get registered settings. |
||
86 | * |
||
87 | * @return \Illuminate\Support\Collection |
||
88 | */ |
||
89 | public function getRegistered() |
||
93 | |||
94 | /** |
||
95 | * Return true if key exists in registered settings collection. |
||
96 | * |
||
97 | * @param string $key |
||
98 | * |
||
99 | * @return bool |
||
100 | */ |
||
101 | public function isRegistered($key) |
||
105 | |||
106 | /** |
||
107 | * Return true if key and value are registered values. |
||
108 | * |
||
109 | * @param string $key |
||
110 | * @param mixed $value |
||
111 | * |
||
112 | * @return bool |
||
113 | */ |
||
114 | public function isValid($key, $value) |
||
129 | |||
130 | /** |
||
131 | * Return true if value is default value for key. |
||
132 | * |
||
133 | * @param string $key |
||
134 | * @param mixed $value |
||
135 | * |
||
136 | * @return bool |
||
137 | */ |
||
138 | public function isDefault($key, $value) |
||
142 | |||
143 | /** |
||
144 | * Get the default value from registered. |
||
145 | * |
||
146 | * @param string $key |
||
147 | * |
||
148 | * @return mixed |
||
149 | */ |
||
150 | public function getDefault($key) |
||
156 | |||
157 | /** |
||
158 | * Return all settings used by resource, including defaults. |
||
159 | * |
||
160 | * @return \Illuminate\Support\Collection |
||
161 | */ |
||
162 | public function all() |
||
174 | |||
175 | /** |
||
176 | * Get all defaults for settings. |
||
177 | * |
||
178 | * @return \Illuminate\Support\Collection |
||
179 | */ |
||
180 | public function allDefaults() |
||
186 | |||
187 | /** |
||
188 | * Get the allowed settings for key. |
||
189 | * |
||
190 | * @param string $key |
||
191 | * |
||
192 | * @return \Illuminate\Support\Collection |
||
193 | */ |
||
194 | public function getAllowed($key) |
||
200 | |||
201 | /** |
||
202 | * Get all allowed values for settings. |
||
203 | * |
||
204 | * @return \Illuminate\Support\Collection |
||
205 | */ |
||
206 | public function allAllowed() |
||
212 | |||
213 | /** |
||
214 | * Get all saved settings. Default values are not included in this output. |
||
215 | * |
||
216 | * @return \Illuminate\Support\Collection |
||
217 | */ |
||
218 | public function allSaved() |
||
222 | |||
223 | /** |
||
224 | * Update or add multiple values to the settings table. |
||
225 | * |
||
226 | * @param array $attributes |
||
227 | * |
||
228 | * @return this |
||
229 | */ |
||
230 | public function set(array $attributes) |
||
238 | |||
239 | /** |
||
240 | * Return true if key is set to value. |
||
241 | * |
||
242 | * @param string $key |
||
243 | * @param string $value |
||
244 | * |
||
245 | * @return bool |
||
246 | */ |
||
247 | public function keyIs($key, $value) |
||
251 | |||
252 | /** |
||
253 | * Reset key to default value. Return default value. |
||
254 | * |
||
255 | * @param string $key |
||
256 | * |
||
257 | * @return mixed |
||
258 | */ |
||
259 | public function reset($key) |
||
267 | |||
268 | /** |
||
269 | * Set a value to a key in local and database settings. |
||
270 | * |
||
271 | * @param string $key |
||
272 | * @param mixed $value |
||
273 | * |
||
274 | * @return mixed |
||
275 | */ |
||
276 | protected function setKeyValue($key, $value) |
||
290 | |||
291 | /** |
||
292 | * Throw exception if key/value invalid. |
||
293 | * |
||
294 | * @param string $key |
||
295 | * @param mixed $value |
||
296 | * |
||
297 | * @throws InvaildSettingsValue |
||
298 | */ |
||
299 | protected function validateKeyValue($key, $value) |
||
305 | |||
306 | /** |
||
307 | * Return true if key is already saved in database. |
||
308 | * |
||
309 | * @param string $key |
||
310 | * |
||
311 | * @return bool |
||
312 | */ |
||
313 | public function isSaved($key) |
||
317 | |||
318 | /** |
||
319 | * Create a new PropertyBag record. |
||
320 | * |
||
321 | * @param string $key |
||
322 | * @param mixed $value |
||
323 | * |
||
324 | * @return LaravelPropertyBag\Settings\PropertyBag |
||
325 | */ |
||
326 | protected function createRecord($key, $value) |
||
335 | |||
336 | /** |
||
337 | * Update a PropertyBag record. |
||
338 | * |
||
339 | * @param string $key |
||
340 | * @param mixed $value |
||
341 | * |
||
342 | * @return LaravelPropertyBag\Settings\PropertyBag |
||
343 | */ |
||
344 | protected function updateRecord($key, $value) |
||
354 | |||
355 | /** |
||
356 | * Json encode value. |
||
357 | * |
||
358 | * @param mixed $value |
||
359 | * |
||
360 | * @return string |
||
361 | */ |
||
362 | protected function valueToJson($value) |
||
366 | |||
367 | /** |
||
368 | * Delete a PropertyBag record. |
||
369 | * |
||
370 | * @param string $key |
||
371 | * |
||
372 | * @return bool |
||
373 | */ |
||
374 | protected function deleteRecord($key) |
||
378 | |||
379 | /** |
||
380 | * Get a property bag record by key. |
||
381 | * |
||
382 | * @param string $key |
||
383 | * |
||
384 | * @return LaravelPropertyBag\Settings\PropertyBag |
||
385 | */ |
||
386 | protected function getByKey($key) |
||
393 | |||
394 | /** |
||
395 | * Load settings from the resource relationship on to this. |
||
396 | */ |
||
397 | protected function sync() |
||
403 | |||
404 | /** |
||
405 | * Get all settings as a flat collection. |
||
406 | * |
||
407 | * @return \Illuminate\Support\Collection |
||
408 | */ |
||
409 | protected function getAllSettingsFlat() |
||
415 | |||
416 | /** |
||
417 | * Retrieve all settings from database. |
||
418 | * |
||
419 | * @return \Illuminate\Support\Collection |
||
420 | */ |
||
421 | protected function getAllSettings() |
||
427 | |||
428 | /** |
||
429 | * Get value from settings by key. Get registered default if not set. |
||
430 | * |
||
431 | * @param string $key |
||
432 | * |
||
433 | * @return mixed |
||
434 | */ |
||
435 | public function get($key) |
||
441 | } |
||
442 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..