1 | <?php |
||
16 | class Collection implements Countable, Iterator, Serializes { |
||
17 | /** |
||
18 | * Collection elements. |
||
19 | * |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $elements = array(); |
||
23 | |||
24 | /** |
||
25 | * Collection configuration. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $config = array(); |
||
30 | |||
31 | /** |
||
32 | * Models registered to the collection. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $model; |
||
37 | |||
38 | /** |
||
39 | * Where Collection is in loop. |
||
40 | * |
||
41 | * @var int |
||
42 | */ |
||
43 | protected $position = 0; |
||
44 | |||
45 | /** |
||
46 | * Collection constructor. |
||
47 | * |
||
48 | * @param array $elements |
||
49 | * @param array $config |
||
50 | */ |
||
51 | 60 | public function __construct( array $elements = array(), array $config = array() ) { |
|
58 | |||
59 | /** |
||
60 | * Adds a new element to the Collection. |
||
61 | * |
||
62 | * @param mixed $element |
||
63 | * |
||
64 | * @throws RuntimeException |
||
65 | * |
||
66 | * @return $this |
||
67 | */ |
||
68 | 27 | public function add( $element ) { |
|
81 | |||
82 | /** |
||
83 | * Removes an element by index or value. |
||
84 | * |
||
85 | * @param mixed $index |
||
86 | * |
||
87 | * @return $this |
||
88 | */ |
||
89 | public function remove( $index ) { |
||
106 | |||
107 | /** |
||
108 | * Fetches the element at the provided index. |
||
109 | * |
||
110 | * @param int $index |
||
111 | * |
||
112 | * @return mixed|null |
||
113 | */ |
||
114 | 12 | public function at( $index ) { |
|
117 | |||
118 | /** |
||
119 | * Maps over the Collection's elements, |
||
120 | * |
||
121 | * @param callable $callback |
||
122 | * |
||
123 | * @return Collection |
||
124 | */ |
||
125 | protected function map( callable $callback ) { |
||
128 | |||
129 | /** |
||
130 | * Filters the Collection's elements. |
||
131 | * |
||
132 | * @param callable $callback |
||
133 | * |
||
134 | * @return Collection |
||
135 | */ |
||
136 | public function filter( callable $callback ) { |
||
139 | |||
140 | /** |
||
141 | * Fetches a single element in the Collection by callback. |
||
142 | * |
||
143 | * @param callable $callback |
||
144 | * |
||
145 | * @return mixed|null |
||
146 | 9 | */ |
|
147 | 6 | public function find( callable $callback ) { |
|
148 | 3 | foreach ( $this->elements as $element ) { |
|
149 | if ( call_user_func( $callback, $element ) ) { |
||
150 | return $element; |
||
151 | 3 | } |
|
152 | 9 | } |
|
153 | |||
154 | return null; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * {@inheritDoc} |
||
159 | * |
||
160 | 3 | * @return array |
|
161 | 3 | */ |
|
162 | public function serialize() { |
||
163 | return array_map(function( $element ) { |
||
164 | if ( $element instanceof Serializes ) { |
||
165 | return $element->serialize(); |
||
166 | } |
||
167 | 3 | ||
168 | 3 | return $element; |
|
169 | 3 | }, $this->elements); |
|
170 | } |
||
171 | |||
172 | /** |
||
173 | * Return the current element. |
||
174 | * |
||
175 | * @return mixed |
||
176 | 3 | */ |
|
177 | 3 | public function current() { |
|
178 | return $this->at( $this->position ); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Move forward to next element. |
||
183 | */ |
||
184 | public function next() { |
||
185 | 3 | $this->position ++; |
|
186 | 3 | } |
|
187 | |||
188 | /** |
||
189 | * Return the key of the current element. |
||
190 | * |
||
191 | * @return mixed |
||
192 | 3 | */ |
|
193 | 3 | public function key() { |
|
196 | |||
197 | /** |
||
198 | * Checks if current position is valid. |
||
199 | * |
||
200 | * @return bool |
||
201 | 6 | */ |
|
202 | 6 | public function valid() { |
|
205 | |||
206 | /** |
||
207 | * Rewind the Iterator to the first element. |
||
208 | */ |
||
209 | public function rewind() { |
||
212 | 60 | ||
213 | 60 | /** |
|
214 | 15 | * Count elements of an object. |
|
215 | * |
||
216 | 15 | * @return int |
|
217 | 3 | */ |
|
218 | public function count() { |
||
221 | 12 | ||
222 | 57 | /** |
|
223 | * Parses the Collection config to set its properties. |
||
224 | * |
||
225 | * @param array $config |
||
226 | * |
||
227 | * @throws LogicException |
||
228 | */ |
||
229 | protected function parse_config( array $config ) { |
||
240 | } |
||
241 |