|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Glorand\Model\Settings\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Glorand\Model\Settings\Contracts\SettingsManagerContract; |
|
6
|
|
|
use Glorand\Model\Settings\Exceptions\ModelSettingsException; |
|
7
|
|
|
use Glorand\Model\Settings\Managers\FieldSettingsManager; |
|
8
|
|
|
use Illuminate\Support\Arr; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Trait HasSettingsField |
|
12
|
|
|
* @package Glorand\Model\Settings\Traits |
|
13
|
|
|
* @property array $settings |
|
14
|
|
|
* @property string $settingsFieldName |
|
15
|
|
|
* @property boolean $persistSettings |
|
16
|
|
|
*/ |
|
17
|
|
|
trait HasSettingsField |
|
18
|
|
|
{ |
|
19
|
|
|
use HasSettings; |
|
20
|
|
|
|
|
21
|
|
|
private $persistSettings = null; |
|
22
|
|
|
|
|
23
|
138 |
|
protected static function bootHasSettingsField() |
|
24
|
|
|
{ |
|
25
|
|
|
static::saving(function (self $model) { |
|
26
|
138 |
|
$model->fixSettingsValue(); |
|
27
|
138 |
|
}); |
|
28
|
138 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract |
|
32
|
|
|
* @throws ModelSettingsException |
|
33
|
|
|
*/ |
|
34
|
48 |
|
public function settings(): SettingsManagerContract |
|
35
|
|
|
{ |
|
36
|
48 |
|
return new FieldSettingsManager($this); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
138 |
|
public function fixSettingsValue() |
|
40
|
|
|
{ |
|
41
|
138 |
|
$settingsFieldName = $this->getSettingsFieldName(); |
|
42
|
138 |
|
$attributes = $this->getAttributes(); |
|
43
|
138 |
|
if (Arr::has($attributes, $settingsFieldName)) { |
|
44
|
39 |
|
if (is_array($this->$settingsFieldName)) { |
|
45
|
3 |
|
$this->$settingsFieldName = json_encode($this->$settingsFieldName); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
138 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return array |
|
52
|
|
|
* @throws ModelSettingsException |
|
53
|
|
|
*/ |
|
54
|
48 |
|
public function getSettingsValue(): array |
|
55
|
|
|
{ |
|
56
|
48 |
|
$settingsFieldName = $this->getSettingsFieldName(); |
|
57
|
48 |
|
$attributes = $this->getAttributes(); |
|
58
|
48 |
|
if (!Arr::has($attributes, $settingsFieldName)) { |
|
59
|
6 |
|
throw new ModelSettingsException("Unknown field ($settingsFieldName) on table {$this->getTable()}"); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
42 |
|
return json_decode($this->getAttributeValue($settingsFieldName) ?? '[]', true); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
138 |
|
public function getSettingsFieldName(): string |
|
69
|
|
|
{ |
|
70
|
138 |
|
return $this->settingsFieldName ?? config('model_settings.settings_field_name'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return bool |
|
75
|
|
|
*/ |
|
76
|
36 |
|
public function isPersistSettings(): bool |
|
77
|
|
|
{ |
|
78
|
36 |
|
return boolval($this->persistSettings ?? config('model_settings.settings_persistent')); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param bool $val |
|
83
|
|
|
*/ |
|
84
|
3 |
|
public function setPersistSettings(bool $val = true) |
|
85
|
|
|
{ |
|
86
|
3 |
|
$this->persistSettings = $val; |
|
87
|
3 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
abstract public function getTable(); |
|
90
|
|
|
|
|
91
|
|
|
abstract public function getAttributes(); |
|
92
|
|
|
|
|
93
|
|
|
abstract public function getAttributeValue($key); |
|
94
|
|
|
} |
|
95
|
|
|
|