Total Complexity | 11 |
Total Lines | 54 |
Duplicated Lines | 0 % |
Coverage | 91.3% |
Changes | 0 |
1 | <?php declare(strict_types=1); |
||
13 | class Collection implements Countable, IteratorAggregate |
||
14 | { |
||
15 | protected $values = []; |
||
16 | |||
17 | public function all() |
||
18 | { |
||
19 | return $this->values; |
||
20 | } |
||
21 | |||
22 | 26 | public function get(string $name) |
|
23 | { |
||
24 | 26 | if ($this->has($name)) { |
|
25 | 21 | return $this->values[$name]; |
|
26 | } else { |
||
27 | 5 | return $this->throwNotFound($name); |
|
28 | } |
||
29 | } |
||
30 | |||
31 | 26 | public function has(string $name): bool |
|
32 | { |
||
33 | 26 | return array_key_exists($name, $this->values); |
|
34 | } |
||
35 | |||
36 | 23 | public function set(string $name, $object) |
|
37 | { |
||
38 | 23 | $this->values[$name] = $object; |
|
39 | 23 | } |
|
40 | |||
41 | 2 | public function count(): int |
|
42 | { |
||
43 | 2 | return count($this->values); |
|
44 | } |
||
45 | |||
46 | 5 | public function select(callable $callback): array |
|
47 | { |
||
48 | 5 | $values = []; |
|
49 | |||
50 | 5 | foreach ($this->values as $key => $value) { |
|
51 | 5 | if ($callback($value, $key)) { |
|
52 | 5 | $values[$key] = $value; |
|
53 | } |
||
54 | } |
||
55 | |||
56 | 5 | return $values; |
|
57 | } |
||
58 | |||
59 | 13 | public function getIterator() |
|
62 | } |
||
63 | |||
64 | 1 | protected function throwNotFound(string $name) |
|
65 | { |
||
66 | 1 | throw new \InvalidArgumentException("Element \"$name\" not found in collection."); |
|
67 | } |
||
68 | } |
||
69 |