1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GuilleGF\PHPTools\Collection; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* An EntityCollection is a Collection implementation that wraps a regular PHP array. |
7
|
|
|
* |
8
|
|
|
* Warning: Using (un-)serialize() on a collection is not a supported use-case |
9
|
|
|
* and may break when we change the internals in the future. If you need to |
10
|
|
|
* serialize a collection use {@link toArray()} and reconstruct the collection |
11
|
|
|
* manually. |
12
|
|
|
* |
13
|
|
|
* @author Guillermo Gonzalez <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
abstract class EntityCollection implements \Countable, \Iterator |
16
|
|
|
{ |
17
|
|
|
/** @var array */ |
18
|
|
|
private $entities; |
19
|
|
|
/** @var bool */ |
20
|
|
|
protected $allowEntitiesChildren; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
|
|
abstract public static function entityClass(): string; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Collection constructor. |
29
|
|
|
* @param array $entities |
30
|
|
|
* @param bool $allowEntitiesChildren |
31
|
|
|
* @throws \Exception |
32
|
|
|
*/ |
33
|
44 |
|
public function __construct(array $entities, bool $allowEntitiesChildren = false) |
34
|
|
|
{ |
35
|
44 |
|
if (empty($entities)) { |
36
|
2 |
|
throw static::customEmptyException(); |
37
|
|
|
} |
38
|
|
|
|
39
|
42 |
|
$this->allowEntitiesChildren = $allowEntitiesChildren; |
40
|
|
|
|
41
|
42 |
|
foreach ($entities as $key => $entity) { |
42
|
42 |
|
$this->set($key, $entity); |
43
|
|
|
} |
44
|
34 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param $entity |
48
|
|
|
* @throws \Exception |
49
|
|
|
*/ |
50
|
2 |
|
public function add($entity) |
51
|
|
|
{ |
52
|
2 |
|
if (!$this->isValid($entity)) { |
53
|
1 |
|
throw static::customInvalidEntityException(); |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
$this->entities[] = $entity; |
57
|
1 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param $key |
61
|
|
|
* @param $entity |
62
|
|
|
* @throws \Exception |
63
|
|
|
*/ |
64
|
42 |
|
public function set($key, $entity) |
65
|
|
|
{ |
66
|
42 |
|
if (!$this->isValid($entity)) { |
67
|
9 |
|
throw static::customInvalidEntityException(); |
68
|
|
|
} |
69
|
|
|
|
70
|
34 |
|
$this->entities[$key] = $entity; |
71
|
34 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param $entity |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
42 |
|
public function isValid($entity): bool |
78
|
|
|
{ |
79
|
42 |
|
if (!is_object($entity)) return false; |
80
|
|
|
|
81
|
38 |
|
$entityClass = $this->allowEntitiesChildren ? get_parent_class($entity) : get_class($entity); |
82
|
38 |
|
if ($entityClass === static::entityClass()){ |
83
|
34 |
|
return true; |
84
|
|
|
} |
85
|
|
|
|
86
|
4 |
|
return false; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
3 |
|
public function keys(): array |
93
|
|
|
{ |
94
|
3 |
|
return array_keys($this->entities); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
3 |
|
public function values(): array |
101
|
|
|
{ |
102
|
3 |
|
return array_values($this->entities); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return mixed |
107
|
|
|
*/ |
108
|
3 |
|
public function first() |
109
|
|
|
{ |
110
|
3 |
|
return reset($this->entities); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return mixed |
115
|
|
|
*/ |
116
|
3 |
|
public function last() |
117
|
|
|
{ |
118
|
3 |
|
return end($this->entities); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
3 |
|
public function toArray(): array |
125
|
|
|
{ |
126
|
3 |
|
return $this->entities; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return mixed |
131
|
|
|
*/ |
132
|
11 |
|
public function current() |
133
|
|
|
{ |
134
|
11 |
|
return current($this->entities); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return mixed |
139
|
|
|
*/ |
140
|
13 |
|
public function next() |
141
|
|
|
{ |
142
|
13 |
|
return next($this->entities); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return mixed |
147
|
|
|
*/ |
148
|
9 |
|
public function key() |
149
|
|
|
{ |
150
|
9 |
|
return key($this->entities); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return bool |
155
|
|
|
*/ |
156
|
4 |
|
public function valid(): bool |
157
|
|
|
{ |
158
|
4 |
|
return key($this->entities) !== null; |
159
|
|
|
} |
160
|
|
|
|
161
|
4 |
|
public function rewind() |
162
|
|
|
{ |
163
|
4 |
|
reset($this->entities); |
164
|
4 |
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return int |
168
|
|
|
*/ |
169
|
5 |
|
public function count(): int |
170
|
|
|
{ |
171
|
5 |
|
return count($this->entities); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return \Exception |
176
|
|
|
*/ |
177
|
1 |
|
public static function customEmptyException(): \Exception |
178
|
|
|
{ |
179
|
1 |
|
return new \LengthException('Collection can not be empty'); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return \Exception |
184
|
|
|
*/ |
185
|
1 |
|
public static function customInvalidEntityException(): \Exception |
186
|
|
|
{ |
187
|
1 |
|
return new \UnexpectedValueException('Element is not valid instance of '.static::entityClass()); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|