Total Complexity | 7 |
Total Lines | 47 |
Duplicated Lines | 0 % |
Coverage | 50% |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
1 | <?php |
||
21 | trait CollectionTrait |
||
22 | { |
||
23 | |||
24 | protected static $collectionItemName = 'Item'; |
||
25 | private $items = []; |
||
26 | |||
27 | public function exist($index): bool |
||
28 | { |
||
29 | return key_exists($index, $this->items); |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * |
||
34 | * @param mixed $key |
||
35 | * @return mixed |
||
36 | * @throws \InvalidArgumentException |
||
37 | */ |
||
38 | public function get($key) |
||
39 | { |
||
40 | Assert::keyExists($this->items, $key, static::getItemName() . " with key `{$key}` not present in collection"); |
||
41 | return clone $this->items[$key]; |
||
42 | } |
||
43 | |||
44 | 2 | public function remove($key): void |
|
45 | { |
||
46 | 2 | Assert::keyExists($this->items, $key, static::getItemName() . " with key `{$key}` not present in collection."); |
|
47 | unset($this->items[$key]); |
||
48 | } |
||
49 | |||
50 | 2 | public function toArray() |
|
51 | { |
||
52 | 2 | return $this->items; |
|
53 | } |
||
54 | |||
55 | 2 | public function count(): int |
|
58 | } |
||
59 | |||
60 | 2 | public function getIterator(): \Traversable |
|
61 | { |
||
62 | 2 | yield from $this->items; |
|
63 | 2 | } |
|
64 | |||
65 | public static function getItemName(): string |
||
68 | } |
||
69 | |||
70 | } |
||
71 |