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