@@ -14,227 +14,227 @@ |
||
14 | 14 | * @subpackage Axolotl |
15 | 15 | */ |
16 | 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 | - public function __construct( array $elements = array(), array $config = array() ) { |
|
52 | - $this->parse_config( $this->config = $config ); |
|
53 | - |
|
54 | - foreach ( $elements as $element ) { |
|
55 | - $this->add( $element ); |
|
56 | - } |
|
57 | - } |
|
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 | - public function add( $element ) { |
|
69 | - if ( $this->model && is_array( $element ) ) { |
|
70 | - $element = new $this->model( $element ); |
|
71 | - } |
|
72 | - |
|
73 | - if ( $this->model && ! ( $element instanceof $this->model ) ) { |
|
74 | - throw new RuntimeException; |
|
75 | - } |
|
76 | - |
|
77 | - $this->elements[] = $element; |
|
78 | - |
|
79 | - return $this; |
|
80 | - } |
|
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 ) { |
|
90 | - if ( ! is_string( $index ) || ! is_numeric( $index ) || ! isset( $this->elements[ $index ] ) ) { |
|
91 | - foreach ( $this->elements as $key => $element ) { |
|
92 | - if ( $element === $index ) { |
|
93 | - $index = $key; |
|
94 | - break; |
|
95 | - } |
|
96 | - } |
|
97 | - } |
|
98 | - |
|
99 | - if ( isset( $this->elements[ $index ] ) ) { |
|
100 | - unset( $this->elements[ $index ] ); |
|
101 | - $this->elements = array_values( $this->elements ); |
|
102 | - } |
|
103 | - |
|
104 | - return $this; |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Fetches the element at the provided index. |
|
109 | - * |
|
110 | - * @param int $index |
|
111 | - * |
|
112 | - * @return mixed|null |
|
113 | - */ |
|
114 | - public function at( $index ) { |
|
115 | - return isset( $this->elements[ $index ] ) ? $this->elements[ $index ] : null; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * Maps over the Collection's elements, |
|
120 | - * |
|
121 | - * @param callable $callback |
|
122 | - * |
|
123 | - * @return Collection |
|
124 | - */ |
|
125 | - public function map( callable $callback ) { |
|
126 | - return new Collection( array_map( $callback, $this->elements ), $this->config ); |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Filters the Collection's elements. |
|
131 | - * |
|
132 | - * @param callable $callback |
|
133 | - * |
|
134 | - * @return Collection |
|
135 | - */ |
|
136 | - public function filter( callable $callback ) { |
|
137 | - return new Collection( array_filter( $this->elements, $callback ) ); |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Fetches a single element in the Collection by callback. |
|
142 | - * |
|
143 | - * @param callable $callback |
|
144 | - * |
|
145 | - * @return mixed|null |
|
146 | - */ |
|
147 | - public function find( callable $callback ) { |
|
148 | - foreach ( $this->elements as $element ) { |
|
149 | - if ( call_user_func( $callback, $element ) ) { |
|
150 | - return $element; |
|
151 | - } |
|
152 | - } |
|
153 | - |
|
154 | - return null; |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * {@inheritDoc} |
|
159 | - * |
|
160 | - * @return array |
|
161 | - */ |
|
162 | - public function serialize() { |
|
163 | - return array_map(function( $element ) { |
|
164 | - if ( $element instanceof Serializes ) { |
|
165 | - return $element->serialize(); |
|
166 | - } |
|
167 | - |
|
168 | - return $element; |
|
169 | - }, $this->elements); |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * Return the current element. |
|
174 | - * |
|
175 | - * @return mixed |
|
176 | - */ |
|
177 | - public function current() { |
|
178 | - return $this->at( $this->position ); |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * Move forward to next element. |
|
183 | - */ |
|
184 | - public function next() { |
|
185 | - $this->position ++; |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * Return the key of the current element. |
|
190 | - * |
|
191 | - * @return mixed |
|
192 | - */ |
|
193 | - public function key() { |
|
194 | - return $this->position; |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * Checks if current position is valid. |
|
199 | - * |
|
200 | - * @return bool |
|
201 | - */ |
|
202 | - public function valid() { |
|
203 | - return isset( $this->elements[ $this->position ] ); |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * Rewind the Iterator to the first element. |
|
208 | - */ |
|
209 | - public function rewind() { |
|
210 | - $this->position = 0; |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * Count elements of an object. |
|
215 | - * |
|
216 | - * @return int |
|
217 | - */ |
|
218 | - public function count() { |
|
219 | - return count( $this->elements ); |
|
220 | - } |
|
221 | - |
|
222 | - /** |
|
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 ) { |
|
230 | - if ( isset( $config['model'] ) ) { |
|
231 | - $model = $config['model']; |
|
232 | - |
|
233 | - if ( ! is_subclass_of( $model, 'Intraxia\Jaxion\Axolotl\Model' ) ) { |
|
234 | - throw new LogicException; |
|
235 | - } |
|
236 | - |
|
237 | - $this->model = $model; |
|
238 | - } |
|
239 | - } |
|
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 | + public function __construct( array $elements = array(), array $config = array() ) { |
|
52 | + $this->parse_config( $this->config = $config ); |
|
53 | + |
|
54 | + foreach ( $elements as $element ) { |
|
55 | + $this->add( $element ); |
|
56 | + } |
|
57 | + } |
|
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 | + public function add( $element ) { |
|
69 | + if ( $this->model && is_array( $element ) ) { |
|
70 | + $element = new $this->model( $element ); |
|
71 | + } |
|
72 | + |
|
73 | + if ( $this->model && ! ( $element instanceof $this->model ) ) { |
|
74 | + throw new RuntimeException; |
|
75 | + } |
|
76 | + |
|
77 | + $this->elements[] = $element; |
|
78 | + |
|
79 | + return $this; |
|
80 | + } |
|
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 ) { |
|
90 | + if ( ! is_string( $index ) || ! is_numeric( $index ) || ! isset( $this->elements[ $index ] ) ) { |
|
91 | + foreach ( $this->elements as $key => $element ) { |
|
92 | + if ( $element === $index ) { |
|
93 | + $index = $key; |
|
94 | + break; |
|
95 | + } |
|
96 | + } |
|
97 | + } |
|
98 | + |
|
99 | + if ( isset( $this->elements[ $index ] ) ) { |
|
100 | + unset( $this->elements[ $index ] ); |
|
101 | + $this->elements = array_values( $this->elements ); |
|
102 | + } |
|
103 | + |
|
104 | + return $this; |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Fetches the element at the provided index. |
|
109 | + * |
|
110 | + * @param int $index |
|
111 | + * |
|
112 | + * @return mixed|null |
|
113 | + */ |
|
114 | + public function at( $index ) { |
|
115 | + return isset( $this->elements[ $index ] ) ? $this->elements[ $index ] : null; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * Maps over the Collection's elements, |
|
120 | + * |
|
121 | + * @param callable $callback |
|
122 | + * |
|
123 | + * @return Collection |
|
124 | + */ |
|
125 | + public function map( callable $callback ) { |
|
126 | + return new Collection( array_map( $callback, $this->elements ), $this->config ); |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Filters the Collection's elements. |
|
131 | + * |
|
132 | + * @param callable $callback |
|
133 | + * |
|
134 | + * @return Collection |
|
135 | + */ |
|
136 | + public function filter( callable $callback ) { |
|
137 | + return new Collection( array_filter( $this->elements, $callback ) ); |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Fetches a single element in the Collection by callback. |
|
142 | + * |
|
143 | + * @param callable $callback |
|
144 | + * |
|
145 | + * @return mixed|null |
|
146 | + */ |
|
147 | + public function find( callable $callback ) { |
|
148 | + foreach ( $this->elements as $element ) { |
|
149 | + if ( call_user_func( $callback, $element ) ) { |
|
150 | + return $element; |
|
151 | + } |
|
152 | + } |
|
153 | + |
|
154 | + return null; |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * {@inheritDoc} |
|
159 | + * |
|
160 | + * @return array |
|
161 | + */ |
|
162 | + public function serialize() { |
|
163 | + return array_map(function( $element ) { |
|
164 | + if ( $element instanceof Serializes ) { |
|
165 | + return $element->serialize(); |
|
166 | + } |
|
167 | + |
|
168 | + return $element; |
|
169 | + }, $this->elements); |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * Return the current element. |
|
174 | + * |
|
175 | + * @return mixed |
|
176 | + */ |
|
177 | + public function current() { |
|
178 | + return $this->at( $this->position ); |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * Move forward to next element. |
|
183 | + */ |
|
184 | + public function next() { |
|
185 | + $this->position ++; |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * Return the key of the current element. |
|
190 | + * |
|
191 | + * @return mixed |
|
192 | + */ |
|
193 | + public function key() { |
|
194 | + return $this->position; |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * Checks if current position is valid. |
|
199 | + * |
|
200 | + * @return bool |
|
201 | + */ |
|
202 | + public function valid() { |
|
203 | + return isset( $this->elements[ $this->position ] ); |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * Rewind the Iterator to the first element. |
|
208 | + */ |
|
209 | + public function rewind() { |
|
210 | + $this->position = 0; |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * Count elements of an object. |
|
215 | + * |
|
216 | + * @return int |
|
217 | + */ |
|
218 | + public function count() { |
|
219 | + return count( $this->elements ); |
|
220 | + } |
|
221 | + |
|
222 | + /** |
|
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 ) { |
|
230 | + if ( isset( $config['model'] ) ) { |
|
231 | + $model = $config['model']; |
|
232 | + |
|
233 | + if ( ! is_subclass_of( $model, 'Intraxia\Jaxion\Axolotl\Model' ) ) { |
|
234 | + throw new LogicException; |
|
235 | + } |
|
236 | + |
|
237 | + $this->model = $model; |
|
238 | + } |
|
239 | + } |
|
240 | 240 | } |