Newer
Older
<?php
/**
* StudipArrayObject
*
* This ArrayObject is a rewrite of the implementation to fix
* issues with php's implementation of ArrayObject regarding cyclic references
* based on Zend\Stdlib\ArrayObject\PhpReferenceCompatibility
*
* @author André Noack <noack@data-quest.de>
*
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

Jan-Hendrik Willms
committed
class StudipArrayObject implements IteratorAggregate, ArrayAccess, Countable
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
{
/**
* Properties of the object have their normal functionality
* when accessed as list (var_dump, foreach, etc.).
*/
const STD_PROP_LIST = 1;
/**
* Entries can be accessed as properties (read and write).
*/
const ARRAY_AS_PROPS = 2;
/**
* @var array
*/
protected $storage;
/**
* @var int
*/
protected $flag;
/**
* @var string
*/
protected $iteratorClass;
/**
* @var array
*/
protected $protectedProperties;
/**
* Constructor
*
* @param array $input
* @param int $flags
* @param string $iteratorClass
*/
public function __construct($input = [], $flags = self::STD_PROP_LIST, $iteratorClass = 'ArrayIterator')
{
$this->setFlags($flags);
$this->storage = $input;
$this->setIteratorClass($iteratorClass);
$this->protectedProperties = array_keys(get_object_vars($this));
}
/**
* Returns whether the requested key exists
*
* @param mixed $key
* @return bool
*/
public function __isset($key)
{
if ($this->flag == self::ARRAY_AS_PROPS) {
return $this->offsetExists($key);
}
$this->validateKeyUsage($key);
return isset($this->$key);
}
/**
* Sets the value at the specified key to value
*
* @param mixed $key
* @param mixed $value
* @return void
*/
public function __set($key, $value)
{
if ($this->flag == self::ARRAY_AS_PROPS) {
$this->offsetSet($key, $value);
return;
}
$this->validateKeyUsage($key);
$this->$key = $value;
}
/**
* Unsets the value at the specified key
*
* @param mixed $key
* @return void
*/
public function __unset($key)
{
if ($this->flag == self::ARRAY_AS_PROPS) {
$this->offsetUnset($key);
return;
}
$this->validateKeyUsage($key);
unset($this->$key);
}
/**
* Returns the value at the specified key
*
* @param mixed $key
* @return mixed
*/
public function __get($key)
{
if ($this->flag == self::ARRAY_AS_PROPS) {
$ret = $this->offsetGet($key);
return $ret;
}
$this->validateKeyUsage($key);
return $this->$key;
}
/**
* Called when serializing an ArrayObject
*/
public function __serialize(): array
{
return get_object_vars($this);
}
/**
* Called when unserializing an ArrayObject
*/
public function __unserialize(array $data): void
{

Jan-Hendrik Willms
committed
$this->protectedProperties = array_keys(get_object_vars($this));
foreach ($data as $k => $v) {
switch ($k) {
case 'flag':
$this->setFlags($v);
break;
case 'storage':
$this->exchangeArray($v);
break;
case 'iteratorClass':
$this->setIteratorClass($v);
break;
case 'protectedProperties':
break;
default:
$this->__set($k, $v);
}
}
}
/**
* Appends the value
*
* @param mixed $value
* @return void
*/
public function append($value)
{
$this->storage[] = $value;
}
/**
* Sort the entries by value
*
* @return void
*/
public function asort()
{
asort($this->storage);
}
/**
* Get the number of public properties in the ArrayObject
*/
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
{
return count($this->storage);
}
/**
* Exchange the array for another one.
*
* @param array|ArrayObject $data
* @return array
*/
public function exchangeArray($data)
{
if (!is_array($data) && !is_object($data)) {
throw new InvalidArgumentException('Passed variable is not an array or object, using empty array instead');
}
if (is_object($data) && ($data instanceof self || $data instanceof \ArrayObject)) {
$data = $data->getArrayCopy();
}
if (!is_array($data)) {
$data = (array) $data;
}
$storage = $this->storage;
$this->storage = $data;
return $storage;
}
/**
* Creates a copy of the ArrayObject.
*
* @return array
*/
public function getArrayCopy()
{
return $this->storage;
}
/**
* Gets the behavior flags.
*
* @return int
*/
public function getFlags()
{
return $this->flag;
}
/**
* Create a new iterator from an ArrayObject instance
*/
public function getIterator(): Traversable
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
{
$class = $this->iteratorClass;
return new $class($this->storage);
}
/**
* Gets the iterator classname for the ArrayObject.
*
* @return string
*/
public function getIteratorClass()
{
return $this->iteratorClass;
}
/**
* Sort the entries by key
*
* @return void
*/
public function ksort()
{
ksort($this->storage);
}
/**
* Sort an array using a case insensitive "natural order" algorithm
*
* @return void
*/
public function natcasesort()
{
natcasesort($this->storage);
}
/**
* Sort entries using a "natural order" algorithm
*
* @return void
*/
public function natsort()
{
natsort($this->storage);
}
/**
* Returns whether the requested key exists
*
* @param mixed $key
*/
public function offsetExists($key): bool
{
return isset($this->storage[$key]);
}
/**
* Returns the value at the specified key
*
* @param mixed $key
*/
public function offsetGet($key): mixed
{
$ret = null;
if (!$this->offsetExists($key)) {
return $ret;
}
$ret = $this->storage[$key];
return $ret;
}
/**
* Sets the value at the specified key to value
*
* @param mixed $key
* @param mixed $value
*/
public function offsetSet($key, $value): void
$this->append($value);
return;
}
$this->storage[$key] = $value;
}
/**
* Unsets the value at the specified key
*
* @param mixed $key
*/
public function offsetUnset($key): void
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
{
if ($this->offsetExists($key)) {
unset($this->storage[$key]);
}
}
/**
* Sets the behavior flags
*
* @param int $flags
* @return void
*/
public function setFlags($flags)
{
$this->flag = $flags;
}
/**
* Sets the iterator classname for the ArrayObject
*
* @param string $class
* @return void
*/
public function setIteratorClass($class)
{
if (class_exists($class)) {
$this->iteratorClass = $class;
return ;
}
if (mb_strpos($class, '\\') === 0) {
$class = '\\' . $class;
if (class_exists($class)) {
$this->iteratorClass = $class;
return ;
}
}
throw new InvalidArgumentException('The iterator class does not exist');
}
/**
* Sort the entries with a user-defined comparison function and maintain key association
*
* @param callable $function
* @return void
*/
public function uasort($function)
{
if (is_callable($function)) {
uasort($this->storage, $function);
}
}
/**
* Sort the entries by keys using a user-defined comparison function
*
* @param callable $function
* @return void
*/
public function uksort($function)
{
if (is_callable($function)) {
uksort($this->storage, $function);
}
}
/**
* Validates whether the given key is a protected property.
*
* @param string $key The key to validate
* @throws InvalidArgumentException when key is invalid
*/
protected function validateKeyUsage($key)
{
if (in_array($key, $this->protectedProperties)) {
throw new InvalidArgumentException("{$key} is a protected property, use a different key");
}
}

Jan-Hendrik Willms
committed
/**
* Returns whether the given value is in the underlying array.
*/
public function contains($value): bool
{
return in_array($value, $this->storage);
}