@@ -22,12 +22,12 @@ class ArrayUtils
2222 /**
2323 * Applies the callback to the elements of the given associative array
2424 *
25- * @param callable $callback The callback function
26- * @param array $array An array to run through the callback function.
25+ * @param callable $callback The callback function
26+ * @param ArrayObject<int|string, mixed>|array $array An array to run through the callback function.
2727 *
2828 * @return array Resulting array
2929 */
30- public static function mapAssoc (callable $ callback , array $ array ): array
30+ public static function mapAssoc (callable $ callback , ArrayObject | array $ array ): array
3131 {
3232 $ r = [];
3333 foreach ($ array as $ key => $ value ) {
@@ -128,40 +128,46 @@ static function ($k, $v) use ($inner, $innerIsSafe) {
128128 * @see implode
129129 */
130130 public static function implodeFiltered (
131- string $ glue ,
132- array $ pieces ,
133- string $ filterCallback = __NAMESPACE__ . ' \ArrayUtils::safeFilterFunc ' ,
131+ array | string $ glue ,
132+ array | null $ pieces ,
133+ callable | null $ filterCallback = null ,
134134 int $ flag = 0
135135 ): string {
136+ $ filterCallback ??= fn ($ value ) => ArrayUtils::safeFilterFunc ($ value );
137+
136138 return self ::safeImplode ($ glue , self ::safeFilter ($ pieces , $ filterCallback , $ flag ));
137139 }
138140
139141 /**
140142 * Safe version of implode.
141143 *
142- * In addition fixes serialisation of float values.
143- *
144+ * In addition, fixes serialisation of float values.
144145 *
145146 */
146- public static function safeImplode ($ glue , array $ pieces ): string
147+ public static function safeImplode (array | string $ glue , array | null $ pieces ): string
147148 {
148- array_walk (
149- $ pieces ,
150- static function (&$ value ) {
151- $ value = TransformationUtils::floatToString ($ value );
152- }
153- );
149+ if (! is_null ($ pieces )) {
150+ array_walk (
151+ $ pieces ,
152+ static function (&$ value ) {
153+ $ value = TransformationUtils::floatToString ($ value );
154+ }
155+ );
156+ }
154157
155158 return implode ($ glue , $ pieces );
156159 }
157160
158161 /**
159162 * Implodes array values with escaping the glue character.
160163 *
161- *
162164 */
163- public static function escapedImplode (string $ glue , array $ pieces ): string
165+ public static function escapedImplode (string | array $ glue , array | null $ pieces ): string
164166 {
167+ if (is_null ($ pieces )) {
168+ return '' ;
169+ }
170+
165171 return implode (
166172 $ glue ,
167173 array_map (
@@ -206,18 +212,17 @@ protected static function safeFilterFunc(mixed $value): bool|int
206212 *
207213 * Uses "strlen" filter function by default, which treats non-null values (e.g. 0, false, etc) as non-empty.
208214 *
209- * @param callback|string $callback
210- *
211- *
212215 * @see array_filter
213216 * @see strlen
214217 */
215218 public static function safeFilter (
216- array $ input ,
217- callable |string $ callback = __NAMESPACE__ . ' \ArrayUtils::safeFilterFunc ' ,
219+ ? array $ input ,
220+ callable |null $ callback = null ,
218221 int $ flag = 0
219- ): array {
220- return array_filter ($ input , $ callback , $ flag );
222+ ): ?array {
223+ $ callback ??= fn ($ value ) => ArrayUtils::safeFilterFunc ($ value );
224+
225+ return is_null ($ input ) ? $ input : array_filter ($ input , $ callback , $ flag );
221226 }
222227
223228 /**
@@ -226,7 +231,7 @@ public static function safeFilter(
226231 * In case some of the missing/extra keys, the keys are sorted using alphabetic order. Ordered keys come first.
227232 *
228233 * @param ?array $array The associate array to order.
229- * @param array $orderArray The desired order of the keys.
234+ * @param array $orderArray The desired order of the keys.
230235 *
231236 */
232237 public static function sortByArray (?array $ array , array $ orderArray = []): ?array
@@ -256,15 +261,16 @@ public static function implodeUrl(array $urlParts): string
256261 /**
257262 * Commonly used util for building transformation URL
258263 *
259- * @param array $qualifiers
260- *
261264 * @return string The resulting string
262265 *
263266 * @internal
264267 */
265- public static function implodeActionQualifiers (...$ qualifiers ): string
268+ public static function implodeActionQualifiers (mixed ...$ qualifiers ): string
266269 {
267- $ serializedQualifiers = array_map ('strval ' , $ qualifiers );
270+ $ serializedQualifiers = [];
271+ foreach ($ qualifiers as $ item ) {
272+ $ serializedQualifiers [] = (string )$ item ;
273+ }
268274
269275 sort ($ serializedQualifiers );
270276
@@ -288,13 +294,13 @@ public static function implodeQualifierValues(...$qualifierValues): string
288294 /**
289295 * Gets a key from an array if exists, otherwise returns default.
290296 *
291- * @param ArrayObject|array $array The data array.
292- * @param int|array|string $key The key. Can be a simple key(string|int), an index array that allows accessing
297+ * @param mixed $array The data array.
298+ * @param int|array|string $key The key. Can be a simple key(string|int), an index array that allows accessing
293299 * nested values
294- * @param mixed|null $default The default value for the case when the key is not found.
300+ * @param mixed|null $default The default value for the case when the key is not found.
295301 *
296302 */
297- public static function get (ArrayObject | array $ array , int |array |string $ key , mixed $ default = null ): mixed
303+ public static function get (mixed $ array , int |array |string $ key , mixed $ default = null ): mixed
298304 {
299305 if (is_array ($ key )) {
300306 $ currLevel = &$ array ;
@@ -323,13 +329,12 @@ public static function get(ArrayObject|array $array, int|array|string $key, mixe
323329 /**
324330 * Pops a key from an array if exists, otherwise returns default
325331 *
326- * @param array $array Data array
327- * @param int|array|string $key key can be a simple key(string|int) or an array that allows accessing nested
328- * values
329- * @param mixed|null $default Default value for the case when key is not found
332+ * @param array $array Data array
333+ * @param int|string $key A simple key(string|int)
334+ * @param mixed|null $default Default value for the case when key is not found
330335 *
331336 */
332- public static function pop (array &$ array , int |array | string $ key , mixed $ default = null ): mixed
337+ public static function pop (array &$ array , int |string $ key , mixed $ default = null ): mixed
333338 {
334339 $ val = self ::get ($ array , $ key , $ default );
335340 unset($ array [$ key ]);
@@ -341,7 +346,7 @@ public static function pop(array &$array, int|array|string $key, mixed $default
341346 * Returns a subset of associative array whitelisted by an array of keys
342347 *
343348 * @param ?array $array Source array (associative or not)
344- * @param array $keys Simple array of keys to keep
349+ * @param array $keys Simple array of keys to keep
345350 *
346351 * @return ?array Resulting array
347352 */
@@ -363,7 +368,7 @@ public static function whitelist(?array $array, array $keys): ?array
363368 * Returns a subset of associative array with excluded keys specified by an array of keys
364369 *
365370 * @param ?array $array Source array (associative or not)
366- * @param array $blacklistedKeys Simple array of keys to be excluded
371+ * @param array $blacklistedKeys Simple array of keys to be excluded
367372 *
368373 * @return ?array Resulting array
369374 */
@@ -500,10 +505,11 @@ public static function addNonEmpty(array &$arr, int|string|null $key, mixed $val
500505 /**
501506 * Adds key-value pair to the associative array where value is taken from source array using the same key.
502507 *
503- * @param array & $resultingArr The target array.
504- * @param mixed $key The key to add
505- * @param array $sourceArray The source array
506- * @param mixed|null $defaultValue Fallback value, in case the key does not exist or is empty
508+ * @param array & $resultingArr The target array.
509+ * @param mixed $key The key to add
510+ * @param array|ArrayObject<int|string, mixed> $sourceArray The source array
511+ * @param mixed|null $defaultValue Fallback value, in case the key does not exist or is
512+ * empty
507513 *
508514 * @return array The resulting array
509515 *
@@ -512,7 +518,7 @@ public static function addNonEmpty(array &$arr, int|string|null $key, mixed $val
512518 public static function addNonEmptyFromOther (
513519 array &$ resultingArr ,
514520 mixed $ key ,
515- array $ sourceArray ,
521+ array | ArrayObject $ sourceArray ,
516522 mixed $ defaultValue = null
517523 ): array {
518524 return self ::addNonEmpty ($ resultingArr , $ key , self ::get ($ sourceArray , $ key , $ defaultValue ));
@@ -533,7 +539,12 @@ public static function mergeNonEmpty(...$arrays): array
533539 $ result = [];
534540
535541 foreach ($ arrays as $ array ) {
536- $ result = array_merge ($ result , self ::safeFilter ($ array ));
542+ if (! empty ($ array )) {
543+ $ filtered = self ::safeFilter ($ array );
544+ if ($ filtered ) {
545+ $ result = array_merge ($ result , $ filtered );
546+ }
547+ }
537548 }
538549
539550 return $ result ;
@@ -621,11 +632,11 @@ public static function convertToAssoc(array $array): array
621632 /**
622633 * Helper function for making a recursive array copy while cloning objects on the way.
623634 *
624- * @param array $array Source array
635+ * @param mixed $array Source array
625636 *
626- * @return array Recursive copy of the source array
637+ * @return mixed Recursive copy of the source array
627638 */
628- public static function deepCopy (array $ array ): array
639+ public static function deepCopy (mixed $ array ): mixed
629640 {
630641 if (! is_array ($ array )) {
631642 return $ array ;
@@ -647,10 +658,8 @@ public static function deepCopy(array $array): array
647658
648659 /**
649660 * Indicates whether all parameters are non-empty
650- *
651- *
652661 */
653- public static function all (...$ params ): bool
662+ public static function all (array ...$ params ): bool
654663 {
655664 foreach ($ params as $ param ) {
656665 if (empty ($ param )) {
0 commit comments