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
|
|
|
use Illuminate\Support\Facades\Schema; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Trait HasSettingsField |
13
|
|
|
* @package Glorand\Model\Settings\Traits |
14
|
|
|
* @property array $settings |
15
|
|
|
* @property string $settingsFieldName |
16
|
|
|
* @property boolean $persistSettings |
17
|
|
|
*/ |
18
|
|
|
trait HasSettingsField |
19
|
|
|
{ |
20
|
|
|
use HasSettings; |
21
|
|
|
|
22
|
|
|
private $persistSettings = null; |
23
|
|
|
|
24
|
138 |
|
protected static function bootHasSettingsField() |
25
|
|
|
{ |
26
|
|
|
static::saving(function (self $model) { |
27
|
138 |
|
$model->fixSettingsValue(); |
28
|
138 |
|
}); |
29
|
138 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract |
33
|
|
|
* @throws ModelSettingsException |
34
|
|
|
*/ |
35
|
48 |
|
public function settings(): SettingsManagerContract |
36
|
|
|
{ |
37
|
48 |
|
return new FieldSettingsManager($this); |
38
|
|
|
} |
39
|
|
|
|
40
|
138 |
|
public function fixSettingsValue() |
41
|
|
|
{ |
42
|
138 |
|
$settingsFieldName = $this->getSettingsFieldName(); |
43
|
138 |
|
$attributes = $this->getAttributes(); |
44
|
138 |
|
if (Arr::has($attributes, $settingsFieldName)) { |
45
|
39 |
|
if (is_array($this->$settingsFieldName)) { |
46
|
3 |
|
$this->$settingsFieldName = json_encode($this->$settingsFieldName); |
47
|
|
|
} |
48
|
|
|
} |
49
|
138 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return array |
53
|
|
|
* @throws ModelSettingsException |
54
|
|
|
*/ |
55
|
48 |
|
public function getSettingsValue(): array |
56
|
|
|
{ |
57
|
48 |
|
$settingsFieldName = $this->getSettingsFieldName(); |
58
|
48 |
|
if (!$this->hasSettingsField()) { |
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
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return mixed |
92
|
|
|
* @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException |
93
|
|
|
*/ |
94
|
48 |
|
private function hasSettingsField() |
95
|
|
|
{ |
96
|
|
|
try { |
97
|
48 |
|
return cache()->remember( |
98
|
48 |
|
config('model_settings.settings_table_cache_prefix') . '::has_field', |
99
|
48 |
|
now()->addDays(1), |
100
|
|
|
function () { |
101
|
48 |
|
return Schema::hasColumn($this->getTable(), $this->getSettingsFieldName()); |
102
|
48 |
|
} |
103
|
|
|
); |
104
|
|
|
} catch (\Exception $e) { |
105
|
|
|
throw new ModelSettingsException("Cache: " . $e->getMessage()); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
abstract public function getTable(); |
110
|
|
|
|
111
|
|
|
abstract public function getAttributes(); |
112
|
|
|
|
113
|
|
|
abstract public function getAttributeValue($key); |
114
|
|
|
} |
115
|
|
|
|