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
|
|
|
use Illuminate\Support\Arr; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class AbstractSettingsManager |
13
|
|
|
* @package Glorand\Model\Settings\Managers |
14
|
|
|
*/ |
15
|
|
|
abstract class AbstractSettingsManager implements SettingsManagerContract |
16
|
|
|
{ |
17
|
|
|
/** @var \Illuminate\Database\Eloquent\Model */ |
18
|
|
|
protected $model; |
19
|
|
|
|
20
|
|
|
/** @var array */ |
21
|
|
|
protected $defaultSettings = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* AbstractSettingsManager constructor. |
25
|
|
|
* @param \Illuminate\Database\Eloquent\Model|HasSettings $model |
26
|
|
|
* @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException |
27
|
|
|
*/ |
28
|
195 |
|
public function __construct(Model $model) |
29
|
|
|
{ |
30
|
195 |
|
$this->model = $model; |
31
|
195 |
|
if (!in_array(HasSettings::class, class_uses_recursive($this->model))) { |
32
|
3 |
|
throw new ModelSettingsException('Wrong model, missing HasSettings trait.'); |
33
|
|
|
} |
34
|
192 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
192 |
|
public function all(): array |
40
|
|
|
{ |
41
|
192 |
|
$array = []; |
42
|
192 |
|
foreach (array_merge($this->model->getDefaultSettings(), $this->model->getSettingsValue()) as $key => $value) { |
|
|
|
|
43
|
168 |
|
Arr::set($array, $key, $value); |
44
|
|
|
} |
45
|
|
|
|
46
|
183 |
|
return $array; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
12 |
|
public function exist(): bool |
53
|
|
|
{ |
54
|
12 |
|
return count($this->all()) > 0; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
12 |
|
public function empty(): bool |
61
|
|
|
{ |
62
|
12 |
|
return count($this->all()) <= 0; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $path |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
12 |
|
public function has(string $path): bool |
70
|
|
|
{ |
71
|
12 |
|
return Arr::has($this->all(), $path); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string|null $path |
76
|
|
|
* @param null $default |
77
|
|
|
* @return array|mixed |
78
|
|
|
*/ |
79
|
48 |
|
public function get(string $path = null, $default = null) |
80
|
|
|
{ |
81
|
48 |
|
return $path ? Arr::get($this->all(), $path, $default) : $this->all(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param iterable|null $paths |
86
|
|
|
* @param null $default |
87
|
|
|
* @return iterable |
88
|
|
|
*/ |
89
|
12 |
|
public function getMultiple(iterable $paths = null, $default = null): iterable |
90
|
|
|
{ |
91
|
12 |
|
$values = []; |
92
|
12 |
|
foreach ($paths as $path) { |
93
|
12 |
|
$values[$path] = $this->get($path, $default); |
94
|
|
|
} |
95
|
|
|
|
96
|
12 |
|
return $values; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $path |
101
|
|
|
* @param $value |
102
|
|
|
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract |
103
|
|
|
*/ |
104
|
24 |
|
public function set(string $path, $value): SettingsManagerContract |
105
|
|
|
{ |
106
|
24 |
|
$settings = $this->all(); |
107
|
24 |
|
Arr::set($settings, $path, $value); |
108
|
|
|
|
109
|
24 |
|
return $this->apply($settings); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $path |
114
|
|
|
* @param mixed $value |
115
|
|
|
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract |
116
|
|
|
*/ |
117
|
12 |
|
public function update(string $path, $value): SettingsManagerContract |
118
|
|
|
{ |
119
|
12 |
|
return $this->set($path, $value); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string|null $path |
124
|
|
|
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract |
125
|
|
|
*/ |
126
|
60 |
|
public function delete(string $path = null): SettingsManagerContract |
127
|
|
|
{ |
128
|
60 |
|
if (!$path) { |
129
|
60 |
|
$settings = []; |
130
|
|
|
} else { |
131
|
12 |
|
$settings = $this->all(); |
132
|
12 |
|
Arr::forget($settings, $path); |
133
|
|
|
} |
134
|
|
|
|
135
|
60 |
|
$this->apply($settings); |
136
|
|
|
|
137
|
60 |
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract |
142
|
|
|
*/ |
143
|
42 |
|
public function clear(): SettingsManagerContract |
144
|
|
|
{ |
145
|
42 |
|
return $this->delete(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param iterable $values |
150
|
|
|
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract |
151
|
|
|
*/ |
152
|
12 |
|
public function setMultiple(iterable $values): SettingsManagerContract |
153
|
|
|
{ |
154
|
12 |
|
$settings = $this->all(); |
155
|
12 |
|
foreach ($values as $path => $value) { |
156
|
12 |
|
Arr::set($settings, $path, $value); |
157
|
|
|
} |
158
|
|
|
|
159
|
12 |
|
return $this->apply($settings); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param iterable $paths |
164
|
|
|
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract |
165
|
|
|
*/ |
166
|
12 |
|
public function deleteMultiple(iterable $paths): SettingsManagerContract |
167
|
|
|
{ |
168
|
12 |
|
$settings = $this->all(); |
169
|
12 |
|
foreach ($paths as $path) { |
170
|
12 |
|
Arr::forget($settings, $path); |
171
|
|
|
} |
172
|
|
|
|
173
|
12 |
|
$this->apply($settings); |
174
|
|
|
|
175
|
12 |
|
return $this; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|