1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Glorand\Model\Settings\Managers; |
4
|
|
|
|
5
|
|
|
use Glorand\Model\Settings\Contracts\SettingsManagerContract; |
6
|
|
|
use Glorand\Model\Settings\Models\ModelSettings; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class TableSettingsManager |
10
|
|
|
* @package Glorand\Model\Settings\Managers |
11
|
|
|
* @property \Illuminate\Database\Eloquent\Model|\Glorand\Model\Settings\Traits\HasSettingsTable $model |
12
|
|
|
*/ |
13
|
|
|
class TableSettingsManager extends AbstractSettingsManager |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @param array $settings |
17
|
|
|
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract |
18
|
|
|
*/ |
19
|
10 |
|
public function apply(array $settings = []): SettingsManagerContract |
20
|
|
|
{ |
21
|
10 |
|
if (!$modelSettings = $this->model->modelSettings()->first()) { |
22
|
10 |
|
$modelSettings = new ModelSettings(); |
23
|
10 |
|
$modelSettings->model()->associate($this->model); |
|
|
|
|
24
|
|
|
} |
25
|
10 |
|
$modelSettings->settings = $settings; |
26
|
10 |
|
$modelSettings->save(); |
27
|
|
|
|
28
|
10 |
|
return $this; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string|null $path |
33
|
|
|
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract |
34
|
|
|
* @throws \Exception |
35
|
|
|
*/ |
36
|
2 |
|
public function delete(string $path = null): SettingsManagerContract |
37
|
|
|
{ |
38
|
2 |
|
if (!$path) { |
39
|
|
|
/** @var ModelSettings $modelSettings */ |
40
|
2 |
|
if ($modelSettings = $this->model->modelSettings()->first()) { |
41
|
2 |
|
$modelSettings->delete(); |
42
|
|
|
} |
43
|
|
|
} else { |
44
|
1 |
|
$settings = $this->all(); |
45
|
1 |
|
array_forget($settings, $path); |
|
|
|
|
46
|
1 |
|
$this->apply($settings); |
47
|
|
|
} |
48
|
|
|
|
49
|
2 |
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param iterable $paths |
54
|
|
|
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract |
55
|
|
|
*/ |
56
|
1 |
|
public function deleteMultiple(iterable $paths): SettingsManagerContract |
57
|
|
|
{ |
58
|
1 |
|
$settings = $this->all(); |
59
|
1 |
|
foreach ($paths as $path) { |
60
|
1 |
|
array_forget($settings, $path); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
$this->apply($settings); |
64
|
|
|
|
65
|
1 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $path |
70
|
|
|
* @param mixed $value |
71
|
|
|
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract |
72
|
|
|
*/ |
73
|
2 |
|
public function set(string $path, $value): SettingsManagerContract |
74
|
|
|
{ |
75
|
2 |
|
$settings = $this->all(); |
76
|
2 |
|
array_set($settings, $path, $value); |
|
|
|
|
77
|
|
|
|
78
|
2 |
|
return $this->apply($settings); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|