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\Cache; |
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
|
1 |
|
use HasSettings; |
22
|
|
|
|
23
|
|
|
private $persistSettings = null; |
24
|
|
|
|
25
|
234 |
|
protected static function bootHasSettingsField() |
26
|
|
|
{ |
27
|
156 |
|
static::saving(function ($model) { |
28
|
|
|
/** @var self $model */ |
29
|
234 |
|
$model->fixSettingsValue(); |
30
|
234 |
|
}); |
31
|
234 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract |
35
|
|
|
* @throws ModelSettingsException |
36
|
|
|
*/ |
37
|
111 |
|
public function settings(): SettingsManagerContract |
38
|
|
|
{ |
39
|
111 |
|
return new FieldSettingsManager($this); |
40
|
|
|
} |
41
|
|
|
|
42
|
234 |
|
public function fixSettingsValue() |
43
|
|
|
{ |
44
|
234 |
|
$settingsFieldName = $this->getSettingsFieldName(); |
45
|
234 |
|
$attributes = $this->getAttributes(); |
46
|
234 |
|
if (Arr::has($attributes, $settingsFieldName)) { |
47
|
96 |
|
if (is_array($this->$settingsFieldName)) { |
48
|
6 |
|
$this->$settingsFieldName = json_encode($this->$settingsFieldName); |
49
|
|
|
} |
50
|
|
|
} |
51
|
234 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return array |
55
|
|
|
* @throws ModelSettingsException |
56
|
|
|
*/ |
57
|
111 |
|
public function getSettingsValue(): array |
58
|
|
|
{ |
59
|
111 |
|
$settingsFieldName = $this->getSettingsFieldName(); |
60
|
111 |
|
if (!$this->hasSettingsField()) { |
61
|
9 |
|
throw new ModelSettingsException("Unknown field ($settingsFieldName) on table {$this->getTable()}"); |
62
|
|
|
} |
63
|
|
|
|
64
|
102 |
|
$value = json_decode($this->getAttributeValue($settingsFieldName) ?? '[]', true); |
65
|
|
|
|
66
|
102 |
|
return is_array($value) ? $value : []; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
234 |
|
public function getSettingsFieldName(): string |
73
|
|
|
{ |
74
|
234 |
|
return $this->settingsFieldName ?? config('model_settings.settings_field_name'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
234 |
|
public function getConnectionName(): string |
81
|
|
|
{ |
82
|
234 |
|
return $this->connection ?? config('database.default'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
87 |
|
public function isPersistSettings(): bool |
89
|
|
|
{ |
90
|
87 |
|
return boolval($this->persistSettings ?? config('model_settings.settings_persistent')); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param bool $val |
95
|
|
|
*/ |
96
|
6 |
|
public function setPersistSettings(bool $val = true) |
97
|
|
|
{ |
98
|
6 |
|
$this->persistSettings = $val; |
99
|
6 |
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return mixed |
104
|
|
|
*/ |
105
|
111 |
|
private function hasSettingsField() |
106
|
|
|
{ |
107
|
111 |
|
return Cache::remember( |
108
|
111 |
|
config('model_settings.settings_table_cache_prefix') . '::has_field', |
109
|
111 |
|
now()->addDays(1), |
110
|
74 |
|
function () { |
111
|
111 |
|
return Schema::connection($this->getConnectionName()) |
112
|
111 |
|
->hasColumn( |
113
|
111 |
|
$this->getTable(), |
114
|
111 |
|
$this->getSettingsFieldName() |
115
|
|
|
); |
116
|
111 |
|
} |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
abstract public function getTable(); |
121
|
|
|
|
122
|
|
|
abstract public function getAttributes(); |
123
|
|
|
|
124
|
|
|
abstract public function getAttributeValue($key); |
125
|
|
|
} |
126
|
|
|
|