@@ -15,157 +15,157 @@ |
||
15 | 15 | */ |
16 | 16 | class Type { |
17 | 17 | |
18 | - /** |
|
19 | - * Type to validate against. |
|
20 | - * |
|
21 | - * @var string |
|
22 | - */ |
|
23 | - private $type; |
|
24 | - |
|
25 | - /** |
|
26 | - * Type constructor. |
|
27 | - * |
|
28 | - * @param string $type |
|
29 | - */ |
|
30 | - public function __construct( $type ) { |
|
31 | - $this->type = $this->determine( $type ); |
|
32 | - } |
|
33 | - |
|
34 | - /** |
|
35 | - * Get validation type. |
|
36 | - * |
|
37 | - * @return string |
|
38 | - */ |
|
39 | - public function get_type() { |
|
40 | - return $this->type; |
|
41 | - } |
|
42 | - |
|
43 | - /** |
|
44 | - * Returns whether the type is an Axolotl model. |
|
45 | - * |
|
46 | - * @return bool |
|
47 | - */ |
|
48 | - public function is_model() { |
|
49 | - if ( ! class_exists( $this->type ) ) { |
|
50 | - return false; |
|
51 | - } |
|
52 | - |
|
53 | - $reflection = new ReflectionClass( $this->type ); |
|
54 | - return $reflection->isSubclassOf( 'Intraxia\Jaxion\Axolotl\Model' ); |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * Create a new model from the given data. |
|
59 | - * |
|
60 | - * @param array $data Data for the model. |
|
61 | - * |
|
62 | - * @return Model |
|
63 | - */ |
|
64 | - public function create_model( array $data ) { |
|
65 | - return new $this->type( $data ); |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * Validates an array of element. |
|
70 | - * |
|
71 | - * @param array $elements Elements to be validated. |
|
72 | - * |
|
73 | - * @throws InvalidArgumentException |
|
74 | - */ |
|
75 | - public function validate_elements( array $elements ) { |
|
76 | - foreach ( $elements as $element ) { |
|
77 | - $this->validate_element( $element ); |
|
78 | - } |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Validate whether the |
|
83 | - * |
|
84 | - * @param mixed $element Element to validate. |
|
85 | - * |
|
86 | - * @throws InvalidArgumentException |
|
87 | - */ |
|
88 | - public function validate_element( $element ) { |
|
89 | - $type = gettype( $element ); |
|
90 | - $callable = $this->type === 'callable'; |
|
91 | - $is_object = 'object' === $type; |
|
92 | - $loose_check = $this->type === 'object'; |
|
93 | - |
|
94 | - // callable must be callable |
|
95 | - if ( $callable && ! is_callable( $element ) ) { |
|
96 | - throw new InvalidArgumentException( 'Item must be callable' ); |
|
97 | - } |
|
98 | - |
|
99 | - // target isn't callable, object must be an instance of target |
|
100 | - if ( ! $loose_check && ! $callable && $is_object && ! is_a( $element, $this->type ) ) { |
|
101 | - throw new InvalidArgumentException( "Item is not type or subtype of $this->type" ); |
|
102 | - } |
|
103 | - |
|
104 | - // a non callable, non object type should match the target string |
|
105 | - if ( ! $callable && ! $is_object && $type !== $this->type ) { |
|
106 | - throw new InvalidArgumentException( "Item is not of type: $this->type" ); |
|
107 | - } |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Determine the type to validate against. |
|
112 | - * |
|
113 | - * @param string $type Type to determine. |
|
114 | - * @param bool $key_type Whether the type is for keys. |
|
115 | - * |
|
116 | - * @return string |
|
117 | - * |
|
118 | - * @throws InvalidArgumentException |
|
119 | - */ |
|
120 | - private function determine( $type, $key_type = false ) { |
|
121 | - if ( ! $key_type && $this->non_scalar_type_exists( $type ) ) { |
|
122 | - return $type; |
|
123 | - } |
|
124 | - |
|
125 | - if ( $scalar_type = $this->determine_scalar( $type ) ) { |
|
126 | - if ( $key_type && (in_array( $scalar_type, [ 'double', 'boolean' ] )) ) { |
|
127 | - throw new InvalidArgumentException( 'This type is not supported as a key.' ); |
|
128 | - } |
|
129 | - |
|
130 | - return $scalar_type; |
|
131 | - } |
|
132 | - |
|
133 | - throw new InvalidArgumentException( 'This type does not exist.' ); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Determines whether the given type exists. |
|
138 | - * |
|
139 | - * @param string $type Type to check. |
|
140 | - * |
|
141 | - * @return bool |
|
142 | - */ |
|
143 | - private function non_scalar_type_exists( $type ) { |
|
144 | - return class_exists( $type ) |
|
145 | - || interface_exists( $type ) |
|
146 | - || in_array( $type, [ 'array', 'object', 'callable' ] ); |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Returns the type if it's scalar, otherwise, returns null. |
|
151 | - * |
|
152 | - * @param string $type Type to check. |
|
153 | - * |
|
154 | - * @return string|null |
|
155 | - */ |
|
156 | - private function determine_scalar( $type ) { |
|
157 | - $synonyms = array( |
|
158 | - 'int' => 'integer', |
|
159 | - 'float' => 'double', |
|
160 | - 'bool' => 'boolean', |
|
161 | - ); |
|
162 | - |
|
163 | - if ( array_key_exists( $type, $synonyms ) ) { |
|
164 | - $type = $synonyms[ $type ]; |
|
165 | - } |
|
166 | - |
|
167 | - return in_array( $type, array( 'string', 'integer', 'double', 'boolean' ) ) ? |
|
168 | - $type : |
|
169 | - null; |
|
170 | - } |
|
18 | + /** |
|
19 | + * Type to validate against. |
|
20 | + * |
|
21 | + * @var string |
|
22 | + */ |
|
23 | + private $type; |
|
24 | + |
|
25 | + /** |
|
26 | + * Type constructor. |
|
27 | + * |
|
28 | + * @param string $type |
|
29 | + */ |
|
30 | + public function __construct( $type ) { |
|
31 | + $this->type = $this->determine( $type ); |
|
32 | + } |
|
33 | + |
|
34 | + /** |
|
35 | + * Get validation type. |
|
36 | + * |
|
37 | + * @return string |
|
38 | + */ |
|
39 | + public function get_type() { |
|
40 | + return $this->type; |
|
41 | + } |
|
42 | + |
|
43 | + /** |
|
44 | + * Returns whether the type is an Axolotl model. |
|
45 | + * |
|
46 | + * @return bool |
|
47 | + */ |
|
48 | + public function is_model() { |
|
49 | + if ( ! class_exists( $this->type ) ) { |
|
50 | + return false; |
|
51 | + } |
|
52 | + |
|
53 | + $reflection = new ReflectionClass( $this->type ); |
|
54 | + return $reflection->isSubclassOf( 'Intraxia\Jaxion\Axolotl\Model' ); |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * Create a new model from the given data. |
|
59 | + * |
|
60 | + * @param array $data Data for the model. |
|
61 | + * |
|
62 | + * @return Model |
|
63 | + */ |
|
64 | + public function create_model( array $data ) { |
|
65 | + return new $this->type( $data ); |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * Validates an array of element. |
|
70 | + * |
|
71 | + * @param array $elements Elements to be validated. |
|
72 | + * |
|
73 | + * @throws InvalidArgumentException |
|
74 | + */ |
|
75 | + public function validate_elements( array $elements ) { |
|
76 | + foreach ( $elements as $element ) { |
|
77 | + $this->validate_element( $element ); |
|
78 | + } |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Validate whether the |
|
83 | + * |
|
84 | + * @param mixed $element Element to validate. |
|
85 | + * |
|
86 | + * @throws InvalidArgumentException |
|
87 | + */ |
|
88 | + public function validate_element( $element ) { |
|
89 | + $type = gettype( $element ); |
|
90 | + $callable = $this->type === 'callable'; |
|
91 | + $is_object = 'object' === $type; |
|
92 | + $loose_check = $this->type === 'object'; |
|
93 | + |
|
94 | + // callable must be callable |
|
95 | + if ( $callable && ! is_callable( $element ) ) { |
|
96 | + throw new InvalidArgumentException( 'Item must be callable' ); |
|
97 | + } |
|
98 | + |
|
99 | + // target isn't callable, object must be an instance of target |
|
100 | + if ( ! $loose_check && ! $callable && $is_object && ! is_a( $element, $this->type ) ) { |
|
101 | + throw new InvalidArgumentException( "Item is not type or subtype of $this->type" ); |
|
102 | + } |
|
103 | + |
|
104 | + // a non callable, non object type should match the target string |
|
105 | + if ( ! $callable && ! $is_object && $type !== $this->type ) { |
|
106 | + throw new InvalidArgumentException( "Item is not of type: $this->type" ); |
|
107 | + } |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Determine the type to validate against. |
|
112 | + * |
|
113 | + * @param string $type Type to determine. |
|
114 | + * @param bool $key_type Whether the type is for keys. |
|
115 | + * |
|
116 | + * @return string |
|
117 | + * |
|
118 | + * @throws InvalidArgumentException |
|
119 | + */ |
|
120 | + private function determine( $type, $key_type = false ) { |
|
121 | + if ( ! $key_type && $this->non_scalar_type_exists( $type ) ) { |
|
122 | + return $type; |
|
123 | + } |
|
124 | + |
|
125 | + if ( $scalar_type = $this->determine_scalar( $type ) ) { |
|
126 | + if ( $key_type && (in_array( $scalar_type, [ 'double', 'boolean' ] )) ) { |
|
127 | + throw new InvalidArgumentException( 'This type is not supported as a key.' ); |
|
128 | + } |
|
129 | + |
|
130 | + return $scalar_type; |
|
131 | + } |
|
132 | + |
|
133 | + throw new InvalidArgumentException( 'This type does not exist.' ); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Determines whether the given type exists. |
|
138 | + * |
|
139 | + * @param string $type Type to check. |
|
140 | + * |
|
141 | + * @return bool |
|
142 | + */ |
|
143 | + private function non_scalar_type_exists( $type ) { |
|
144 | + return class_exists( $type ) |
|
145 | + || interface_exists( $type ) |
|
146 | + || in_array( $type, [ 'array', 'object', 'callable' ] ); |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * Returns the type if it's scalar, otherwise, returns null. |
|
151 | + * |
|
152 | + * @param string $type Type to check. |
|
153 | + * |
|
154 | + * @return string|null |
|
155 | + */ |
|
156 | + private function determine_scalar( $type ) { |
|
157 | + $synonyms = array( |
|
158 | + 'int' => 'integer', |
|
159 | + 'float' => 'double', |
|
160 | + 'bool' => 'boolean', |
|
161 | + ); |
|
162 | + |
|
163 | + if ( array_key_exists( $type, $synonyms ) ) { |
|
164 | + $type = $synonyms[ $type ]; |
|
165 | + } |
|
166 | + |
|
167 | + return in_array( $type, array( 'string', 'integer', 'double', 'boolean' ) ) ? |
|
168 | + $type : |
|
169 | + null; |
|
170 | + } |
|
171 | 171 | } |
@@ -9,318 +9,318 @@ |
||
9 | 9 | |
10 | 10 | interface Collection extends Iterator, Countable, Serializes { |
11 | 11 | |
12 | - /** |
|
13 | - * Returns the type of the collection. |
|
14 | - * |
|
15 | - * @return string |
|
16 | - */ |
|
17 | - public function get_type(); |
|
18 | - |
|
19 | - /** |
|
20 | - * Returns a collection with $element added. |
|
21 | - * |
|
22 | - * @param mixed $element |
|
23 | - * @return Collection |
|
24 | - * @throws InvalidArgumentException |
|
25 | - */ |
|
26 | - public function add( $element ); |
|
27 | - |
|
28 | - /** |
|
29 | - * Removes every element from the collection. |
|
30 | - * |
|
31 | - * @return Collection |
|
32 | - */ |
|
33 | - public function clear(); |
|
34 | - |
|
35 | - /** |
|
36 | - * Returns true if the collection contains any elements that satisfy |
|
37 | - * $condition, returns false if it contains none. |
|
38 | - * |
|
39 | - * @param callable $condition |
|
40 | - * @return bool |
|
41 | - */ |
|
42 | - public function contains( callable $condition ); |
|
43 | - |
|
44 | - /** |
|
45 | - * Returns the first element in the collection that satisfies |
|
46 | - * $condition, returns false if no such element exists. |
|
47 | - * |
|
48 | - * @param callable $condition |
|
49 | - * @return mixed |
|
50 | - */ |
|
51 | - public function find( callable $condition ); |
|
52 | - |
|
53 | - /** |
|
54 | - * Returns the index of the first element in the collection that satisfies |
|
55 | - * $condition, returns -1 if no such element exists. |
|
56 | - * |
|
57 | - * @param callable $condition |
|
58 | - * @return int |
|
59 | - */ |
|
60 | - public function find_index( callable $condition ); |
|
61 | - |
|
62 | - /** |
|
63 | - * Returns the element in the collection at $index. |
|
64 | - * |
|
65 | - * @param int $index Index to get element from. |
|
66 | - * @return mixed |
|
67 | - * @throws OutOfRangeException |
|
68 | - */ |
|
69 | - public function at( $index ); |
|
70 | - |
|
71 | - /** |
|
72 | - * Returns true if $index is within the collection's range and returns false |
|
73 | - * if it is not. |
|
74 | - * |
|
75 | - * @param int $index Index to check for existence. |
|
76 | - * @return bool |
|
77 | - * @throws InvalidArgumentException |
|
78 | - */ |
|
79 | - public function index_exists( $index ); |
|
80 | - |
|
81 | - /** |
|
82 | - * Returns the number of elements in the collection. |
|
83 | - * |
|
84 | - * @return int |
|
85 | - */ |
|
86 | - public function count(); |
|
87 | - |
|
88 | - /** |
|
89 | - * Returns a collection that only contains the elements which satisfy |
|
90 | - * $condition. |
|
91 | - * |
|
92 | - * @param callable $condition |
|
93 | - * @return Collection |
|
94 | - */ |
|
95 | - public function filter( callable $condition ); |
|
96 | - |
|
97 | - /** |
|
98 | - * Returns the last element in the collection that satisfies $condition, |
|
99 | - * returns false if no such element exists. |
|
100 | - * |
|
101 | - * @param callable $condition |
|
102 | - * @return mixed |
|
103 | - */ |
|
104 | - public function find_last( callable $condition ); |
|
105 | - |
|
106 | - /** |
|
107 | - * Returns the index of the last element in the collection that satisfies |
|
108 | - * $condition, returns -1 if no such element exists. |
|
109 | - * |
|
110 | - * @param callable $condition |
|
111 | - * @return int |
|
112 | - */ |
|
113 | - public function find_last_index( callable $condition ); |
|
114 | - |
|
115 | - /** |
|
116 | - * Returns a collection that contains the subset of elements ranging from the |
|
117 | - * index $start to $end. |
|
118 | - * |
|
119 | - * @param int $start Begining index to slice from. |
|
120 | - * @param int $end End index to slice to. |
|
121 | - * @return Collection |
|
122 | - * @throws InvalidArgumentException |
|
123 | - */ |
|
124 | - public function slice( $start, $end ); |
|
125 | - |
|
126 | - /** |
|
127 | - * Inserts $element at $index. |
|
128 | - * |
|
129 | - * @param int $index Index to start at. |
|
130 | - * @param mixed $element Element to insert. |
|
131 | - * @return Collection |
|
132 | - * @throws InvalidArgumentException |
|
133 | - * @throws OutOfRangeException |
|
134 | - */ |
|
135 | - public function insert( $index, $element ); |
|
136 | - |
|
137 | - /** |
|
138 | - * Inserts the range $elements at $index. |
|
139 | - * |
|
140 | - * @param int $index |
|
141 | - * @param array $elements |
|
142 | - * @return Collection |
|
143 | - * @throws OutOfRangeException |
|
144 | - */ |
|
145 | - public function insert_range( $index, array $elements ); |
|
146 | - |
|
147 | - /** |
|
148 | - * Removes all of the elements that satisfy $condition. |
|
149 | - * |
|
150 | - * @param callable $condition |
|
151 | - * @return Collection |
|
152 | - */ |
|
153 | - public function without( callable $condition ); |
|
154 | - |
|
155 | - /** |
|
156 | - * Removes the element at $index. |
|
157 | - * |
|
158 | - * @param int $index Index to remove. |
|
159 | - * @return Collection |
|
160 | - * @throws OutOfRangeException |
|
161 | - */ |
|
162 | - public function remove_at( $index ); |
|
163 | - |
|
164 | - /** |
|
165 | - * Reverses the order of the elements in the collection. |
|
166 | - * |
|
167 | - * @return Collection |
|
168 | - */ |
|
169 | - public function reverse(); |
|
170 | - |
|
171 | - /** |
|
172 | - * Sorts the elements in the collection using the user supplied comparison |
|
173 | - * function $callback. |
|
174 | - * |
|
175 | - * @param callable $callback |
|
176 | - * @return Collection |
|
177 | - */ |
|
178 | - public function sort( callable $callback ); |
|
179 | - |
|
180 | - /** |
|
181 | - * Returns an array containing the elements in the collection. |
|
182 | - * |
|
183 | - * @return array |
|
184 | - */ |
|
185 | - public function to_array(); |
|
186 | - |
|
187 | - /** |
|
188 | - * Iteratively reduces the collection to a single value using the callback |
|
189 | - * function $callable. |
|
190 | - * |
|
191 | - * @param callable $callable |
|
192 | - * @param null $initial |
|
193 | - * @return mixed |
|
194 | - */ |
|
195 | - public function reduce( callable $callable, $initial = null ); |
|
196 | - |
|
197 | - /** |
|
198 | - * Returns true if every element in the collection satisfies $condition, |
|
199 | - * returns false if not. |
|
200 | - * |
|
201 | - * @param callable $condition |
|
202 | - * @return bool |
|
203 | - */ |
|
204 | - public function every( callable $condition ); |
|
205 | - |
|
206 | - /** |
|
207 | - * Removes all of the elements in the collection starting at index $num. |
|
208 | - * |
|
209 | - * @param int $num Number of elements to drop. |
|
210 | - * @return Collection |
|
211 | - * @throws InvalidArgumentException |
|
212 | - */ |
|
213 | - public function drop( $num ); |
|
214 | - |
|
215 | - /** |
|
216 | - * Removes all of the elements in the collectioin between index 0 and $num. |
|
217 | - * |
|
218 | - * @param int $num Number of elements to drop. |
|
219 | - * @return Collection |
|
220 | - * @throws InvalidArgumentException |
|
221 | - */ |
|
222 | - public function drop_right( $num ); |
|
223 | - |
|
224 | - /** |
|
225 | - * Iteratively drops elements in the collection that satisfy $condition until |
|
226 | - * an element is encountered that does not satisfy $condition. |
|
227 | - * |
|
228 | - * @param callable $condition |
|
229 | - * @return Collection |
|
230 | - */ |
|
231 | - public function drop_while( callable $condition ); |
|
232 | - |
|
233 | - /** |
|
234 | - * Removes the first element in the collection. |
|
235 | - * |
|
236 | - * @return Collection |
|
237 | - * @throws InvalidArgumentException |
|
238 | - */ |
|
239 | - public function tail(); |
|
240 | - |
|
241 | - /** |
|
242 | - * Removes all of the elements in the collection starting at index $num. |
|
243 | - * |
|
244 | - * @param int $num Number of elements to take. |
|
245 | - * @return Collection |
|
246 | - * @throws InvalidArgumentException |
|
247 | - */ |
|
248 | - public function take( $num ); |
|
249 | - |
|
250 | - /** |
|
251 | - * Removes all of the elements in the collection before index $num. |
|
252 | - * |
|
253 | - * @param int $num Number of elements to take. |
|
254 | - * @return Collection |
|
255 | - * @throws InvalidArgumentException |
|
256 | - */ |
|
257 | - public function take_right( $num ); |
|
258 | - |
|
259 | - /** |
|
260 | - * Iterates through the collection until an element is encountered that does |
|
261 | - * not satisfy $condition, then drops all of the elements starting at that |
|
262 | - * index. |
|
263 | - * |
|
264 | - * @param callable $condition |
|
265 | - * @return Collection |
|
266 | - */ |
|
267 | - public function take_while( callable $condition ); |
|
268 | - |
|
269 | - /** |
|
270 | - * Applies the callback function $callable to each element in the collection. |
|
271 | - * |
|
272 | - * @param callable $callable |
|
273 | - */ |
|
274 | - public function each( callable $callable ); |
|
275 | - |
|
276 | - /** |
|
277 | - * Returns a new instance of the collection with the callback function |
|
278 | - * $callable applied to each element. |
|
279 | - * |
|
280 | - * @param callable $callable |
|
281 | - * @return Collection |
|
282 | - */ |
|
283 | - public function map( callable $callable ); |
|
284 | - |
|
285 | - /** |
|
286 | - * Iteratively reduces the collection to a single value using the callback |
|
287 | - * function $callable starting at the rightmost index. |
|
288 | - * |
|
289 | - * @param callable $callable |
|
290 | - * @param null $initial |
|
291 | - * @return mixed |
|
292 | - */ |
|
293 | - public function reduce_right( callable $callable, $initial = null ); |
|
294 | - |
|
295 | - /** |
|
296 | - * Randomly reorders the elements in the collection. |
|
297 | - * |
|
298 | - * @return Collection |
|
299 | - */ |
|
300 | - public function shuffle(); |
|
301 | - |
|
302 | - /** |
|
303 | - * Adds every member of $elements to the collection. |
|
304 | - * |
|
305 | - * @param array|Collection $elements Array of elements to merge. |
|
306 | - * @return Collection |
|
307 | - * @throws InvalidArgumentException |
|
308 | - */ |
|
309 | - public function merge( $elements ); |
|
310 | - |
|
311 | - /** |
|
312 | - * Get first element of the collection |
|
313 | - * |
|
314 | - * @return mixed |
|
315 | - * @throws OutOfBoundsException |
|
316 | - */ |
|
317 | - public function first(); |
|
318 | - |
|
319 | - /** |
|
320 | - * Get last element of the collection |
|
321 | - * |
|
322 | - * @return mixed |
|
323 | - * @throws OutOfBoundsException |
|
324 | - */ |
|
325 | - public function last(); |
|
12 | + /** |
|
13 | + * Returns the type of the collection. |
|
14 | + * |
|
15 | + * @return string |
|
16 | + */ |
|
17 | + public function get_type(); |
|
18 | + |
|
19 | + /** |
|
20 | + * Returns a collection with $element added. |
|
21 | + * |
|
22 | + * @param mixed $element |
|
23 | + * @return Collection |
|
24 | + * @throws InvalidArgumentException |
|
25 | + */ |
|
26 | + public function add( $element ); |
|
27 | + |
|
28 | + /** |
|
29 | + * Removes every element from the collection. |
|
30 | + * |
|
31 | + * @return Collection |
|
32 | + */ |
|
33 | + public function clear(); |
|
34 | + |
|
35 | + /** |
|
36 | + * Returns true if the collection contains any elements that satisfy |
|
37 | + * $condition, returns false if it contains none. |
|
38 | + * |
|
39 | + * @param callable $condition |
|
40 | + * @return bool |
|
41 | + */ |
|
42 | + public function contains( callable $condition ); |
|
43 | + |
|
44 | + /** |
|
45 | + * Returns the first element in the collection that satisfies |
|
46 | + * $condition, returns false if no such element exists. |
|
47 | + * |
|
48 | + * @param callable $condition |
|
49 | + * @return mixed |
|
50 | + */ |
|
51 | + public function find( callable $condition ); |
|
52 | + |
|
53 | + /** |
|
54 | + * Returns the index of the first element in the collection that satisfies |
|
55 | + * $condition, returns -1 if no such element exists. |
|
56 | + * |
|
57 | + * @param callable $condition |
|
58 | + * @return int |
|
59 | + */ |
|
60 | + public function find_index( callable $condition ); |
|
61 | + |
|
62 | + /** |
|
63 | + * Returns the element in the collection at $index. |
|
64 | + * |
|
65 | + * @param int $index Index to get element from. |
|
66 | + * @return mixed |
|
67 | + * @throws OutOfRangeException |
|
68 | + */ |
|
69 | + public function at( $index ); |
|
70 | + |
|
71 | + /** |
|
72 | + * Returns true if $index is within the collection's range and returns false |
|
73 | + * if it is not. |
|
74 | + * |
|
75 | + * @param int $index Index to check for existence. |
|
76 | + * @return bool |
|
77 | + * @throws InvalidArgumentException |
|
78 | + */ |
|
79 | + public function index_exists( $index ); |
|
80 | + |
|
81 | + /** |
|
82 | + * Returns the number of elements in the collection. |
|
83 | + * |
|
84 | + * @return int |
|
85 | + */ |
|
86 | + public function count(); |
|
87 | + |
|
88 | + /** |
|
89 | + * Returns a collection that only contains the elements which satisfy |
|
90 | + * $condition. |
|
91 | + * |
|
92 | + * @param callable $condition |
|
93 | + * @return Collection |
|
94 | + */ |
|
95 | + public function filter( callable $condition ); |
|
96 | + |
|
97 | + /** |
|
98 | + * Returns the last element in the collection that satisfies $condition, |
|
99 | + * returns false if no such element exists. |
|
100 | + * |
|
101 | + * @param callable $condition |
|
102 | + * @return mixed |
|
103 | + */ |
|
104 | + public function find_last( callable $condition ); |
|
105 | + |
|
106 | + /** |
|
107 | + * Returns the index of the last element in the collection that satisfies |
|
108 | + * $condition, returns -1 if no such element exists. |
|
109 | + * |
|
110 | + * @param callable $condition |
|
111 | + * @return int |
|
112 | + */ |
|
113 | + public function find_last_index( callable $condition ); |
|
114 | + |
|
115 | + /** |
|
116 | + * Returns a collection that contains the subset of elements ranging from the |
|
117 | + * index $start to $end. |
|
118 | + * |
|
119 | + * @param int $start Begining index to slice from. |
|
120 | + * @param int $end End index to slice to. |
|
121 | + * @return Collection |
|
122 | + * @throws InvalidArgumentException |
|
123 | + */ |
|
124 | + public function slice( $start, $end ); |
|
125 | + |
|
126 | + /** |
|
127 | + * Inserts $element at $index. |
|
128 | + * |
|
129 | + * @param int $index Index to start at. |
|
130 | + * @param mixed $element Element to insert. |
|
131 | + * @return Collection |
|
132 | + * @throws InvalidArgumentException |
|
133 | + * @throws OutOfRangeException |
|
134 | + */ |
|
135 | + public function insert( $index, $element ); |
|
136 | + |
|
137 | + /** |
|
138 | + * Inserts the range $elements at $index. |
|
139 | + * |
|
140 | + * @param int $index |
|
141 | + * @param array $elements |
|
142 | + * @return Collection |
|
143 | + * @throws OutOfRangeException |
|
144 | + */ |
|
145 | + public function insert_range( $index, array $elements ); |
|
146 | + |
|
147 | + /** |
|
148 | + * Removes all of the elements that satisfy $condition. |
|
149 | + * |
|
150 | + * @param callable $condition |
|
151 | + * @return Collection |
|
152 | + */ |
|
153 | + public function without( callable $condition ); |
|
154 | + |
|
155 | + /** |
|
156 | + * Removes the element at $index. |
|
157 | + * |
|
158 | + * @param int $index Index to remove. |
|
159 | + * @return Collection |
|
160 | + * @throws OutOfRangeException |
|
161 | + */ |
|
162 | + public function remove_at( $index ); |
|
163 | + |
|
164 | + /** |
|
165 | + * Reverses the order of the elements in the collection. |
|
166 | + * |
|
167 | + * @return Collection |
|
168 | + */ |
|
169 | + public function reverse(); |
|
170 | + |
|
171 | + /** |
|
172 | + * Sorts the elements in the collection using the user supplied comparison |
|
173 | + * function $callback. |
|
174 | + * |
|
175 | + * @param callable $callback |
|
176 | + * @return Collection |
|
177 | + */ |
|
178 | + public function sort( callable $callback ); |
|
179 | + |
|
180 | + /** |
|
181 | + * Returns an array containing the elements in the collection. |
|
182 | + * |
|
183 | + * @return array |
|
184 | + */ |
|
185 | + public function to_array(); |
|
186 | + |
|
187 | + /** |
|
188 | + * Iteratively reduces the collection to a single value using the callback |
|
189 | + * function $callable. |
|
190 | + * |
|
191 | + * @param callable $callable |
|
192 | + * @param null $initial |
|
193 | + * @return mixed |
|
194 | + */ |
|
195 | + public function reduce( callable $callable, $initial = null ); |
|
196 | + |
|
197 | + /** |
|
198 | + * Returns true if every element in the collection satisfies $condition, |
|
199 | + * returns false if not. |
|
200 | + * |
|
201 | + * @param callable $condition |
|
202 | + * @return bool |
|
203 | + */ |
|
204 | + public function every( callable $condition ); |
|
205 | + |
|
206 | + /** |
|
207 | + * Removes all of the elements in the collection starting at index $num. |
|
208 | + * |
|
209 | + * @param int $num Number of elements to drop. |
|
210 | + * @return Collection |
|
211 | + * @throws InvalidArgumentException |
|
212 | + */ |
|
213 | + public function drop( $num ); |
|
214 | + |
|
215 | + /** |
|
216 | + * Removes all of the elements in the collectioin between index 0 and $num. |
|
217 | + * |
|
218 | + * @param int $num Number of elements to drop. |
|
219 | + * @return Collection |
|
220 | + * @throws InvalidArgumentException |
|
221 | + */ |
|
222 | + public function drop_right( $num ); |
|
223 | + |
|
224 | + /** |
|
225 | + * Iteratively drops elements in the collection that satisfy $condition until |
|
226 | + * an element is encountered that does not satisfy $condition. |
|
227 | + * |
|
228 | + * @param callable $condition |
|
229 | + * @return Collection |
|
230 | + */ |
|
231 | + public function drop_while( callable $condition ); |
|
232 | + |
|
233 | + /** |
|
234 | + * Removes the first element in the collection. |
|
235 | + * |
|
236 | + * @return Collection |
|
237 | + * @throws InvalidArgumentException |
|
238 | + */ |
|
239 | + public function tail(); |
|
240 | + |
|
241 | + /** |
|
242 | + * Removes all of the elements in the collection starting at index $num. |
|
243 | + * |
|
244 | + * @param int $num Number of elements to take. |
|
245 | + * @return Collection |
|
246 | + * @throws InvalidArgumentException |
|
247 | + */ |
|
248 | + public function take( $num ); |
|
249 | + |
|
250 | + /** |
|
251 | + * Removes all of the elements in the collection before index $num. |
|
252 | + * |
|
253 | + * @param int $num Number of elements to take. |
|
254 | + * @return Collection |
|
255 | + * @throws InvalidArgumentException |
|
256 | + */ |
|
257 | + public function take_right( $num ); |
|
258 | + |
|
259 | + /** |
|
260 | + * Iterates through the collection until an element is encountered that does |
|
261 | + * not satisfy $condition, then drops all of the elements starting at that |
|
262 | + * index. |
|
263 | + * |
|
264 | + * @param callable $condition |
|
265 | + * @return Collection |
|
266 | + */ |
|
267 | + public function take_while( callable $condition ); |
|
268 | + |
|
269 | + /** |
|
270 | + * Applies the callback function $callable to each element in the collection. |
|
271 | + * |
|
272 | + * @param callable $callable |
|
273 | + */ |
|
274 | + public function each( callable $callable ); |
|
275 | + |
|
276 | + /** |
|
277 | + * Returns a new instance of the collection with the callback function |
|
278 | + * $callable applied to each element. |
|
279 | + * |
|
280 | + * @param callable $callable |
|
281 | + * @return Collection |
|
282 | + */ |
|
283 | + public function map( callable $callable ); |
|
284 | + |
|
285 | + /** |
|
286 | + * Iteratively reduces the collection to a single value using the callback |
|
287 | + * function $callable starting at the rightmost index. |
|
288 | + * |
|
289 | + * @param callable $callable |
|
290 | + * @param null $initial |
|
291 | + * @return mixed |
|
292 | + */ |
|
293 | + public function reduce_right( callable $callable, $initial = null ); |
|
294 | + |
|
295 | + /** |
|
296 | + * Randomly reorders the elements in the collection. |
|
297 | + * |
|
298 | + * @return Collection |
|
299 | + */ |
|
300 | + public function shuffle(); |
|
301 | + |
|
302 | + /** |
|
303 | + * Adds every member of $elements to the collection. |
|
304 | + * |
|
305 | + * @param array|Collection $elements Array of elements to merge. |
|
306 | + * @return Collection |
|
307 | + * @throws InvalidArgumentException |
|
308 | + */ |
|
309 | + public function merge( $elements ); |
|
310 | + |
|
311 | + /** |
|
312 | + * Get first element of the collection |
|
313 | + * |
|
314 | + * @return mixed |
|
315 | + * @throws OutOfBoundsException |
|
316 | + */ |
|
317 | + public function first(); |
|
318 | + |
|
319 | + /** |
|
320 | + * Get last element of the collection |
|
321 | + * |
|
322 | + * @return mixed |
|
323 | + * @throws OutOfBoundsException |
|
324 | + */ |
|
325 | + public function last(); |
|
326 | 326 | } |