1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LM\Common\Model; |
6
|
|
|
|
7
|
|
|
use LM\Common\Type\TypeCheckerTrait; |
8
|
|
|
use Serializable; |
9
|
|
|
use UnexpectedValueException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Object that provides the features of an array as an object. |
13
|
|
|
* |
14
|
|
|
* @deprecated |
15
|
|
|
* @todo Delete? |
16
|
|
|
*/ |
17
|
|
|
class ArrayObject implements Serializable |
18
|
|
|
{ |
19
|
|
|
use TypeCheckerTrait; |
|
|
|
|
20
|
|
|
|
21
|
|
|
/** @var int */ |
22
|
|
|
private $currentItemIndex; |
23
|
|
|
|
24
|
|
|
/** @var mixed[] */ |
25
|
|
|
private $items; |
26
|
|
|
|
27
|
|
|
/** @var string */ |
28
|
|
|
private $type; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param mixed[] $items An array of items to initialise the objec with. The |
32
|
|
|
* constructor checks they are of the type $type. |
33
|
|
|
* @param string $type The type of the items of the array object. |
34
|
|
|
*/ |
35
|
|
|
public function __construct(array $items, string $type) |
36
|
|
|
{ |
37
|
|
|
$this->items = []; |
38
|
|
|
foreach ($items as $key => $item) { |
39
|
|
|
$this->checkType($item, $type); |
40
|
|
|
if ($this->isStringType($type)) { |
41
|
|
|
$this->items[$key] = $item; |
42
|
|
|
} elseif ($this->isIntegerType($type)) { |
43
|
|
|
$this->items[$key] = $item; |
44
|
|
|
} elseif ($this->isClassOrInterfaceName($type)) { |
45
|
|
|
$this->items[$key] = $item; |
46
|
|
|
} else { |
47
|
|
|
throw new UnexpectedValueException(); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
$this->currentItemIndex = 0; |
51
|
|
|
$this->type = $type; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Returns a copy of the object with a value added to it. |
56
|
|
|
* |
57
|
|
|
* @param mixed $value The item to add to the array object. |
58
|
|
|
* @todo Rename to append. |
59
|
|
|
* @todo Remove $type parameter. |
60
|
|
|
*/ |
61
|
|
|
public function add($value, string $type = null): self |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$this->checkType($value, $this->type); |
64
|
|
|
$items = $this->items; |
65
|
|
|
$items[] = $value; |
66
|
|
|
|
67
|
|
|
return new self($items, $this->type); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @todo Remove type parameter. |
72
|
|
|
*/ |
73
|
|
|
public function checkItemsType(string $type = null): self |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
foreach ($this->items as $item) { |
76
|
|
|
$this->checkType($item, $this->type); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @todo Delete |
84
|
|
|
* @todo Rename to add. |
85
|
|
|
* @todo Remove type parameter. |
86
|
|
|
*/ |
87
|
|
|
public function addWithkey($key, $value, string $type): self |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$this->checkType($value, $this->type); |
90
|
|
|
$items = $this->items; |
91
|
|
|
$items[$key] = $value; |
92
|
|
|
|
93
|
|
|
return new self($items, $this->type); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return bool Whether the array object's current item has a successor. |
98
|
|
|
*/ |
99
|
|
|
public function hasNextItem(): bool |
100
|
|
|
{ |
101
|
|
|
return $this->currentItemIndex + 1 < count($this->items); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $key The key of the item. |
106
|
|
|
* @todo Remove type parameter. |
107
|
|
|
*/ |
108
|
|
|
public function get($key, string $type = null) |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
$item = $this->items[$key]; |
111
|
|
|
$this->checkType($item, $this->type); |
112
|
|
|
|
113
|
|
|
return $item; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return $mixed The current item. |
|
|
|
|
118
|
|
|
*/ |
119
|
|
|
public function getCurrentItem(string $class) |
120
|
|
|
{ |
121
|
|
|
$currentItem = $this->items[$this->currentItemIndex]; |
122
|
|
|
$this->checkType($currentItem, $class); |
123
|
|
|
|
124
|
|
|
return $currentItem; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @todo Mutable object! |
129
|
|
|
*/ |
130
|
|
|
public function setToNextItem(): void |
131
|
|
|
{ |
132
|
|
|
$this->currentItemIndex++; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return int The number of items the object holds. |
137
|
|
|
*/ |
138
|
|
|
public function getSize(): int |
139
|
|
|
{ |
140
|
|
|
return count($this->items); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return array An array representation of the object. |
145
|
|
|
*/ |
146
|
|
|
public function toArray(string $type): array |
|
|
|
|
147
|
|
|
{ |
148
|
|
|
foreach ($this->items as $item) { |
149
|
|
|
$this->checkType($item, $this->type); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $this->items; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function serialize(): string |
156
|
|
|
{ |
157
|
|
|
return serialize([ |
158
|
|
|
$this->currentItemIndex, |
159
|
|
|
$this->items, |
160
|
|
|
$this->type, |
161
|
|
|
]); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function unserialize($serialized): void |
165
|
|
|
{ |
166
|
|
|
list( |
167
|
|
|
$this->currentItemIndex, |
168
|
|
|
$this->items, |
169
|
|
|
$this->type) = unserialize($serialized); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
This trait has been deprecated. The supplier of the trait has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the trait will be removed and what other trait to use instead.