-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Options.php
276 lines (245 loc) · 6.29 KB
/
Options.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
/**
* Arikaim
*
* @link http://www.arikaim.com
* @copyright Copyright (c) Konstantin Atanasov <info@arikaim.com>
* @license http://www.arikaim.com/license
*
*/
namespace Arikaim\Core\Options;
use Arikaim\Core\Collection\Collection;
use Arikaim\Core\Utils\Utils;
use Arikaim\Core\Utils\Number;
use Arikaim\Core\Interfaces\OptionsStorageInterface;
use Arikaim\Core\Interfaces\OptionsInterface;
/**
* Options base class
*/
class Options extends Collection implements OptionsInterface
{
/**
* Save cache time
*
* @var integer
*/
public static $cacheSaveTime = 10;
/**
* Should reload options array
*
* @var boolean
*/
protected $needReload;
/**
* Storage adapter
*
* @var OptionsStorageInterface
*/
protected $adapter;
/**
* Constructor
*
* @param OptionsStorageInterface $adapter
* @param bool $disabled
*/
public function __construct(OptionsStorageInterface $adapter)
{
$this->adapter = $adapter;
$this->needReload = true;
parent::__construct([]);
}
/**
* Set storage adapter
*
* @param OptionsStorageInterface $adapter
* @return void
*/
public function setStorageAdapter(OptionsStorageInterface $adapter)
{
$this->adapter = $adapter;
}
/**
* Store options in collection
*
* @return void
*/
public function load(): void
{
global $arikaim;
$options = $arikaim->get('cache')->fetch('options');
if ($options === false) {
$options = $this->adapter->loadOptions();
$arikaim->get('cache')->save('options',$options,Self::$cacheSaveTime);
}
$this->data = $options;
$this->needReload = false;
}
/**
* Create option, if option exists return false
*
* @param string $key
* @param mixed $value
* @param boolean $autoLoad
* @param string|null $extension
* @return boolean
*/
public function createOption(string $key, $value, bool $autoLoad = false, ?string $extension = null): bool
{
$result = $this->adapter->createOption($key,$value,$autoLoad,$extension);
if ($result !== false) {
$this->data[$key] = $value;
}
return $result;
}
/**
* Save option
*
* @param string $key
* @param mixed $value
* @param string|null $extension
* @return bool
*/
public function set(string $key, $value, $extension = null)
{
global $arikaim;
$result = $this->adapter->saveOption($key,$value,$extension);
if ($result !== false) {
// clear options cache
$arikaim->get('cache')->delete('options');
$this->data[$key] = $value;
}
return $result;
}
/**
* Return true if option name exist
*
* @param string $key
* @return boolean
*/
public function has(string $key): bool
{
return $this->adapter->hasOption($key);
}
/**
* Get text value
*
* @param string $key
* @param string|null $default
* @return string
*/
public function getString(string $key, ?string $default = null): string
{
if ($this->needReload == true) {
$this->load();
}
$value = $this->data[$key] ?? $default ?? '';
return (\trim($value) == '') ? ($default ?? '') : (string)$value;
}
/**
* Get option
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function get(string $key, $default = null)
{
if ($this->needReload == true) {
$this->load();
}
if (isset($this->data[$key]) == false) {
$this->data[$key] = $this->adapter->read($key,$default);
}
$result = $this->data[$key] ?? $default;
return $this->resolveOptionType($result);
}
/**
* Force load option
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function read(string $key, $default = null)
{
$this->data[$key] = $this->adapter->read($key,$default);
return $this->data[$key];
}
/**
* Remove option(s)
*
* @param string $key
* @param string|null $extension
* @return bool
*/
public function removeOptions($key = null, $extension = null)
{
$this->needReload = true;
return $this->adapter->remove($key,$extension);
}
/**
* Search options
*
* @param string $searchKey
* @param bool $compactKeys
* @return array
*/
public function searchOptions($searchKey, $compactKeys = false)
{
return $this->resolveOptions($this->adapter->searchOptions($searchKey,$compactKeys));
}
/**
* Get extension options
*
* @param string $extensioName
* @return mixed
*/
public function getExtensionOptions($extensioName)
{
return $this->adapter->getExtensionOptions($extensioName);
}
/**
* Return collection array
*
* @return array
*/
public function toArray(): array
{
if ($this->needReload == true) {
$this->load();
}
return $this->resolveOptions($this->data);
}
/**
* Resolve option type
*
* @param mixed $value
* @return mixed
*/
protected function resolveOptionType($value)
{
if (\is_string($value) == true) {
$value = (Utils::isJson($value) == true) ? \json_decode($value,true) : $value;
}
if (\is_array($value) == true) {
$value = $this->resolveOptions($value);
}
if (\is_string($value) == true) {
$value = (Number::isBoolean($value) == true) ? (bool)Number::toBoolean($value) : $value;
}
return $value;
}
/**
* Resolve options
*
* @param array $options
* @return array
*/
protected function resolveOptions(array $options): array
{
foreach($options as $key => $value) {
$options[$key] = $this->resolveOptionType($value);
}
return $options;
}
}