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 |
|
*/ |
28
|
|
|
public function __construct(Model $model) |
29
|
28 |
|
{ |
30
|
28 |
|
$this->model = $model; |
31
|
1 |
|
if (!in_array(HasSettings::class, class_uses_recursive($this->model))) { |
32
|
|
|
throw new ModelSettingsException('Wrong model, missing HasSettings trait.'); |
33
|
27 |
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return array |
38
|
27 |
|
*/ |
39
|
|
|
public function all(): array |
40
|
27 |
|
{ |
41
|
|
|
return array_merge($this->model->getDefaultSettings(), $this->model->getSettingsValue()); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $path |
46
|
|
|
* @return bool |
47
|
2 |
|
*/ |
48
|
|
|
public function has(string $path): bool |
49
|
2 |
|
{ |
50
|
|
|
return Arr::has($this->all(), $path); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string|null $path |
55
|
|
|
* @param null $default |
56
|
|
|
* @return array|mixed |
57
|
8 |
|
*/ |
58
|
|
|
public function get(string $path = null, $default = null) |
59
|
8 |
|
{ |
60
|
|
|
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
|
2 |
|
*/ |
68
|
|
|
public function getMultiple(iterable $paths = null, $default = null): iterable |
69
|
2 |
|
{ |
70
|
2 |
|
$values = []; |
71
|
2 |
|
foreach ($paths as $path) { |
72
|
|
|
$values[$path] = $this->get($path, $default); |
73
|
|
|
} |
74
|
2 |
|
|
75
|
|
|
return $values; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $path |
80
|
|
|
* @param mixed $value |
81
|
|
|
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract |
82
|
2 |
|
*/ |
83
|
|
|
public function update(string $path, $value): SettingsManagerContract |
84
|
2 |
|
{ |
85
|
|
|
return $this->set($path, $value); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract |
90
|
2 |
|
*/ |
91
|
|
|
public function clear(): SettingsManagerContract |
92
|
2 |
|
{ |
93
|
|
|
return $this->delete(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param iterable $values |
98
|
|
|
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract |
99
|
2 |
|
*/ |
100
|
|
|
public function setMultiple(iterable $values): SettingsManagerContract |
101
|
2 |
|
{ |
102
|
2 |
|
$settings = $this->all(); |
103
|
2 |
|
foreach ($values as $path => $value) { |
104
|
|
|
Arr::set($settings, $path, $value); |
105
|
|
|
} |
106
|
2 |
|
|
107
|
|
|
return $this->apply($settings); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param iterable $paths |
112
|
|
|
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract |
113
|
|
|
*/ |
114
|
|
|
public function deleteMultiple(iterable $paths): SettingsManagerContract |
115
|
|
|
{ |
116
|
|
|
$settings = $this->all(); |
117
|
|
|
foreach ($paths as $path) { |
118
|
|
|
Arr::forget($settings, $path); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$this->apply($settings); |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|