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
|
|
|
use Illuminate\Support\Arr; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class TableSettingsManager |
11
|
|
|
* @package Glorand\Model\Settings\Managers |
12
|
|
|
* @property \Illuminate\Database\Eloquent\Model|\Glorand\Model\Settings\Traits\HasSettingsTable $model |
13
|
|
|
*/ |
14
|
|
|
class TableSettingsManager extends AbstractSettingsManager |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @param array $settings |
18
|
|
|
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract |
19
|
11 |
|
*/ |
20
|
|
|
public function apply(array $settings = []): SettingsManagerContract |
21
|
11 |
|
{ |
22
|
11 |
|
if (!$modelSettings = $this->model->modelSettings()->first()) { |
23
|
11 |
|
$modelSettings = new ModelSettings(); |
24
|
|
|
$modelSettings->model()->associate($this->model); |
|
|
|
|
25
|
11 |
|
} |
26
|
11 |
|
$modelSettings->settings = $settings; |
27
|
|
|
$modelSettings->save(); |
28
|
11 |
|
|
29
|
|
|
return $this; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string|null $path |
34
|
|
|
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract |
35
|
|
|
* @throws \Exception |
36
|
2 |
|
*/ |
37
|
|
|
public function delete(string $path = null): SettingsManagerContract |
38
|
2 |
|
{ |
39
|
|
|
if (!$path) { |
40
|
2 |
|
/** @var ModelSettings $modelSettings */ |
41
|
2 |
|
if ($modelSettings = $this->model->modelSettings()->first()) { |
42
|
|
|
$modelSettings->delete(); |
43
|
|
|
} |
44
|
1 |
|
} else { |
45
|
1 |
|
$settings = $this->all(); |
46
|
1 |
|
Arr::forget($settings, $path); |
47
|
|
|
$this->apply($settings); |
48
|
|
|
} |
49
|
2 |
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $path |
55
|
|
|
* @param mixed $value |
56
|
1 |
|
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract |
57
|
|
|
*/ |
58
|
1 |
|
public function set(string $path, $value): SettingsManagerContract |
59
|
1 |
|
{ |
60
|
1 |
|
$settings = $this->all(); |
61
|
|
|
Arr::set($settings, $path, $value); |
62
|
|
|
|
63
|
1 |
|
return $this->apply($settings); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|