1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Arbory\Base\Admin\Settings; |
4
|
|
|
|
5
|
|
|
use Arbory\Base\Admin\Form\Fields\ArboryFile; |
6
|
|
|
use Arbory\Base\Admin\Form\Fields\ArboryImage; |
7
|
|
|
use Arbory\Base\Admin\Form\Fields\Text; |
8
|
|
|
use Arbory\Base\Admin\Form\Fields\Translatable; |
9
|
|
|
|
10
|
|
|
class SettingDefinition |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $key; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $type; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var mixed |
24
|
|
|
*/ |
25
|
|
|
protected $value; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var mixed |
29
|
|
|
*/ |
30
|
|
|
protected $configEntry; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Setting|null |
34
|
|
|
*/ |
35
|
|
|
protected $model; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $key |
39
|
|
|
* @param mixed $value |
40
|
|
|
* @param string|null $type |
41
|
|
|
* @param mixed $configEntry |
42
|
|
|
* @param Setting|null $databaseEntry |
43
|
|
|
*/ |
44
|
|
|
public function __construct( |
45
|
|
|
string $key, $value = null, string $type = null, $configEntry = null, Setting $databaseEntry = null |
46
|
|
|
) |
47
|
|
|
{ |
48
|
|
|
$this->key = $key; |
49
|
|
|
$this->value = $value; |
50
|
|
|
$this->type = $type ?? Text::class; |
51
|
|
|
$this->configEntry = $configEntry; |
52
|
|
|
$this->model = $databaseEntry; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
public function save() |
60
|
|
|
{ |
61
|
|
|
$setting = new Setting( $this->toArray() ); |
62
|
|
|
|
63
|
|
|
if( $this->isInDatabase() ) |
64
|
|
|
{ |
65
|
|
|
$setting->exists = true; |
66
|
|
|
$setting->update(); |
67
|
|
|
} |
68
|
|
|
else |
69
|
|
|
{ |
70
|
|
|
$setting->save(); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public function getKey(): string |
78
|
|
|
{ |
79
|
|
|
return $this->key; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $key |
84
|
|
|
*/ |
85
|
|
|
public function setKey( string $key ) |
86
|
|
|
{ |
87
|
|
|
$this->key = $key; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function getType(): string |
94
|
|
|
{ |
95
|
|
|
return $this->type; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $type |
100
|
|
|
*/ |
101
|
|
|
public function setType( string $type ) |
102
|
|
|
{ |
103
|
|
|
$this->type = $type; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return mixed |
108
|
|
|
*/ |
109
|
|
|
public function getValue() |
110
|
|
|
{ |
111
|
|
|
return $this->value; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param mixed $value |
116
|
|
|
*/ |
117
|
|
|
public function setValue( $value ) |
118
|
|
|
{ |
119
|
|
|
$this->value = $value; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return mixed |
124
|
|
|
*/ |
125
|
|
|
public function getConfigEntry() |
126
|
|
|
{ |
127
|
|
|
return $this->configEntry; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return Setting|null |
132
|
|
|
*/ |
133
|
|
|
public function getModel() |
134
|
|
|
{ |
135
|
|
|
return $this->model; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param Setting|null $model |
140
|
|
|
*/ |
141
|
|
|
public function setModel( $model ) |
142
|
|
|
{ |
143
|
|
|
$this->model = $model; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return bool |
148
|
|
|
*/ |
149
|
|
|
public function isInDatabase(): bool |
150
|
|
|
{ |
151
|
|
|
return Setting::query()->where( 'name', $this->getKey() )->exists(); |
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return bool |
156
|
|
|
*/ |
157
|
|
|
public function isFile(): bool |
158
|
|
|
{ |
159
|
|
|
return in_array( $this->getType(), [ |
160
|
|
|
ArboryFile::class, |
161
|
|
|
ArboryImage::class |
162
|
|
|
], true ); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return bool |
167
|
|
|
*/ |
168
|
|
|
public function isImage(): bool |
169
|
|
|
{ |
170
|
|
|
return $this->getType() === ArboryImage::class; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return bool |
175
|
|
|
*/ |
176
|
|
|
public function isTranslatable(): bool |
177
|
|
|
{ |
178
|
|
|
return $this->getType() === Translatable::class; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @return array |
183
|
|
|
*/ |
184
|
|
|
public function toArray() |
185
|
|
|
{ |
186
|
|
|
return [ |
187
|
|
|
'name' => $this->key, |
188
|
|
|
'value' => $this->value, |
189
|
|
|
'type' => $this->type |
190
|
|
|
]; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|