1 | <?php |
||
2 | |||
3 | namespace Glorand\Model\Settings\Managers; |
||
4 | |||
5 | use Glorand\Model\Settings\Contracts\SettingsManagerContract; |
||
6 | use Glorand\Model\Settings\Exceptions\ModelSettingsException; |
||
7 | use Glorand\Model\Settings\Traits\HasSettings; |
||
8 | use Illuminate\Database\Eloquent\Model; |
||
9 | |||
10 | /** |
||
11 | * Class AbstractSettingsManager |
||
12 | * @package Glorand\Model\Settings\Managers |
||
13 | */ |
||
14 | abstract class AbstractSettingsManager implements SettingsManagerContract |
||
15 | { |
||
16 | /** @var \Illuminate\Database\Eloquent\Model */ |
||
17 | protected $model; |
||
18 | |||
19 | /** |
||
20 | * AbstractSettingsManager constructor. |
||
21 | * @param \Illuminate\Database\Eloquent\Model $model |
||
22 | * @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException |
||
23 | */ |
||
24 | 26 | public function __construct(Model $model) |
|
25 | { |
||
26 | 26 | $this->model = $model; |
|
27 | 26 | if (!in_array(HasSettings::class, class_uses_recursive($this->model))) { |
|
28 | 1 | throw new ModelSettingsException('Wrong model, missing HasSettings trait.'); |
|
29 | } |
||
30 | 25 | } |
|
31 | |||
32 | /** |
||
33 | * @return array |
||
34 | */ |
||
35 | 25 | public function all(): array |
|
36 | { |
||
37 | 25 | return $this->model->getSettingsValue(); |
|
38 | } |
||
39 | |||
40 | /** |
||
41 | * @param string $path |
||
42 | * @return bool |
||
43 | */ |
||
44 | 2 | public function has(string $path): bool |
|
45 | { |
||
46 | 2 | return array_has($this->all(), $path); |
|
47 | } |
||
48 | |||
49 | /** |
||
50 | * @param string|null $path |
||
51 | * @param null $default |
||
1 ignored issue
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
52 | * @return array|mixed |
||
53 | */ |
||
54 | 8 | public function get(string $path = null, $default = null) |
|
55 | { |
||
56 | 8 | return $path ? array_get($this->all(), $path, $default) : $this->all(); |
|
57 | } |
||
58 | |||
59 | /** |
||
60 | * @param iterable|null $paths |
||
61 | * @param null $default |
||
62 | * @return iterable |
||
63 | */ |
||
64 | 2 | public function getMultiple(iterable $paths = null, $default = null): iterable |
|
65 | { |
||
66 | 2 | $values = []; |
|
67 | 2 | foreach ($paths as $path) { |
|
68 | 2 | $values[$path] = $this->get($path, $default); |
|
69 | } |
||
70 | |||
71 | 2 | return $values; |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * @param string $path |
||
76 | * @param mixed $value |
||
77 | * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract |
||
78 | */ |
||
79 | 2 | public function update(string $path, $value): SettingsManagerContract |
|
80 | { |
||
81 | 2 | return $this->set($path, $value); |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract |
||
86 | */ |
||
87 | 2 | public function clear(): SettingsManagerContract |
|
88 | { |
||
89 | 2 | return $this->delete(); |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * @param iterable $values |
||
94 | * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract |
||
95 | */ |
||
96 | 2 | public function setMultiple(iterable $values): SettingsManagerContract |
|
97 | { |
||
98 | 2 | $settings = $this->all(); |
|
99 | 2 | foreach ($values as $path => $value) { |
|
100 | 2 | array_set($settings, $path, $value); |
|
101 | } |
||
102 | |||
103 | 2 | return $this->apply($settings); |
|
104 | } |
||
105 | } |
||
106 |