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
|
123 |
|
public function __construct(Model $model) |
29
|
|
|
{ |
30
|
123 |
|
$this->model = $model; |
31
|
123 |
|
if (!in_array(HasSettings::class, class_uses_recursive($this->model))) { |
32
|
3 |
|
throw new ModelSettingsException('Wrong model, missing HasSettings trait.'); |
33
|
|
|
} |
34
|
120 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
120 |
|
public function all(): array |
40
|
|
|
{ |
41
|
120 |
|
return array_merge($this->model->getDefaultSettings(), $this->model->getSettingsValue()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $path |
46
|
|
|
* @return bool |
47
|
|
|
*/ |
48
|
9 |
|
public function has(string $path): bool |
49
|
|
|
{ |
50
|
9 |
|
return Arr::has($this->all(), $path); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string|null $path |
55
|
|
|
* @param null $default |
56
|
|
|
* @return array|mixed |
57
|
|
|
*/ |
58
|
36 |
|
public function get(string $path = null, $default = null) |
59
|
|
|
{ |
60
|
36 |
|
return $path ? Arr::get($this->all(), $path, $default) : $this->all(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param iterable|null $paths |
65
|
|
|
* @param null $default |
66
|
|
|
* @return iterable |
67
|
|
|
*/ |
68
|
9 |
|
public function getMultiple(iterable $paths = null, $default = null): iterable |
69
|
|
|
{ |
70
|
9 |
|
$values = []; |
71
|
9 |
|
foreach ($paths as $path) { |
72
|
9 |
|
$values[$path] = $this->get($path, $default); |
73
|
|
|
} |
74
|
|
|
|
75
|
9 |
|
return $values; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $path |
80
|
|
|
* @param $value |
81
|
|
|
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract |
82
|
|
|
*/ |
83
|
18 |
|
public function set(string $path, $value): SettingsManagerContract |
84
|
|
|
{ |
85
|
18 |
|
$settings = $this->all(); |
86
|
18 |
|
Arr::set($settings, $path, $value); |
87
|
|
|
|
88
|
18 |
|
return $this->apply($settings); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $path |
93
|
|
|
* @param mixed $value |
94
|
|
|
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract |
95
|
|
|
*/ |
96
|
9 |
|
public function update(string $path, $value): SettingsManagerContract |
97
|
|
|
{ |
98
|
9 |
|
return $this->set($path, $value); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param string|null $path |
103
|
|
|
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract |
104
|
|
|
*/ |
105
|
33 |
|
public function delete(string $path = null): SettingsManagerContract |
106
|
|
|
{ |
107
|
33 |
|
if (!$path) { |
108
|
33 |
|
$settings = []; |
109
|
|
|
} else { |
110
|
9 |
|
$settings = $this->all(); |
111
|
9 |
|
Arr::forget($settings, $path); |
112
|
|
|
} |
113
|
|
|
|
114
|
33 |
|
$this->apply($settings); |
115
|
|
|
|
116
|
33 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract |
121
|
|
|
*/ |
122
|
21 |
|
public function clear(): SettingsManagerContract |
123
|
|
|
{ |
124
|
21 |
|
return $this->delete(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param iterable $values |
129
|
|
|
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract |
130
|
|
|
*/ |
131
|
9 |
|
public function setMultiple(iterable $values): SettingsManagerContract |
132
|
|
|
{ |
133
|
9 |
|
$settings = $this->all(); |
134
|
9 |
|
foreach ($values as $path => $value) { |
135
|
9 |
|
Arr::set($settings, $path, $value); |
136
|
|
|
} |
137
|
|
|
|
138
|
9 |
|
return $this->apply($settings); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param iterable $paths |
143
|
|
|
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract |
144
|
|
|
*/ |
145
|
9 |
|
public function deleteMultiple(iterable $paths): SettingsManagerContract |
146
|
|
|
{ |
147
|
9 |
|
$settings = $this->all(); |
148
|
9 |
|
foreach ($paths as $path) { |
149
|
9 |
|
Arr::forget($settings, $path); |
150
|
|
|
} |
151
|
|
|
|
152
|
9 |
|
$this->apply($settings); |
153
|
|
|
|
154
|
9 |
|
return $this; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|