1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* This file is part of the O2System Framework package.
|
4
|
|
|
*
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE
|
6
|
|
|
* file that was distributed with this source code.
|
7
|
|
|
*
|
8
|
|
|
* @author Steeve Andrian Salim
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim
|
10
|
|
|
*/
|
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------
|
13
|
|
|
|
14
|
|
|
namespace O2System\Spl\Patterns\Structural\Repository;
|
15
|
|
|
|
16
|
|
|
use O2System\Spl\Iterators\ArrayIterator;
|
17
|
|
|
|
18
|
|
|
/**
|
19
|
|
|
* Class AbstractRepository
|
20
|
|
|
* @package O2System\Spl\Patterns\Structural\Repository
|
21
|
|
|
*/
|
22
|
|
|
abstract class AbstractRepository implements
|
23
|
|
|
StorageInterface,
|
24
|
|
|
\ArrayAccess,
|
25
|
|
|
\Countable,
|
26
|
|
|
\IteratorAggregate,
|
27
|
|
|
\Serializable,
|
28
|
|
|
\JsonSerializable
|
29
|
|
|
{
|
30
|
|
|
/**
|
31
|
|
|
* AbstractRepository::$storage
|
32
|
|
|
*
|
33
|
|
|
* The array of data storage.
|
34
|
|
|
*
|
35
|
|
|
* @var array
|
36
|
|
|
*/
|
37
|
|
|
protected $storage = [];
|
38
|
|
|
|
39
|
|
|
// ------------------------------------------------------------------------
|
40
|
|
|
|
41
|
|
|
/**
|
42
|
|
|
* AbstractRepository::offsetSet
|
43
|
|
|
*
|
44
|
|
|
* Implementation of array access interface method setter as an alias of store method.
|
45
|
|
|
*
|
46
|
|
|
* @see http://php.net/manual/en/arrayaccess.offsetset.php
|
47
|
|
|
*
|
48
|
|
|
* @param string $offset The data offset key.
|
49
|
|
|
* @param mixed $data The data to be stored.
|
50
|
|
|
*
|
51
|
|
|
* @return void
|
52
|
|
|
*/
|
53
|
|
|
final public function offsetSet($offset, $data)
|
54
|
|
|
{
|
55
|
|
|
$this->store($offset, $data);
|
56
|
|
|
}
|
57
|
|
|
|
58
|
|
|
// ------------------------------------------------------------------------
|
59
|
|
|
|
60
|
|
|
/**
|
61
|
|
|
* AbstractRepository::store
|
62
|
|
|
*
|
63
|
|
|
* Store the data into the storage.
|
64
|
|
|
* An alias of AbstractRepository::__set method.
|
65
|
|
|
*
|
66
|
|
|
* @param string $offset The data offset key.
|
67
|
|
|
* @param mixed $data The data to be stored.
|
68
|
|
|
*
|
69
|
|
|
* @return void
|
70
|
|
|
*/
|
71
|
|
|
public function store($offset, $data)
|
72
|
|
|
{
|
73
|
|
|
$this->storage[ $offset ] = $data;
|
74
|
|
|
}
|
75
|
|
|
|
76
|
|
|
// ------------------------------------------------------------------------
|
77
|
|
|
|
78
|
|
|
/**
|
79
|
|
|
* AbstractRepository::__get
|
80
|
|
|
*
|
81
|
|
|
* Implementation of magic method getter and as an alias of get method.
|
82
|
|
|
*
|
83
|
|
|
* @see http://php.net/manual/en/language.oop5.overloading.php#object.get
|
84
|
|
|
*
|
85
|
|
|
* @param string $offset The object offset key.
|
86
|
|
|
*
|
87
|
|
|
* @return mixed Varies depends the data contents, return NULL when there offset is not found.
|
88
|
|
|
*/
|
89
|
|
|
final public function __get($offset)
|
90
|
|
|
{
|
91
|
|
|
return $this->get($offset);
|
92
|
|
|
}
|
93
|
|
|
|
94
|
|
|
// ------------------------------------------------------------------------
|
95
|
|
|
|
96
|
|
|
/**
|
97
|
|
|
* AbstractRepository::__set
|
98
|
|
|
*
|
99
|
|
|
* Implementation of magic method setter as an alias of store method.
|
100
|
|
|
*
|
101
|
|
|
* @see http://php.net/manual/en/language.oop5.overloading.php#object.set
|
102
|
|
|
*
|
103
|
|
|
* @param string $offset The data offset key.
|
104
|
|
|
* @param mixed $data The data to be stored.
|
105
|
|
|
*
|
106
|
|
|
* @return void
|
107
|
|
|
*/
|
108
|
|
|
final public function __set($offset, $data)
|
109
|
|
|
{
|
110
|
|
|
$this->store($offset, $data);
|
111
|
|
|
}
|
112
|
|
|
|
113
|
|
|
// ------------------------------------------------------------------------
|
114
|
|
|
|
115
|
|
|
/**
|
116
|
|
|
* AbstractRepository::get
|
117
|
|
|
*
|
118
|
|
|
* Retrieve the contained object which specified offset key.
|
119
|
|
|
* An alias of AbstractRepository::__get method.
|
120
|
|
|
*
|
121
|
|
|
* @param string $offset The object offset key.
|
122
|
|
|
*
|
123
|
|
|
* @return mixed Varies depends the data contents, return NULL when there offset is not found.
|
124
|
|
|
*/
|
125
|
|
|
public function get($offset)
|
126
|
|
|
{
|
127
|
|
|
if ($this->__isset($offset)) {
|
128
|
|
|
return $this->storage[ $offset ];
|
129
|
|
|
}
|
130
|
|
|
}
|
131
|
|
|
|
132
|
|
|
// ------------------------------------------------------------------------
|
133
|
|
|
|
134
|
|
|
/**
|
135
|
|
|
* AbstractRepository::__isset
|
136
|
|
|
*
|
137
|
|
|
* Implementation of magic method to check inaccessible properties
|
138
|
|
|
* and as an alias of exists method.
|
139
|
|
|
*
|
140
|
|
|
* @see http://php.net/manual/en/language.oop5.overloading.php#object.isset
|
141
|
|
|
*
|
142
|
|
|
* @param string $offset The object offset key.
|
143
|
|
|
*
|
144
|
|
|
* @return bool Returns TRUE on success or FALSE on failure.
|
145
|
|
|
*/
|
146
|
|
|
final public function __isset($offset)
|
147
|
|
|
{
|
148
|
|
|
return $this->exists($offset);
|
149
|
|
|
}
|
150
|
|
|
|
151
|
|
|
// ------------------------------------------------------------------------
|
152
|
|
|
|
153
|
|
|
/**
|
154
|
|
|
* AbstractRepository::exists
|
155
|
|
|
*
|
156
|
|
|
* Checks if the data exists on the storage.
|
157
|
|
|
*
|
158
|
|
|
* @param string $offset The object offset key.
|
159
|
|
|
*
|
160
|
|
|
* @return bool Returns TRUE on success or FALSE on failure.
|
161
|
|
|
*/
|
162
|
|
|
public function exists($offset)
|
163
|
|
|
{
|
164
|
|
|
return (bool)isset($this->storage[ $offset ]);
|
165
|
|
|
}
|
166
|
|
|
|
167
|
|
|
// ------------------------------------------------------------------------
|
168
|
|
|
|
169
|
|
|
/**
|
170
|
|
|
* AbstractRepository::offsetGet
|
171
|
|
|
*
|
172
|
|
|
* Implementation of array access interface method getter
|
173
|
|
|
* and as an alias of get method.
|
174
|
|
|
*
|
175
|
|
|
* @see http://php.net/manual/en/arrayaccess.offsetget.php
|
176
|
|
|
*
|
177
|
|
|
* @param string $offset The object offset key.
|
178
|
|
|
*
|
179
|
|
|
* @return mixed Varies depends the data contents, return NULL when there offset is not found.
|
180
|
|
|
*/
|
181
|
|
|
final public function offsetGet($offset)
|
182
|
|
|
{
|
183
|
|
|
return $this->get($offset);
|
184
|
|
|
}
|
185
|
|
|
|
186
|
|
|
// ------------------------------------------------------------------------
|
187
|
|
|
|
188
|
|
|
/**
|
189
|
|
|
* AbstractRepository::offsetExists
|
190
|
|
|
*
|
191
|
|
|
* Implementation of array access interface to check inaccessible properties
|
192
|
|
|
* and as an alias of exists method.
|
193
|
|
|
*
|
194
|
|
|
* @see http://php.net/manual/en/arrayaccess.offsetexists.php
|
195
|
|
|
*
|
196
|
|
|
* @param string $offset The object offset key.
|
197
|
|
|
*
|
198
|
|
|
* @return bool Returns TRUE on success or FALSE on failure.
|
199
|
|
|
*/
|
200
|
|
|
final public function offsetExists($offset)
|
201
|
|
|
{
|
202
|
|
|
return $this->exists($offset);
|
203
|
|
|
}
|
204
|
|
|
|
205
|
|
|
// ------------------------------------------------------------------------
|
206
|
|
|
|
207
|
|
|
/**
|
208
|
|
|
* AbstractRepository::__unset
|
209
|
|
|
*
|
210
|
|
|
* Implementation of magic method unset to remove inaccessible properties
|
211
|
|
|
* and as an alias of remove method.
|
212
|
|
|
*
|
213
|
|
|
* @see http://php.net/manual/en/language.oop5.overloading.php#object.unset
|
214
|
|
|
*
|
215
|
|
|
* @param string $offset The object offset key.
|
216
|
|
|
*
|
217
|
|
|
* @return void
|
218
|
|
|
*/
|
219
|
|
|
final public function __unset($offset)
|
220
|
|
|
{
|
221
|
|
|
$this->remove($offset);
|
222
|
|
|
}
|
223
|
|
|
|
224
|
|
|
// ------------------------------------------------------------------------
|
225
|
|
|
|
226
|
|
|
/**
|
227
|
|
|
* AbstractRepository::remove
|
228
|
|
|
*
|
229
|
|
|
* Removes a data from the storage.
|
230
|
|
|
* An alias of AbstractRepository::__unset method.
|
231
|
|
|
*
|
232
|
|
|
* @param string $offset The object offset key.
|
233
|
|
|
*
|
234
|
|
|
* @return void
|
235
|
|
|
*/
|
236
|
|
|
public function remove($offset)
|
237
|
|
|
{
|
238
|
|
|
if ($this->__isset($offset)) {
|
239
|
|
|
unset($this->storage[ $offset ]);
|
240
|
|
|
}
|
241
|
|
|
}
|
242
|
|
|
|
243
|
|
|
// ------------------------------------------------------------------------
|
244
|
|
|
|
245
|
|
|
/**
|
246
|
|
|
* AbstractRepository::offsetUnset
|
247
|
|
|
*
|
248
|
|
|
* Implementation of array access interface unset method to remove inaccessible properties
|
249
|
|
|
* and as an alias of remove method.
|
250
|
|
|
*
|
251
|
|
|
* @see http://php.net/manual/en/arrayaccess.offsetunset.php
|
252
|
|
|
*
|
253
|
|
|
* @param string $offset The object offset key.
|
254
|
|
|
*
|
255
|
|
|
* @return void
|
256
|
|
|
*/
|
257
|
|
|
final public function offsetUnset($offset)
|
258
|
|
|
{
|
259
|
|
|
$this->remove($offset);
|
260
|
|
|
}
|
261
|
|
|
|
262
|
|
|
// ------------------------------------------------------------------------
|
263
|
|
|
|
264
|
|
|
/**
|
265
|
|
|
* AbstractRepository::destroy
|
266
|
|
|
*
|
267
|
|
|
* Removes all data from the storage.
|
268
|
|
|
*
|
269
|
|
|
* @return array Array of old storage items.
|
270
|
|
|
*/
|
271
|
|
|
public function destroy()
|
272
|
|
|
{
|
273
|
|
|
$storage = $this->storage;
|
274
|
|
|
|
275
|
|
|
$this->storage = [];
|
276
|
|
|
|
277
|
|
|
return $storage;
|
278
|
|
|
}
|
279
|
|
|
|
280
|
|
|
// ------------------------------------------------------------------------
|
281
|
|
|
|
282
|
|
|
/**
|
283
|
|
|
* AbstractRepository::merge
|
284
|
|
|
*
|
285
|
|
|
* Merge new array of data into the data storage.
|
286
|
|
|
*
|
287
|
|
|
* @param array $data New array of data.
|
288
|
|
|
*
|
289
|
|
|
* @return array The old array of data storage.
|
290
|
|
|
*/
|
291
|
|
|
public function merge(array $data)
|
292
|
|
|
{
|
293
|
|
|
$oldStorage = $this->storage;
|
294
|
|
|
$this->storage = array_merge($this->storage, $data);
|
295
|
|
|
|
296
|
|
|
return $oldStorage;
|
297
|
|
|
}
|
298
|
|
|
|
299
|
|
|
// ------------------------------------------------------------------------
|
300
|
|
|
|
301
|
|
|
/**
|
302
|
|
|
* AbstractRepository::exchange
|
303
|
|
|
*
|
304
|
|
|
* Exchange the array of data storage into the new array of data.
|
305
|
|
|
*
|
306
|
|
|
* @param array $data New array of data.
|
307
|
|
|
*
|
308
|
|
|
* @return array The old array of data storage.
|
309
|
|
|
*/
|
310
|
|
|
public function exchange(array $data)
|
311
|
|
|
{
|
312
|
|
|
$oldStorage = $this->storage;
|
313
|
|
|
$this->storage = $data;
|
314
|
|
|
|
315
|
|
|
return $oldStorage;
|
316
|
|
|
}
|
317
|
|
|
|
318
|
|
|
// ------------------------------------------------------------------------
|
319
|
|
|
|
320
|
|
|
/**
|
321
|
|
|
* AbstractRepository::search
|
322
|
|
|
*
|
323
|
|
|
* Search data based on array offset key.
|
324
|
|
|
*
|
325
|
|
|
* @param string $offset The data offset key.
|
326
|
|
|
* @param mixed $return The fail over of data returns when the data is not found.
|
327
|
|
|
*
|
328
|
|
|
* @return mixed The returns is varies depends on the content of the data or the return variable.
|
329
|
|
|
*/
|
330
|
|
|
public function search($offset, $return = false)
|
331
|
|
|
{
|
332
|
|
|
if (array_key_exists($offset, $this->storage)) {
|
333
|
|
|
return $this->storage[ $offset ];
|
334
|
|
|
} elseif (false !== ($offsetKey = array_search($offset, $this->storage))) {
|
335
|
|
|
return $this->storage[ $offsetKey ];
|
336
|
|
|
}
|
337
|
|
|
|
338
|
|
|
return $return;
|
339
|
|
|
}
|
340
|
|
|
|
341
|
|
|
// ------------------------------------------------------------------------
|
342
|
|
|
|
343
|
|
|
/**
|
344
|
|
|
* AbstractRepository::count
|
345
|
|
|
*
|
346
|
|
|
* Application of Countable::count method to count the numbers of contained objects.
|
347
|
|
|
*
|
348
|
|
|
* @see http://php.net/manual/en/countable.count.php
|
349
|
|
|
* @return int The numbers of data on the storage.
|
350
|
|
|
*/
|
351
|
|
|
final public function count()
|
352
|
|
|
{
|
353
|
|
|
return (int)count($this->storage);
|
354
|
|
|
}
|
355
|
|
|
|
356
|
|
|
// ------------------------------------------------------------------------
|
357
|
|
|
|
358
|
|
|
/**
|
359
|
|
|
* AbstractDataStorage::serialize
|
360
|
|
|
*
|
361
|
|
|
* Application of Serializable::serialize method to serialize the data storage.
|
362
|
|
|
*
|
363
|
|
|
* @see http://php.net/manual/en/serializable.serialize.php
|
364
|
|
|
*
|
365
|
|
|
* @return string The string representation of the serialized data storage.
|
366
|
|
|
*/
|
367
|
|
|
final public function createSerialize($callback)
|
368
|
|
|
{
|
369
|
|
|
return call_user_func($callback, [$this->storage]);
|
370
|
|
|
}
|
371
|
|
|
|
372
|
|
|
// ------------------------------------------------------------------------
|
373
|
|
|
|
374
|
|
|
/**
|
375
|
|
|
* AbstractDataStorage::serialize
|
376
|
|
|
*
|
377
|
|
|
* Application of Serializable::serialize method to serialize the data storage.
|
378
|
|
|
*
|
379
|
|
|
* @see http://php.net/manual/en/serializable.serialize.php
|
380
|
|
|
*
|
381
|
|
|
* @return string The string representation of the serialized data storage.
|
382
|
|
|
*/
|
383
|
|
|
final public function serialize()
|
384
|
|
|
{
|
385
|
|
|
return serialize($this->storage);
|
386
|
|
|
}
|
387
|
|
|
|
388
|
|
|
// ------------------------------------------------------------------------
|
389
|
|
|
|
390
|
|
|
/**
|
391
|
|
|
* AbstractDataStorage::unserialize
|
392
|
|
|
*
|
393
|
|
|
* Application of Serializable::unserialize method to unserialize and construct the data storage.
|
394
|
|
|
*
|
395
|
|
|
* @see http://php.net/manual/en/serializable.unserialize.php
|
396
|
|
|
*
|
397
|
|
|
* @param string $serialized The string representation of the serialized data storage.
|
398
|
|
|
*
|
399
|
|
|
* @return void
|
400
|
|
|
*/
|
401
|
|
|
final public function unserialize($serialized)
|
402
|
|
|
{
|
403
|
|
|
$this->storage = unserialize($serialized);
|
404
|
|
|
}
|
405
|
|
|
|
406
|
|
|
// ------------------------------------------------------------------------
|
407
|
|
|
|
408
|
|
|
/**
|
409
|
|
|
* AbstractDataStorage::jsonSerialize
|
410
|
|
|
*
|
411
|
|
|
* Specify data which should be serialized to JSON
|
412
|
|
|
*
|
413
|
|
|
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
|
414
|
|
|
* @return mixed data which can be serialized by <b>json_encode</b>,
|
415
|
|
|
* which is a value of any type other than a resource.
|
416
|
|
|
* @since 5.4.0
|
417
|
|
|
*/
|
418
|
|
|
final public function jsonSerialize()
|
419
|
|
|
{
|
420
|
|
|
return $this->storage;
|
421
|
|
|
}
|
422
|
|
|
|
423
|
|
|
// ------------------------------------------------------------------------
|
424
|
|
|
|
425
|
|
|
/**
|
426
|
|
|
* AbstractRepository::getArrayCopy
|
427
|
|
|
*
|
428
|
|
|
* Gets a copy of the data storage.
|
429
|
|
|
*
|
430
|
|
|
* @return array Returns a copy of the data storage.
|
431
|
|
|
*/
|
432
|
|
|
public function getArrayCopy()
|
433
|
|
|
{
|
434
|
|
|
return $this->storage;
|
435
|
|
|
}
|
436
|
|
|
|
437
|
|
|
// ------------------------------------------------------------------------
|
438
|
|
|
|
439
|
|
|
/**
|
440
|
|
|
* AbstractProvider::getIterator
|
441
|
|
|
*
|
442
|
|
|
* Application of IteratorAggregate::getIterator method to retrieve an external iterator.
|
443
|
|
|
*
|
444
|
|
|
* @see http://php.net/manual/en/iteratoraggregate.getiterator.php
|
445
|
|
|
* @return \ArrayIterator
|
446
|
|
|
*/
|
447
|
|
|
final public function getIterator()
|
448
|
|
|
{
|
449
|
|
|
return new ArrayIterator($this->registry);
|
|
|
|
|
450
|
|
|
}
|
451
|
|
|
} |