1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* WebHemi. |
4
|
|
|
* |
5
|
|
|
* PHP version 7.1 |
6
|
|
|
* |
7
|
|
|
* @copyright 2012 - 2018 Gixx-web (http://www.gixx-web.com) |
8
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
9
|
|
|
* |
10
|
|
|
* @link http://www.gixx-web.com |
11
|
|
|
*/ |
12
|
|
|
declare(strict_types = 1); |
13
|
|
|
|
14
|
|
|
namespace WebHemi\Data\Entity; |
15
|
|
|
|
16
|
|
|
use ArrayAccess; |
17
|
|
|
use InvalidArgumentException; |
18
|
|
|
use Iterator; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class EntitySet. |
22
|
|
|
* |
23
|
|
|
* We have some strictness additions to the original ArrayAccess and Iterator interfaces. |
24
|
|
|
* A) The offset must be Integer. |
25
|
|
|
* B) The value must be an object that implements the EntityInterface. |
26
|
|
|
*/ |
27
|
|
|
class EntitySet implements ArrayAccess, Iterator |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $container = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Checks whether an offset exists. |
36
|
|
|
* |
37
|
|
|
* @param int $offset |
38
|
|
|
* @throws InvalidArgumentException |
39
|
|
|
* @return bool |
40
|
|
|
*/ |
41
|
2 |
|
public function offsetExists($offset) : bool |
42
|
|
|
{ |
43
|
2 |
|
if (!is_int($offset)) { |
|
|
|
|
44
|
1 |
|
throw new InvalidArgumentException( |
45
|
1 |
|
sprintf(__METHOD__.' requires parameter 1 to be integer, %s given.', gettype($offset)), |
46
|
1 |
|
1000 |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
return isset($this->container[$offset]); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns the value at an offset. |
55
|
|
|
* |
56
|
|
|
* @param int $offset |
57
|
|
|
* @throws InvalidArgumentException |
58
|
|
|
* @return null|EntityInterface |
59
|
|
|
*/ |
60
|
1 |
|
public function offsetGet($offset) |
61
|
|
|
{ |
62
|
1 |
|
if (!is_int($offset)) { |
|
|
|
|
63
|
1 |
|
throw new InvalidArgumentException( |
64
|
1 |
|
sprintf(__METHOD__.' requires parameter 1 to be integer, %s given.', gettype($offset)), |
65
|
1 |
|
1001 |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
return $this->container[$offset] ?? null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Sets a value for an offset. It appends it if offset is Null. |
74
|
|
|
* |
75
|
|
|
* @param null|int $offset |
76
|
|
|
* @param EntityInterface $value |
77
|
|
|
* @throws InvalidArgumentException |
78
|
|
|
*/ |
79
|
7 |
|
public function offsetSet($offset, $value) : void |
80
|
|
|
{ |
81
|
7 |
|
if (is_null($offset)) { |
82
|
6 |
|
$offset = empty($this->container) ? 0 : max(array_keys($this->container)) + 1; |
83
|
|
|
} |
84
|
|
|
|
85
|
7 |
|
if (!is_int($offset)) { |
|
|
|
|
86
|
1 |
|
throw new InvalidArgumentException( |
87
|
1 |
|
sprintf(__METHOD__.' requires parameter 1 to be integer, %s given.', gettype($offset)), |
88
|
1 |
|
1002 |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
7 |
|
if (!$value instanceof EntityInterface) { |
|
|
|
|
93
|
1 |
|
$valueType = is_object($value) ? get_class($value) : gettype($value); |
94
|
|
|
|
95
|
1 |
|
throw new InvalidArgumentException( |
96
|
1 |
|
sprintf(__METHOD__.' requires parameter 2 to be an instance of EntityInterface, %s given.', $valueType), |
97
|
1 |
|
1003 |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
7 |
|
$this->container[$offset] = $value; |
102
|
7 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Deletes a record from the container at an offset. It will not reorder the container. |
106
|
|
|
* |
107
|
|
|
* @param mixed $offset |
108
|
|
|
* @throws InvalidArgumentException |
109
|
|
|
*/ |
110
|
1 |
|
public function offsetUnset($offset) : void |
111
|
|
|
{ |
112
|
1 |
|
if (!is_int($offset)) { |
113
|
1 |
|
throw new InvalidArgumentException( |
114
|
1 |
|
sprintf(__METHOD__.' requires parameter 1 to be integer, %s given.', gettype($offset)), |
115
|
1 |
|
1004 |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
if ($this->offsetExists($offset)) { |
120
|
1 |
|
unset($this->container[$offset]); |
121
|
|
|
} |
122
|
1 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Rewinds the Iterator to the first element. |
126
|
|
|
*/ |
127
|
2 |
|
public function rewind() : void |
128
|
|
|
{ |
129
|
2 |
|
reset($this->container); |
130
|
2 |
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Returns the current element. |
134
|
|
|
* |
135
|
|
|
* @return mixed |
136
|
|
|
*/ |
137
|
2 |
|
public function current() |
138
|
|
|
{ |
139
|
2 |
|
return current($this->container); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Returns the key of the current element. |
144
|
|
|
* |
145
|
|
|
* @return int|mixed|null|string |
146
|
|
|
*/ |
147
|
1 |
|
public function key() |
148
|
|
|
{ |
149
|
1 |
|
return key($this->container); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Moves forward to next element. |
154
|
|
|
*/ |
155
|
2 |
|
public function next() : void |
156
|
|
|
{ |
157
|
2 |
|
next($this->container); |
158
|
2 |
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Checks if current position is valid. |
162
|
|
|
* |
163
|
|
|
* @return bool |
164
|
|
|
*/ |
165
|
2 |
|
public function valid() : bool |
166
|
|
|
{ |
167
|
2 |
|
return key($this->container) !== null; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Return the raw container |
172
|
|
|
* |
173
|
|
|
* @return array |
174
|
|
|
*/ |
175
|
2 |
|
public function toArray() : array |
176
|
|
|
{ |
177
|
2 |
|
return $this->container; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Merges another EntitySet into the current container. |
182
|
|
|
* |
183
|
|
|
* @param EntitySet $entitySet |
184
|
|
|
*/ |
185
|
2 |
|
public function merge(EntitySet $entitySet) : void |
186
|
|
|
{ |
187
|
2 |
|
$this->container = array_merge($this->container, $entitySet->toArray()); |
188
|
2 |
|
} |
189
|
|
|
} |
190
|
|
|
|