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