1 | <?php |
||
8 | class Collection |
||
9 | { |
||
10 | /** |
||
11 | * Collection items. |
||
12 | * |
||
13 | * @var array |
||
14 | */ |
||
15 | private $items = []; |
||
16 | |||
17 | /** |
||
18 | * Implementation namespace. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | private $classname; |
||
23 | |||
24 | /** |
||
25 | * Create a new collection instance. |
||
26 | * |
||
27 | * @param string|null $classname |
||
28 | * @param array $items |
||
29 | */ |
||
30 | 72 | public function __construct($classname = null, array $items = []) |
|
31 | { |
||
32 | 72 | $this->items = $items; |
|
33 | 72 | $this->classname = $classname; |
|
34 | 72 | } |
|
35 | |||
36 | /** |
||
37 | * Add a new item to collection. |
||
38 | * |
||
39 | * @param string $name |
||
40 | * @param mixed $value |
||
41 | * |
||
42 | * @return self |
||
43 | */ |
||
44 | 72 | public function put($name, $value) |
|
45 | { |
||
46 | 72 | if (is_object($value) && !$this->isValidObjectInstance($value)) { |
|
47 | 12 | throw new InvalidCollectionItemInstanceException(sprintf('%s must implement %s', $name, $this->classname)); |
|
48 | } |
||
49 | |||
50 | 60 | $this->items[$name] = $value; |
|
51 | |||
52 | 60 | return $this->items[$name]; |
|
53 | } |
||
54 | |||
55 | /** |
||
56 | * Verify's if the given object has a valid instance. |
||
57 | * |
||
58 | * @param object $object |
||
59 | * |
||
60 | * @return bool |
||
61 | */ |
||
62 | 69 | private function isValidObjectInstance($object) |
|
63 | { |
||
64 | 69 | if (!is_null($this->classname) && !$this->isInstanceOrSubclassOf($object)) { |
|
65 | 12 | return false; |
|
66 | } |
||
67 | |||
68 | 57 | return true; |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * Check object instance. |
||
73 | * |
||
74 | * @param object $object |
||
75 | * |
||
76 | * @return bool |
||
77 | */ |
||
78 | 69 | private function isInstanceOrSubclassOf($object) |
|
79 | { |
||
80 | 69 | return $object instanceof $this->classname || is_subclass_of($object, $this->classname); |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * Get all items from collection. |
||
85 | * |
||
86 | * @return array |
||
87 | */ |
||
88 | 15 | public function all() |
|
89 | { |
||
90 | 15 | return $this->items; |
|
91 | } |
||
92 | |||
93 | /** |
||
94 | * Find collection item by name. |
||
95 | * |
||
96 | * @param string $name |
||
97 | * |
||
98 | * @return mixed|null |
||
99 | */ |
||
100 | 33 | public function find($name) |
|
101 | { |
||
102 | 33 | if ($this->has($name)) { |
|
103 | 21 | return $this->items[$name]; |
|
104 | } |
||
105 | 12 | } |
|
106 | |||
107 | /** |
||
108 | * Check if the given item exists on collection. |
||
109 | * |
||
110 | * @param string $name |
||
111 | * |
||
112 | * @return bool |
||
113 | */ |
||
114 | 42 | public function has($name) |
|
115 | { |
||
116 | 42 | return array_key_exists($name, $this->items); |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * Find a specific collection item or throw's an exception. |
||
121 | * |
||
122 | * @param string $name |
||
123 | * |
||
124 | * @return mixed |
||
125 | */ |
||
126 | 12 | public function findOrFail($name) |
|
127 | { |
||
128 | 12 | $result = $this->find($name); |
|
129 | |||
130 | 12 | if (is_null($result)) { |
|
131 | 3 | throw new CollectionItemNotFoundException(sprintf('Collection item "%s" not found', $name)); |
|
132 | } |
||
133 | |||
134 | 9 | return $result; |
|
135 | } |
||
136 | |||
137 | /** |
||
138 | * Run a callback over each item. |
||
139 | * |
||
140 | * @param callable $callback |
||
141 | * |
||
142 | * @return self |
||
143 | */ |
||
144 | 3 | public function each(callable $callback) |
|
145 | { |
||
146 | 3 | if ($this->count()) { |
|
147 | 3 | array_walk($this->items, $callback); |
|
148 | 3 | } |
|
149 | |||
150 | 3 | return $this; |
|
151 | } |
||
152 | |||
153 | /** |
||
154 | * Remove an item from collection. |
||
155 | * |
||
156 | * @param string $name |
||
157 | * |
||
158 | * @return self |
||
159 | */ |
||
160 | 9 | public function remove($name) |
|
168 | |||
169 | /** |
||
170 | * Count total of items on collection. |
||
171 | * |
||
172 | * @return int |
||
173 | */ |
||
174 | 6 | public function count() |
|
175 | { |
||
176 | 6 | return count($this->items); |
|
177 | } |
||
178 | |||
179 | /** |
||
180 | * Get all items except for those with the specified keys. |
||
181 | * |
||
182 | * @param mixed $key |
||
183 | * |
||
184 | * @return static |
||
185 | */ |
||
186 | 3 | public function except($key) |
|
192 | |||
193 | /** |
||
194 | * Filter items by key name. |
||
195 | * |
||
196 | * @param array $keys |
||
197 | * |
||
198 | * @return array |
||
199 | */ |
||
200 | 6 | private function filterByKeys(array $keys) |
|
214 | |||
215 | /** |
||
216 | * Run a map over each item in the collection. |
||
217 | * |
||
218 | * @param callable $callback |
||
219 | * |
||
220 | * @return static |
||
221 | */ |
||
222 | 3 | public function map(callable $callback) |
|
229 | |||
230 | /** |
||
231 | * Get all items with the specified keys. |
||
232 | * |
||
233 | * @param mixed $key |
||
234 | * |
||
235 | * @return static |
||
236 | */ |
||
237 | 3 | public function only($key) |
|
243 | |||
244 | /** |
||
245 | * Filter the elements in the collection using a callback function. |
||
246 | * |
||
247 | * @param callable|null $callback |
||
248 | * |
||
249 | * @return static |
||
250 | */ |
||
251 | 3 | public function filter(callable $callback = null) |
|
259 | } |
||
260 |