Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
80.51% |
95 / 118 |
|
81.48% |
22 / 27 |
CRAP | |
0.00% |
0 / 1 |
Base | |
80.51% |
95 / 118 |
|
81.48% |
22 / 27 |
81.06 | |
0.00% |
0 / 1 |
getFirst | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getLast | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
sortByName | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
sortByContactName | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
sortByCustomKey | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
sortByCustomStringKey | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
__clone | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
hasEntity | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
4 | |||
getEntity | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
4 | |||
addEntity | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
offsetSet | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
addData | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
4 | |||
addList | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getIds | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getIdsCsv | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getCsvForProperty | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
getCsvForPropertyList | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
6 | |||
withValueFor | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
withLessData | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
setJsonCompressLevel | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
getAsArray | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
jsonSerialize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__toString | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getResolveLevel | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setResolveLevel | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
withResolveLevel | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
chunk | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | /** |
4 | * @package Dldb |
5 | * @copyright BerlinOnline Stadtportal GmbH & Co. KG |
6 | **/ |
7 | |
8 | namespace BO\Zmsentities\Collection; |
9 | |
10 | use BO\Zmsentities\Helper\Sorter; |
11 | use BO\Zmsentities\Schema\Entity; |
12 | |
13 | /** |
14 | * @SuppressWarnings(NumberOfChildren) |
15 | * @SuppressWarnings(Public) |
16 | * @SuppressWarnings(Complexity) |
17 | * |
18 | */ |
19 | class Base extends \ArrayObject implements \JsonSerializable |
20 | { |
21 | const ENTITY_CLASS = ''; |
22 | |
23 | /** |
24 | * @var Int $resolveLevel indicator on data integrity |
25 | */ |
26 | protected $resolveLevel = null; |
27 | |
28 | public function getFirst() |
29 | { |
30 | $item = $this->getIterator()->current(); |
31 | return $item; |
32 | } |
33 | |
34 | public function getLast() |
35 | { |
36 | $copy = $this->getArrayCopy(); |
37 | $item = end($copy); |
38 | return $item; |
39 | } |
40 | |
41 | public function sortByName() |
42 | { |
43 | $this->uasort(function ($a, $b) { |
44 | return strcmp( |
45 | Sorter::toSortableString(ucfirst($a->name)), |
46 | Sorter::toSortableString(ucfirst($b->name)) |
47 | ); |
48 | }); |
49 | return $this; |
50 | } |
51 | |
52 | public function sortByContactName() |
53 | { |
54 | $this->uasort(function ($a, $b) { |
55 | return strcmp( |
56 | Sorter::toSortableString(ucfirst($a->contact['name'])), |
57 | Sorter::toSortableString(ucfirst($b->contact['name'])) |
58 | ); |
59 | }); |
60 | return $this; |
61 | } |
62 | |
63 | public function sortByCustomKey($key) |
64 | { |
65 | $this->uasort(function ($a, $b) use ($key) { |
66 | return ($a[$key] - $b[$key]); |
67 | }); |
68 | return $this; |
69 | } |
70 | |
71 | public function sortByCustomStringKey($key) |
72 | { |
73 | $this->uasort(function ($a, $b) use ($key) { |
74 | return strcmp( |
75 | Sorter::toSortableString(ucfirst($a[$key])), |
76 | Sorter::toSortableString(ucfirst($b[$key])) |
77 | ); |
78 | }); |
79 | return $this; |
80 | } |
81 | |
82 | public function __clone() |
83 | { |
84 | foreach ($this as $key => $property) { |
85 | $this[$key] = clone $property; |
86 | } |
87 | } |
88 | |
89 | public function hasEntity($primary) |
90 | { |
91 | foreach ($this as $entity) { |
92 | if (isset($entity->{$entity::PRIMARY}) && $primary == $entity->{$entity::PRIMARY}) { |
93 | return true; |
94 | } |
95 | } |
96 | return false; |
97 | } |
98 | |
99 | public function getEntity($primary) |
100 | { |
101 | foreach ($this as $entity) { |
102 | if (isset($entity->{$entity::PRIMARY}) && $primary == $entity->{$entity::PRIMARY}) { |
103 | return $entity; |
104 | } |
105 | } |
106 | return null; |
107 | } |
108 | |
109 | public function addEntity(\BO\Zmsentities\Schema\Entity $entity) |
110 | { |
111 | $this->offsetSet(null, $entity); |
112 | return $this; |
113 | } |
114 | |
115 | public function offsetSet($index, $value) |
116 | { |
117 | $className = $this::ENTITY_CLASS; |
118 | if (is_a($value, $className)) { |
119 | return parent::offsetSet($index, $value); |
120 | } elseif (is_array($value)) { |
121 | return parent::offsetSet($index, new $className($value)); |
122 | } else { |
123 | throw new \Exception('Invalid entity ' . get_class($value) . ' for collection ' . __CLASS__); |
124 | } |
125 | } |
126 | |
127 | public function addData($mergeData) |
128 | { |
129 | foreach ($mergeData as $item) { |
130 | if ($item instanceof Entity) { |
131 | if (null === $item->getResolveLevel()) { |
132 | $item->setResolveLevel($this->getResolveLevel()); |
133 | } |
134 | $this->addEntity($item); |
135 | } else { |
136 | $className = $this::ENTITY_CLASS; |
137 | $entity = new $className($item); |
138 | $entity->setResolveLevel($this->getResolveLevel()); |
139 | $this->addEntity($entity); |
140 | } |
141 | } |
142 | return $this; |
143 | } |
144 | |
145 | public function addList(Base $list) |
146 | { |
147 | foreach ($list as $item) { |
148 | $this->addEntity($item); |
149 | } |
150 | return $this; |
151 | } |
152 | |
153 | public function getIds() |
154 | { |
155 | $list = []; |
156 | foreach ($this as $entity) { |
157 | $list[] = $entity->id; |
158 | } |
159 | return $list; |
160 | } |
161 | |
162 | public function getIdsCsv() |
163 | { |
164 | return implode(',', $this->getIds()); |
165 | } |
166 | |
167 | public function getCsvForProperty($propertyName, $csvSeperator = ',') |
168 | { |
169 | $list = []; |
170 | foreach ($this as $entry) { |
171 | if ($entry->hasProperty($propertyName)) { |
172 | $list[] = $entry->getProperty($propertyName); |
173 | } |
174 | } |
175 | return implode($csvSeperator, $list); |
176 | } |
177 | |
178 | public function getCsvForPropertyList(array $propertyList, $propertySeperator = '', $csvSeperator = ',') |
179 | { |
180 | $list = []; |
181 | foreach ($this as $entry) { |
182 | $propertyConcat = ''; |
183 | foreach ($propertyList as $propertyName) { |
184 | if ($entry->hasProperty($propertyName)) { |
185 | if ($propertyConcat) { |
186 | $propertyConcat .= $propertySeperator; |
187 | } |
188 | $propertyConcat .= $entry->getProperty($propertyName); |
189 | } |
190 | } |
191 | if ($propertyConcat) { |
192 | $list[] = $propertyConcat; |
193 | } |
194 | } |
195 | return implode($csvSeperator, $list); |
196 | } |
197 | |
198 | /** |
199 | * Change a parameter on all entries |
200 | */ |
201 | public function withValueFor($param, $newValue) |
202 | { |
203 | $list = new static(); |
204 | foreach ($this as $entry) { |
205 | $list[] = $entry->withProperty($param, $newValue); |
206 | } |
207 | return $list; |
208 | } |
209 | |
210 | /** |
211 | * Reduce items data of dereferenced entities to a required minimum |
212 | * |
213 | */ |
214 | public function withLessData(array $keepArray = []) |
215 | { |
216 | $list = new static(); |
217 | foreach ($this as $key => $item) { |
218 | $list[$key] = $item->withLessData($keepArray); |
219 | } |
220 | return $list; |
221 | } |
222 | |
223 | public function setJsonCompressLevel($jsonCompressLevel) |
224 | { |
225 | foreach ($this as $item) { |
226 | $item->setJsonCompressLevel($jsonCompressLevel); |
227 | } |
228 | } |
229 | |
230 | public function getAsArray() |
231 | { |
232 | $array = []; |
233 | foreach ($this as $item) { |
234 | $array[] = $item; |
235 | } |
236 | |
237 | return $array; |
238 | } |
239 | |
240 | public function jsonSerialize() |
241 | { |
242 | return $this->getArrayCopy(); |
243 | } |
244 | |
245 | public function __toString() |
246 | { |
247 | $list = []; |
248 | foreach ($this as $item) { |
249 | $list[] = $item->__toString(); |
250 | } |
251 | return "[" . implode(',', $list) . "]"; |
252 | } |
253 | |
254 | /** |
255 | * @return Int |
256 | */ |
257 | public function getResolveLevel() |
258 | { |
259 | return $this->resolveLevel; |
260 | } |
261 | |
262 | /** |
263 | * @param Int $resolveLevel |
264 | * @return self |
265 | */ |
266 | public function setResolveLevel($resolveLevel) |
267 | { |
268 | $this->resolveLevel = $resolveLevel; |
269 | return $this; |
270 | } |
271 | |
272 | /** |
273 | * @param Int $resolveLevel |
274 | * @return self |
275 | */ |
276 | public function withResolveLevel($resolveLevel) |
277 | { |
278 | $collection = new static(); |
279 | foreach ($this as $entity) { |
280 | $reduced = $entity->withResolveLevel($resolveLevel); |
281 | if (null !== $reduced) { |
282 | $collection[] = $reduced; |
283 | } |
284 | } |
285 | return $collection; |
286 | } |
287 | |
288 | public function chunk($length) |
289 | { |
290 | $chunks = [new static()]; |
291 | $id = 0; |
292 | foreach ($this as $entry) { |
293 | if (! isset($chunks[floor($id / $length)])) { |
294 | $chunks[floor($id / $length)] = new static(); |
295 | } |
296 | |
297 | $chunks[floor($id / $length)][] = $entry; |
298 | |
299 | $id++; |
300 | } |
301 | |
302 | return $chunks; |
303 | } |
304 | } |