Skip to content

Commit 1b2dab0

Browse files
committed
feat(Functions): Added xwp_parse_args
1 parent 93f6c92 commit 1b2dab0

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

xwp-helper-fns.php

+50-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,35 @@
88

99
use XWP\Helper\Functions as f;
1010

11+
if ( ! function_exists( 'xwp_parse_args' ) ) :
12+
13+
/**
14+
* Same as `wp_parse_args` but recursive.
15+
*
16+
* @param string|array|object $args Arguments to parse.
17+
* @param array $defaults Optional. Default values. Default empty array.
18+
* @return array<string,mixed>
19+
*/
20+
function xwp_parse_args( string|array|object $args, array $defaults = array() ): array {
21+
match ( true ) {
22+
is_object( $args ) => $parsed = get_object_vars( $args ),
23+
is_array( $args ) => $parsed = &$args,
24+
default => wp_parse_str( $args, $parsed ),
25+
};
26+
27+
$result = $defaults;
28+
29+
foreach ( $args as $k => $v ) {
30+
$result[ $k ] = is_array( $v ) && isset( $result[ $k ] ) && is_array( $result[ $k ] )
31+
? xwp_parse_args( $v, $result[ $k ] )
32+
: $v;
33+
}
34+
35+
return $result;
36+
}
37+
38+
endif;
39+
1140
if ( ! function_exists( 'xwp_wpfs' ) ) :
1241
/**
1342
* Loads the WordPress filesystem
@@ -52,7 +81,7 @@ function wp_load_filesystem(
5281
}
5382
endif;
5483

55-
if ( ! function_exists( 'wp_array_flatmap' ) ) :
84+
if ( ! function_exists( 'xwp_array_flatmap' ) ) :
5685
/**
5786
* Flattens and maps an array.
5887
*
@@ -64,11 +93,30 @@ function wp_load_filesystem(
6493
*
6594
* @return array<array-key, R>
6695
*/
67-
function wp_array_flatmap( callable $callback, array $input_array ) {
96+
function xwp_array_flatmap( callable $callback, array $input_array ) {
6897
return f\Array_Extra::flatmap( $callback, $input_array );
6998
}
7099
endif;
71100

101+
if ( ! function_exists( 'wp_array_flatmap' ) ) :
102+
/**
103+
* Flattens and maps an array.
104+
*
105+
* @template T The type of the elements in the input array.
106+
* @template R The type of the elements in the returned array.
107+
*
108+
* @param array<array-key, T>|callable(T): R $callback Function to apply to each element.
109+
* @param array<array-key, T>|callable(T): R $input_array Array to flatten and map.
110+
*
111+
* @return array<array-key, R>
112+
*/
113+
function wp_array_flatmap( callable|array $callback, array|callable $input_array ) {
114+
return is_array( $input_array )
115+
? xwp_array_flatmap( $callback, $input_array )
116+
: xwp_array_flatmap( $input_array, $callback );
117+
}
118+
endif;
119+
72120
if ( ! function_exists( 'wp_array_flatmap_assoc' ) ) :
73121
/**
74122
* Flatten and map an associative array of arrays.

0 commit comments

Comments
 (0)