1 | <?php |
||
2 | |||
3 | namespace Glorand\Model\Settings\Traits; |
||
4 | |||
5 | use Glorand\Model\Settings\Contracts\SettingsManagerContract; |
||
6 | use Glorand\Model\Settings\Exceptions\ModelSettingsException; |
||
7 | use Glorand\Model\Settings\Managers\FieldSettingsManager; |
||
8 | |||
9 | /** |
||
10 | * Trait HasSettingsField |
||
11 | * @package Glorand\Model\Settings\Traits |
||
12 | * @property array $settings |
||
13 | * @property string $settingsFieldName |
||
14 | * @property boolean $persistSettings |
||
15 | */ |
||
16 | trait HasSettingsField |
||
17 | { |
||
18 | use HasSettings; |
||
19 | |||
20 | private $persistSettings = null; |
||
21 | |||
22 | /** |
||
23 | * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract |
||
24 | * @throws ModelSettingsException |
||
25 | */ |
||
26 | 10 | public function settings(): SettingsManagerContract |
|
27 | { |
||
28 | 10 | return new FieldSettingsManager($this); |
|
29 | } |
||
30 | |||
31 | /** |
||
32 | * @return array |
||
33 | * @throws ModelSettingsException |
||
34 | */ |
||
35 | 10 | public function getSettingsValue(): array |
|
36 | { |
||
37 | 10 | $settingsFieldName = $this->getSettingsFieldName(); |
|
38 | 10 | $attributes = $this->getAttributes(); |
|
39 | 10 | if (!array_has($attributes, $settingsFieldName)) { |
|
0 ignored issues
–
show
|
|||
40 | 2 | throw new ModelSettingsException("Unknown field ($settingsFieldName) on table {$this->getTable()}"); |
|
41 | } |
||
42 | |||
43 | 8 | return json_decode($this->getAttributeValue($settingsFieldName) ?? '[]', true); |
|
44 | } |
||
45 | |||
46 | /** |
||
47 | * @return string |
||
48 | */ |
||
49 | 10 | public function getSettingsFieldName(): string |
|
50 | { |
||
51 | 10 | return $this->settingsFieldName ?? config('model_settings.settings_field_name'); |
|
52 | } |
||
53 | |||
54 | /** |
||
55 | * @return bool |
||
56 | */ |
||
57 | 7 | public function isPersistSettings(): bool |
|
58 | { |
||
59 | 7 | return boolval($this->persistSettings ?? config('model_settings.settings_persistent')); |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * @param bool $val |
||
64 | */ |
||
65 | 1 | public function setPersistSettings(bool $val = true) |
|
66 | { |
||
67 | 1 | $this->persistSettings = $val; |
|
68 | 1 | } |
|
69 | |||
70 | abstract public function getTable(); |
||
71 | |||
72 | abstract public function getAttributes(); |
||
73 | |||
74 | abstract public function getAttributeValue($key); |
||
75 | } |
||
76 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.