1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bavix\Context; |
4
|
|
|
|
5
|
|
|
use Bavix\Helpers\JSON; |
6
|
|
|
use Bavix\Security\Security; |
7
|
|
|
|
8
|
|
|
abstract class Container implements Content, \ArrayAccess, \Iterator |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* unpack array store |
13
|
|
|
* |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
protected $rows = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* packed array store |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $store; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $password; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Security |
32
|
|
|
*/ |
33
|
|
|
protected $security; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Container constructor. |
37
|
|
|
* |
38
|
|
|
* @param string $password |
39
|
|
|
*/ |
40
|
15 |
|
public function __construct(?string $password = null) |
41
|
|
|
{ |
42
|
15 |
|
$this->password = $password; |
43
|
15 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return Security |
47
|
|
|
*/ |
48
|
11 |
|
protected function security(): Security |
49
|
|
|
{ |
50
|
11 |
|
if (!$this->security) |
51
|
|
|
{ |
52
|
11 |
|
$this->security = new Security($this->password); |
53
|
|
|
} |
54
|
|
|
|
55
|
11 |
|
return $this->security; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param mixed $data |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
15 |
|
public function encrypt($data): string |
64
|
|
|
{ |
65
|
15 |
|
if (!$this->password) |
66
|
|
|
{ |
67
|
4 |
|
return JSON::encode($data); |
68
|
|
|
} |
69
|
|
|
|
70
|
11 |
|
return $this->security() |
71
|
11 |
|
->encrypt(JSON::encode($data)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $data |
76
|
|
|
* |
77
|
|
|
* @return mixed |
78
|
|
|
*/ |
79
|
4 |
|
public function decrypt(string $data) |
80
|
|
|
{ |
81
|
4 |
|
if (!$this->password) |
82
|
|
|
{ |
83
|
2 |
|
return JSON::decode($data); |
84
|
|
|
} |
85
|
|
|
|
86
|
2 |
|
$decrypt = $this->security() |
87
|
2 |
|
->decrypt($data); |
88
|
|
|
|
89
|
2 |
|
return JSON::decode($decrypt); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* cleanup content from container |
94
|
|
|
*/ |
95
|
2 |
|
public function cleanup(): void |
96
|
|
|
{ |
97
|
2 |
|
foreach ($this->getStore() as $key => $value) |
98
|
|
|
{ |
99
|
2 |
|
$this->remove($key); |
100
|
|
|
} |
101
|
2 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $name |
105
|
|
|
*/ |
106
|
6 |
|
public function remove(string $name): void |
107
|
|
|
{ |
108
|
6 |
|
unset($this->rows[$name], $this->store[$name]); |
109
|
6 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string $name |
113
|
|
|
* @param mixed $data |
114
|
|
|
*/ |
115
|
15 |
|
public function set(string $name, $data): void |
116
|
|
|
{ |
117
|
15 |
|
$this->store[$name] = $this->encrypt($data); |
118
|
15 |
|
$this->rows[$name] = $data; |
119
|
15 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $name |
123
|
|
|
* @param mixed $default |
124
|
|
|
* |
125
|
|
|
* @return mixed |
126
|
|
|
*/ |
127
|
15 |
|
public function get(string $name, $default = null) |
128
|
|
|
{ |
129
|
15 |
|
if (isset($this->rows[$name])) |
130
|
|
|
{ |
131
|
11 |
|
return $this->rows[$name]; |
132
|
|
|
} |
133
|
|
|
|
134
|
10 |
|
if (isset($this->store[$name])) |
135
|
|
|
{ |
136
|
4 |
|
$this->rows[$name] = $this->decrypt($this->store[$name]); |
137
|
|
|
|
138
|
4 |
|
return $this->rows[$name]; |
139
|
|
|
} |
140
|
|
|
|
141
|
6 |
|
return $default; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return array |
146
|
|
|
*/ |
147
|
2 |
|
public function export(): array |
148
|
|
|
{ |
149
|
2 |
|
return \iterator_to_array($this->asGenerator()); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
2 |
|
public function __toString(): string |
156
|
|
|
{ |
157
|
2 |
|
return JSON::encode($this->asGenerator(), $this->jsonOptions()); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param string $name |
162
|
|
|
* |
163
|
|
|
* @return bool |
164
|
|
|
*/ |
165
|
2 |
|
public function __isset(string $name): bool |
166
|
|
|
{ |
167
|
2 |
|
return isset($this->store[$name]); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param string $name |
172
|
|
|
*/ |
173
|
2 |
|
public function __unset(string $name): void |
174
|
|
|
{ |
175
|
2 |
|
$this->remove($name); |
176
|
2 |
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param string $name |
180
|
|
|
* @param mixed $data |
181
|
|
|
*/ |
182
|
2 |
|
public function __set(string $name, $data): void |
183
|
|
|
{ |
184
|
2 |
|
$this->set($name, $data); |
185
|
2 |
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param string $name |
189
|
|
|
* |
190
|
|
|
* @return mixed |
191
|
|
|
*/ |
192
|
2 |
|
public function __get(string $name) |
193
|
|
|
{ |
194
|
2 |
|
return $this->get($name); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @return \Generator |
199
|
|
|
*/ |
200
|
2 |
|
protected function asGenerator(): ?\Generator |
201
|
|
|
{ |
202
|
2 |
|
foreach ($this->getStore() as $name => $value) |
203
|
|
|
{ |
204
|
2 |
|
yield $name => $this->get($name); |
205
|
|
|
} |
206
|
2 |
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @return int |
210
|
|
|
*/ |
211
|
2 |
|
protected function jsonOptions(): int |
212
|
|
|
{ |
213
|
2 |
|
return JSON_UNESCAPED_UNICODE | JSON_NUMERIC_CHECK; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @return array |
218
|
|
|
*/ |
219
|
2 |
|
public function getStore(): array |
220
|
|
|
{ |
221
|
2 |
|
return $this->store; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Return the current element |
226
|
|
|
* @link http://php.net/manual/en/iterator.current.php |
227
|
|
|
* @return mixed Can return any type. |
228
|
|
|
* @since 5.0.0 |
229
|
|
|
*/ |
230
|
2 |
|
public function current() |
231
|
|
|
{ |
232
|
2 |
|
return $this->get($this->key()); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Move forward to next element |
237
|
|
|
* @link http://php.net/manual/en/iterator.next.php |
238
|
|
|
* @return void Any returned value is ignored. |
239
|
|
|
* @since 5.0.0 |
240
|
|
|
*/ |
241
|
2 |
|
public function next(): void |
242
|
|
|
{ |
243
|
2 |
|
next($this->store); |
244
|
2 |
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Return the key of the current element |
248
|
|
|
* @link http://php.net/manual/en/iterator.key.php |
249
|
|
|
* @return mixed scalar on success, or null on failure. |
250
|
|
|
* @since 5.0.0 |
251
|
|
|
*/ |
252
|
2 |
|
public function key() |
253
|
|
|
{ |
254
|
2 |
|
return \key($this->store); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Checks if current position is valid |
259
|
|
|
* @link http://php.net/manual/en/iterator.valid.php |
260
|
|
|
* @return boolean The return value will be casted to boolean and then evaluated. |
261
|
|
|
* Returns true on success or false on failure. |
262
|
|
|
* @since 5.0.0 |
263
|
|
|
*/ |
264
|
2 |
|
public function valid(): bool |
265
|
|
|
{ |
266
|
2 |
|
return null !== $this->key(); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Rewind the Iterator to the first element |
271
|
|
|
* @link http://php.net/manual/en/iterator.rewind.php |
272
|
|
|
* @return void Any returned value is ignored. |
273
|
|
|
* @since 5.0.0 |
274
|
|
|
*/ |
275
|
2 |
|
public function rewind(): void |
276
|
|
|
{ |
277
|
2 |
|
reset($this->store); |
278
|
2 |
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Whether a offset exists |
282
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetexists.php |
283
|
|
|
* @param mixed $offset <p> |
284
|
|
|
* An offset to check for. |
285
|
|
|
* </p> |
286
|
|
|
* @return boolean true on success or false on failure. |
287
|
|
|
* </p> |
288
|
|
|
* <p> |
289
|
|
|
* The return value will be casted to boolean if non-boolean was returned. |
290
|
|
|
* @since 5.0.0 |
291
|
|
|
*/ |
292
|
2 |
|
public function offsetExists($offset): bool |
293
|
|
|
{ |
294
|
2 |
|
return isset($this->store[$offset]); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Offset to retrieve |
299
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetget.php |
300
|
|
|
* @param mixed $offset <p> |
301
|
|
|
* The offset to retrieve. |
302
|
|
|
* </p> |
303
|
|
|
* @return mixed Can return all value types. |
304
|
|
|
* @since 5.0.0 |
305
|
|
|
*/ |
306
|
2 |
|
public function offsetGet($offset) |
307
|
|
|
{ |
308
|
2 |
|
return $this->get($offset); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Offset to set |
313
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetset.php |
314
|
|
|
* @param mixed $offset <p> |
315
|
|
|
* The offset to assign the value to. |
316
|
|
|
* </p> |
317
|
|
|
* @param mixed $value <p> |
318
|
|
|
* The value to set. |
319
|
|
|
* </p> |
320
|
|
|
* @return void |
321
|
|
|
* @since 5.0.0 |
322
|
|
|
*/ |
323
|
2 |
|
public function offsetSet($offset, $value): void |
324
|
|
|
{ |
325
|
2 |
|
$this->set($offset, $value); |
326
|
2 |
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Offset to unset |
330
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetunset.php |
331
|
|
|
* @param mixed $offset <p> |
332
|
|
|
* The offset to unset. |
333
|
|
|
* </p> |
334
|
|
|
* @return void |
335
|
|
|
* @since 5.0.0 |
336
|
|
|
*/ |
337
|
2 |
|
public function offsetUnset($offset): void |
338
|
|
|
{ |
339
|
2 |
|
$this->remove($offset); |
340
|
2 |
|
} |
341
|
|
|
|
342
|
|
|
} |
343
|
|
|
|