1 | <?php |
||
14 | trait StructureOfElementsMethods { |
||
15 | /** |
||
16 | * @var array collection items storage. |
||
17 | */ |
||
18 | protected $elements; |
||
19 | |||
20 | ///region ----------------- DATA CLEANING METHODS ----------------- |
||
21 | |||
22 | /** |
||
23 | * Clear all data from the collection. |
||
24 | * |
||
25 | * @since 1.0 |
||
26 | */ |
||
27 | public function clear(): void { |
||
30 | |||
31 | /** |
||
32 | * Remove first occurrence of a specified element from the collection. |
||
33 | * |
||
34 | * @param mixed $element element to be removed. |
||
35 | * |
||
36 | * @since 1.0 |
||
37 | */ |
||
38 | public function remove($element): void { |
||
43 | |||
44 | /** |
||
45 | * Remove all occurrences of a specified element from the collection. |
||
46 | * |
||
47 | * @param mixed $element element to be removed. |
||
48 | * |
||
49 | * @since 1.0 |
||
50 | */ |
||
51 | public function removeAll($element): void { |
||
57 | |||
58 | /** |
||
59 | * Remove element at specified index. |
||
60 | * |
||
61 | * @param int|string $key element index. |
||
62 | * |
||
63 | * @since 1.0 |
||
64 | */ |
||
65 | public function removeAt($key): void { |
||
68 | |||
69 | ///endregion |
||
70 | /// |
||
71 | ////region ----------------- DATA MANIPULATION BY CALLABLE METHODS ----------------- |
||
72 | |||
73 | /** |
||
74 | * Apply a user function to every element of the collection. |
||
75 | * |
||
76 | * @param callable $do callable that takes on two parameters. |
||
77 | * The input parameter's value being the first, and the key/index second. |
||
78 | * Example: |
||
79 | * <code> |
||
80 | * $collection->onEach(function ($element, $key) { |
||
81 | * $element++; // won't change original collection |
||
82 | * }); |
||
83 | * </code> |
||
84 | * |
||
85 | * If callable needs to be working with the actual values of the collection, |
||
86 | * specify the first parameter of callable as a reference. Then, any changes made |
||
87 | * to those elements will be made in the original collection itself. |
||
88 | * Example: |
||
89 | * <code> |
||
90 | * $collection = Collection::of([1, 2, 3]); |
||
91 | * $collection->onEach(function (&$element, $key) { |
||
92 | * $element++; |
||
93 | * }); |
||
94 | * // $collection is equal to Collection::of([2, 3, 4]); |
||
95 | * </code> |
||
96 | * |
||
97 | * @return bool true on success or false on failure. |
||
98 | * |
||
99 | * @see onEachRecursive to run callbale on each element recursively. |
||
100 | * @since 1.0 |
||
101 | */ |
||
102 | public function onEach(callable $do): bool { |
||
105 | |||
106 | /** |
||
107 | * Apply a user function recursively to every element of the collection. |
||
108 | * |
||
109 | * @param callable $do callable that takes on two parameters. |
||
110 | * The input parameter's value being the first, and the key/index second. |
||
111 | * Example: |
||
112 | * <code> |
||
113 | * $collection->onEach(function ($element, $key) { |
||
114 | * $element++; // won't change original collection |
||
115 | * }); |
||
116 | * </code> |
||
117 | * |
||
118 | * If callable needs to be working with the actual values of the collection, |
||
119 | * specify the first parameter of callable as a reference. Then, any changes made |
||
120 | * to those elements will be made in the original collection itself. |
||
121 | * Example: |
||
122 | * <code> |
||
123 | * $collection = Collection::of([1, 2, 3]); |
||
124 | * $collection->onEachRecursive(function (&$element) { |
||
125 | * $element++; |
||
126 | * }); |
||
127 | * // $collection is equal to Collection::of([2, 3, 4]); |
||
128 | * </code> |
||
129 | * |
||
130 | * @return bool true on success or false on failure. |
||
131 | * |
||
132 | * @since 1.0 |
||
133 | */ |
||
134 | public function onEachRecursive(callable $do): bool { |
||
137 | |||
138 | /** |
||
139 | * Iterates over each value in the collection passing them to the callback function. |
||
140 | * If the callback function returns `true`, the current value passed to the new collection, |
||
141 | * if `false` value will won't be added to the new collection. |
||
142 | * Note: keys are preserved. |
||
143 | * |
||
144 | * Example: |
||
145 | * <code> |
||
146 | * $collection = Collection::of([1, 2, 3, 3]); |
||
147 | * // remove from the collection all elements equal to 3 |
||
148 | * $filteredCollection = $collection->filter(function ($element) { |
||
149 | * if ($element == 3) { |
||
150 | * return false; |
||
151 | * }; |
||
152 | * return true; |
||
153 | * }); |
||
154 | * // $filteredCollection is equal to Collection::of([1, 2]); |
||
155 | * </code> |
||
156 | * |
||
157 | * @param callable $by user function that take collection element as |
||
158 | * and input parameter. |
||
159 | * |
||
160 | * @return static new collection containing all the elements of original collection |
||
161 | * after applying the callback function to each element. |
||
162 | * |
||
163 | * @since 1.0 |
||
164 | */ |
||
165 | public function filter(callable $by): self { |
||
168 | |||
169 | /** |
||
170 | * Applies the callback to the elements of the collection. |
||
171 | * The return value of a callback function |
||
172 | * Example: |
||
173 | * <code> |
||
174 | * $collection = Collection::of([1, 2, 3]); |
||
175 | * // remove from the collection all elements equal to 3 |
||
176 | * $mappedCollection = $collection->map(function ($element) { |
||
177 | * return $element * $element; |
||
178 | * }); |
||
179 | * // $mappedCollection is equal to Collection::of([1, 4, 9]); |
||
180 | * </code> |
||
181 | * |
||
182 | * @param callable $by callback function to run for each element. |
||
183 | * |
||
184 | * @return static new collection containing all the elements of original collection |
||
185 | * after applying the callback function to each element. |
||
186 | * |
||
187 | * @since 1.0 |
||
188 | */ |
||
189 | public function map(callable $by): self { |
||
192 | |||
193 | /** |
||
194 | * Iteratively reduce the collection to a single value using a callback function |
||
195 | * |
||
196 | * Example: |
||
197 | * <code> |
||
198 | * $collection = Collection::of([1, 2, 3]); |
||
199 | * // remove from the collection all elements equal to 3 |
||
200 | * $reducedCollectionValue = $collection->reduce(function ($result, $element) { |
||
201 | * $result += $element; |
||
202 | * |
||
203 | * return $result; |
||
204 | * }); |
||
205 | * // reducedCollectionValue is equal to `6` |
||
206 | * </code> |
||
207 | * |
||
208 | * @param callback $by the callback function. |
||
209 | * @param mixed $withInitial [optional] if the optional initial is available, it will |
||
210 | * be used at the beginning of the process, or as a final result in case |
||
211 | * the collection is empty. |
||
212 | * </p |
||
213 | * @return mixed the resulting value. |
||
214 | * @since 1.0 |
||
215 | */ |
||
216 | public function reduce(callable $by, $withInitial = null) { |
||
219 | |||
220 | ///endregion |
||
221 | /// |
||
222 | ///region ----------------- DATA CHECK METHODS ----------------- |
||
223 | |||
224 | public function hasKey($key): bool { |
||
227 | |||
228 | public function has($element): bool { |
||
231 | |||
232 | public function hasSet($key): bool { |
||
235 | |||
236 | ///endregion |
||
237 | /// |
||
238 | ///region ----------------- DATA RETRIEVE METHODS ----------------- |
||
239 | |||
240 | /** |
||
241 | * Searches the collection for a given value and returns the corresponding key if successful. |
||
242 | * |
||
243 | * If element is found more than once, the first matching key is returned. To return the keys |
||
244 | * for all matching values, use {@link allKeysOf}. |
||
245 | * |
||
246 | * @param mixed $element value to look for at the collection. |
||
247 | * @param bool $strictTypeCheck determines if strict comparison (===) should be used during the search. |
||
248 | * |
||
249 | * @return false|int|string key of an element or `false` if element not found. |
||
250 | * @since 1.0 |
||
251 | */ |
||
252 | public function keyOf($element, bool $strictTypeCheck = true) { |
||
256 | |||
257 | /** |
||
258 | * Searches the collection for a given value and returns the last corresponding key if successful. |
||
259 | * |
||
260 | * @param mixed $element value to look for at the collection. |
||
261 | * @param bool $strictTypeCheck determines if strict comparison (===) should be used during the search. |
||
262 | * |
||
263 | * @return false|int|string key of an element or `false` if element not found. |
||
264 | * @since 1.0 |
||
265 | */ |
||
266 | public function lastKeyOf($element, bool $strictTypeCheck = true) { |
||
271 | |||
272 | /** |
||
273 | * Searches the collection for a given value and returns the corresponding keys if successful. |
||
274 | * |
||
275 | * @param mixed $element value to look for at the collection. |
||
276 | * @param bool $strictTypeCheck determines if strict comparison (===) should be used during the search. |
||
277 | * |
||
278 | * @return Contract\Struct\Collection collection of keys given element obtain. |
||
279 | */ |
||
280 | public function allKeysOf($element, bool $strictTypeCheck = true): Contract\Struct\Collection { |
||
283 | |||
284 | /** |
||
285 | * Counts all the values of collection. |
||
286 | * |
||
287 | * @return Collection an associative collection of values from the collection as |
||
288 | * keys and their count as value. |
||
289 | * |
||
290 | * @since 1.0 |
||
291 | */ |
||
292 | public function countValuesFrequency(): Contract\Struct\Collection { |
||
295 | |||
296 | /** |
||
297 | * Counts all the values of collection ignoring string values case. |
||
298 | * |
||
299 | * @return Collection an associative collection of values from the collection as |
||
300 | * keys and their count as value. |
||
301 | * |
||
302 | * @since 1.0 |
||
303 | */ |
||
304 | public function countValuesFrequencyIgnoringCase(): Contract\Struct\Collection { |
||
307 | |||
308 | /** |
||
309 | * Calculate the product of values in collection. |
||
310 | * |
||
311 | * @return int|float the product as an integer or float. |
||
312 | * |
||
313 | * @since 1.0 |
||
314 | */ |
||
315 | public function calculateProduct() { |
||
318 | |||
319 | ///endregion |
||
320 | /// |
||
321 | ///region ----------------- DATA MANIPULATION METHODS ----------------- |
||
322 | /** |
||
323 | * Return last element of the collection and remove the element from the |
||
324 | * collection. |
||
325 | * |
||
326 | * @return mixed first element of the collection |
||
327 | * |
||
328 | * @since 1.0 |
||
329 | */ |
||
330 | public function pop() { |
||
333 | |||
334 | /** |
||
335 | * Return first element of the collection and remove the element from the |
||
336 | * collection. |
||
337 | * |
||
338 | * @return mixed first element of the collection |
||
339 | * @since 1.0 |
||
340 | */ |
||
341 | public function shift() { |
||
344 | |||
345 | ///endregion |
||
346 | /// |
||
347 | ///region ----------------- VALUE OBJECT METHODS ----------------- |
||
348 | |||
349 | public function isEmpty(): bool { |
||
352 | |||
353 | public function isNull(): bool { |
||
356 | |||
357 | public function isEqualTo($value): bool { |
||
371 | ///endregion |
||
372 | } |
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.