1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Chadicus\Spl\DataStructures; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This class behaves the same as ArrayObject disallowing modifications. |
7
|
|
|
*/ |
8
|
|
|
final class ImmutableArrayObject extends \ArrayObject |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @see \ArrayObject::offsetSet() |
12
|
|
|
* |
13
|
|
|
* @param mixed $index The index being set. |
14
|
|
|
* @param mixed $newval The new value for the index. |
15
|
|
|
* |
16
|
|
|
* @return void |
17
|
|
|
* |
18
|
|
|
* @throws \LogicException Always thrown since object is immutable. |
19
|
|
|
*/ |
20
|
|
|
public function offsetSet($index, $newval) |
21
|
|
|
{ |
22
|
|
|
throw new \LogicException('Attempting to write to an immutable array'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @see \ArrayObject::offsetUnset() |
27
|
|
|
* |
28
|
|
|
* @param mixed $index The index being unset. |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
* |
32
|
|
|
* @throws \LogicException Always thrown since object is immutable. |
33
|
|
|
*/ |
34
|
|
|
public function offsetUnset($index) |
35
|
|
|
{ |
36
|
|
|
throw new \LogicException('Attempting to write to an immutable array'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @see \ArrayObject::append() |
41
|
|
|
* |
42
|
|
|
* @param mixed $value The value being appended. |
43
|
|
|
* |
44
|
|
|
* @return void |
45
|
|
|
* |
46
|
|
|
* @throws \LogicException Always thrown since object is immutable. |
47
|
|
|
*/ |
48
|
|
|
public function append($value) |
49
|
|
|
{ |
50
|
|
|
throw new \LogicException('Attempting to write to an immutable array'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @see \ArrayObject::exchangeArray() |
55
|
|
|
* |
56
|
|
|
* @param mixed $array The new array or object to exchange with the current array. |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
* |
60
|
|
|
* @throws \LogicException Always thrown since object is immutable. |
61
|
|
|
*/ |
62
|
|
|
public function exchangeArray($array) |
63
|
|
|
{ |
64
|
|
|
throw new \LogicException('Attempting to write to an immutable array'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Creates a new ImmutableArrayObject from the given mutable ArrayObject instance. |
69
|
|
|
* |
70
|
|
|
* @param \ArrayObject $arrayObject The mutable array object. |
71
|
|
|
* |
72
|
|
|
* @return ImmutableArrayObject |
73
|
|
|
*/ |
74
|
|
|
public static function createFromMutable(\ArrayObject $arrayObject) |
75
|
|
|
{ |
76
|
|
|
return new static($arrayObject->getArrayCopy(), $arrayObject->getFlags(), $arrayObject->getIteratorClass()); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|