Total Complexity | 8 |
Total Lines | 79 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
15 | class ItemBag implements \IteratorAggregate, \Countable |
||
16 | { |
||
17 | /** |
||
18 | * Item storage |
||
19 | * |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $items; |
||
24 | |||
25 | /** |
||
26 | * Constructor |
||
27 | * |
||
28 | * @param array $items An array of items |
||
29 | */ |
||
30 | 27 | public function __construct(array $items = array()) |
|
31 | { |
||
32 | 27 | $this->replace($items); |
|
33 | 27 | } |
|
34 | |||
35 | /** |
||
36 | * Return all the items |
||
37 | * |
||
38 | * |
||
39 | * @return array An array of items |
||
40 | */ |
||
41 | 15 | public function all() |
|
42 | { |
||
43 | 15 | return $this->items; |
|
44 | } |
||
45 | |||
46 | /** |
||
47 | * Replace the contents of this bag with the specified items |
||
48 | * |
||
49 | * |
||
50 | * @param array $items An array of items |
||
51 | */ |
||
52 | 27 | public function replace(array $items = array()) |
|
53 | { |
||
54 | 27 | $this->items = array(); |
|
55 | |||
56 | 27 | foreach ($items as $item) { |
|
57 | 12 | $this->add($item); |
|
58 | } |
||
59 | 27 | } |
|
60 | |||
61 | /** |
||
62 | * Add an item to the bag |
||
63 | * |
||
64 | * |
||
65 | * @param ItemInterface|array $item An existing item, or associative array of item parameters |
||
66 | */ |
||
67 | 27 | public function add($item) |
|
68 | { |
||
69 | 27 | if ($item instanceof ItemInterface) { |
|
70 | 15 | $this->items[] = $item; |
|
71 | } else { |
||
72 | 12 | $this->items[] = new Item($item); |
|
73 | } |
||
74 | 27 | } |
|
75 | |||
76 | /** |
||
77 | * Returns an iterator for items |
||
78 | * |
||
79 | * @return \ArrayIterator An \ArrayIterator instance |
||
80 | */ |
||
81 | 3 | public function getIterator(): \Traversable |
|
84 | } |
||
85 | |||
86 | /** |
||
87 | * Returns the number of items |
||
88 | * |
||
89 | * @return int The number of items |
||
90 | */ |
||
91 | 6 | public function count(): int |
|
94 | } |
||
95 | } |
||
96 |