Skip to content

Commit fcd4c1a

Browse files
committed
feat(Functions): Added Array and string fns
1 parent 4a41e8d commit fcd4c1a

5 files changed

+191
-118
lines changed

Array_Extra.php

+13
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,17 @@ final public static function diff_assoc( array $input_array, array $keys ): arra
3939
final public static function slice_assoc( array $input_array, array $keys ) {
4040
return \array_intersect_key( $input_array, \array_flip( $keys ) );
4141
}
42+
43+
final public static function from_string( string|array $target, string $delim ): array {
44+
if ( \is_array( $target ) ) {
45+
return $target;
46+
}
47+
48+
return \array_values(
49+
\array_filter(
50+
\array_map( 'trim', \explode( $delim, $target ) ),
51+
static fn( $v ) => '' !== $v,
52+
),
53+
);
54+
}
4255
}

composer.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@
3131
"XWP\\Helper\\Functions\\": "."
3232
},
3333
"files": [
34-
"xwp-helper-fns.php",
34+
"xwp-helper-fns-arr.php",
3535
"xwp-helper-fns-num.php",
36-
"xwp-helper-fns-req.php"
36+
"xwp-helper-fns-req.php",
37+
"xwp-helper-fns.php"
3738
]
3839
}
3940
}

xwp-helper-fns-arr.php

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
/**
3+
* Array helper functions definition file.
4+
*
5+
* @package eXtended WordPress
6+
* @subpackage Helper\Functions
7+
*/
8+
9+
use XWP\Helper\Functions as f;
10+
11+
if ( ! function_exists( 'xwp_array_flatmap' ) ) :
12+
/**
13+
* Flattens and maps an array.
14+
*
15+
* @template T The type of the elements in the input array.
16+
* @template R The type of the elements in the returned array.
17+
*
18+
* @param callable(T): R $callback Function to apply to each element.
19+
* @param array<array-key, T> $input_array Array to flatten and map.
20+
*
21+
* @return array<array-key, R>
22+
*/
23+
function xwp_array_flatmap( callable $callback, array $input_array ) {
24+
return f\Array_Extra::flatmap( $callback, $input_array );
25+
}
26+
endif;
27+
28+
if ( ! function_exists( 'wp_array_flatmap' ) ) :
29+
/**
30+
* Flattens and maps an array.
31+
*
32+
* @template T The type of the elements in the input array.
33+
* @template R The type of the elements in the returned array.
34+
*
35+
* @param array<array-key, T>|callable(T): R $callback Function to apply to each element.
36+
* @param array<array-key, T>|callable(T): R $input_array Array to flatten and map.
37+
*
38+
* @return array<array-key, R>
39+
*/
40+
function wp_array_flatmap( callable|array $callback, array|callable $input_array ) {
41+
return is_array( $input_array )
42+
? xwp_array_flatmap( $callback, $input_array )
43+
: xwp_array_flatmap( $input_array, $callback );
44+
}
45+
endif;
46+
47+
if ( ! function_exists( 'wp_array_flatmap_assoc' ) ) :
48+
/**
49+
* Flatten and map an associative array of arrays.
50+
*
51+
* @template R
52+
* @template T — Applies the callback to the elements of the given arrays
53+
*
54+
* @param callable(T): R $callback Callback function to run for each element in each array.
55+
* @param array<string, <array<T>> $input The input array.
56+
* @param key-of<T> $key Key whose value will be used as the key for the returned array.
57+
* @param bool $unkey Optional. Whether to remove the key from the returned array. Default true.
58+
*
59+
* @return array<value-of<key-of<T>>, R> An array containing all the elements of arr1 after applying the callback function to each one.
60+
*/
61+
function wp_array_flatmap_assoc( callable $callback, array $input, string $key, bool $unkey = true ) {
62+
return f\Array_Extra::flatmap_assoc( $callback, $input, $key, $unkey );
63+
}
64+
endif;
65+
66+
if ( ! function_exists( 'wp_array_diff_assoc' ) ) :
67+
/**
68+
* Legacy function to extract a slice of an array not including the specified keys.
69+
*
70+
* @param array $input_array Input array.
71+
* @param array $keys Keys to exclude.
72+
*/
73+
function wp_array_diff_assoc( array $input_array, array $keys ) {
74+
return xwp_array_diff_assoc( $input_array, ...$keys );
75+
}
76+
endif;
77+
78+
if ( ! function_exists( 'xwp_array_diff_assoc' ) ) :
79+
/**
80+
* Extracts a slice of array not including the specified keys.
81+
*
82+
* @template T
83+
*
84+
* @param T $arr Input array.
85+
* @param string|array<string> ...$keys Keys to exclude.
86+
* @return T
87+
*/
88+
function xwp_array_diff_assoc( array $arr, array|string ...$keys ) {
89+
if ( is_array( $keys[0] ) ) {
90+
$keys = $keys[0];
91+
}
92+
93+
return f\Array_Extra::diff_assoc( $arr, $keys );
94+
}
95+
96+
endif;
97+
98+
if ( ! function_exists( 'wp_array_rekey' ) ) :
99+
/**
100+
* Rekey an array of arrays by a specific key.
101+
*
102+
* @param array<string, array<string, mixed>> $arr The input array.
103+
* @param string $key The key to rekey by.
104+
* @return array<string, array<string, mixed>> The rekeyed array.
105+
*/
106+
function wp_array_rekey( array $arr, string $key ): array {
107+
return f\Array_Extra::rekey( $arr, $key );
108+
}
109+
endif;
110+
111+
if ( ! function_exists( 'xwp_array_slice_assoc' ) ) :
112+
/**
113+
* Extracts a slice of an array.
114+
*
115+
* @template T The type of the elements in the input array.
116+
*
117+
* @param array<string, T> $input_array Input array.
118+
* @param string ...$keys Keys to include.
119+
* @return array<string, T> Array with only the keys specified.
120+
*/
121+
function xwp_array_slice_assoc( array $input_array, string ...$keys ) {
122+
return f\Array_Extra::slice_assoc( $input_array, $keys );
123+
}
124+
endif;
125+
126+
if ( ! function_exists( 'xwp_str_to_arr' ) ) :
127+
/**
128+
* Convert a string to an array.
129+
*
130+
* @param null|string|array<int,string> $target The string to convert.
131+
* @param null|string $delim Optional. The delimiter to use. Default is ','.
132+
* @return array<int,string>
133+
*/
134+
function xwp_str_to_arr( string|array|null $target = null, ?string $delim = null ): array {
135+
return f\Array_Extra::from_string( $target ?? array(), $delim ?? ',' );
136+
}
137+
endif;

xwp-helper-fns-num.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @subpackage Functions
77
*/
88

9-
if ( ! function_exists( 'xwp_is_float_str' ) ) :
9+
if ( ! function_exists( 'xwp_is_int_str' ) ) :
1010

1111
/**
1212
* Check if a string is a integer

xwp-helper-fns.php

+37-115
Original file line numberDiff line numberDiff line change
@@ -81,121 +81,6 @@ function wp_load_filesystem(
8181
}
8282
endif;
8383

84-
if ( ! function_exists( 'xwp_array_flatmap' ) ) :
85-
/**
86-
* Flattens and maps an array.
87-
*
88-
* @template T The type of the elements in the input array.
89-
* @template R The type of the elements in the returned array.
90-
*
91-
* @param callable(T): R $callback Function to apply to each element.
92-
* @param array<array-key, T> $input_array Array to flatten and map.
93-
*
94-
* @return array<array-key, R>
95-
*/
96-
function xwp_array_flatmap( callable $callback, array $input_array ) {
97-
return f\Array_Extra::flatmap( $callback, $input_array );
98-
}
99-
endif;
100-
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-
120-
if ( ! function_exists( 'wp_array_flatmap_assoc' ) ) :
121-
/**
122-
* Flatten and map an associative array of arrays.
123-
*
124-
* @template R
125-
* @template T — Applies the callback to the elements of the given arrays
126-
*
127-
* @param callable(T): R $callback Callback function to run for each element in each array.
128-
* @param array<string, <array<T>> $input The input array.
129-
* @param key-of<T> $key Key whose value will be used as the key for the returned array.
130-
* @param bool $unkey Optional. Whether to remove the key from the returned array. Default true.
131-
*
132-
* @return array<value-of<key-of<T>>, R> An array containing all the elements of arr1 after applying the callback function to each one.
133-
*/
134-
function wp_array_flatmap_assoc( callable $callback, array $input, string $key, bool $unkey = true ) {
135-
return f\Array_Extra::flatmap_assoc( $callback, $input, $key, $unkey );
136-
}
137-
endif;
138-
139-
if ( ! function_exists( 'wp_array_diff_assoc' ) ) :
140-
/**
141-
* Legacy function to extract a slice of an array not including the specified keys.
142-
*
143-
* @param array $input_array Input array.
144-
* @param array $keys Keys to exclude.
145-
*/
146-
function wp_array_diff_assoc( array $input_array, array $keys ) {
147-
return xwp_array_diff_assoc( $input_array, ...$keys );
148-
}
149-
endif;
150-
151-
if ( ! function_exists( 'xwp_array_diff_assoc' ) ) :
152-
/**
153-
* Extracts a slice of array not including the specified keys.
154-
*
155-
* @template T The type of the elements in the input array.
156-
*
157-
* @param array<string, T> $input_array Input array.
158-
* @param array<string>|string ...$keys Keys to exclude.
159-
* @return array<string, T> Array with the keys removed.
160-
*/
161-
function xwp_array_diff_assoc( array $input_array, string ...$keys ) {
162-
if ( is_array( $keys[0] ) ) {
163-
$keys = $keys[0];
164-
}
165-
166-
return f\Array_Extra::diff_assoc( $input_array, $keys );
167-
}
168-
169-
endif;
170-
171-
if ( ! function_exists( 'wp_array_rekey' ) ) :
172-
/**
173-
* Rekey an array of arrays by a specific key.
174-
*
175-
* @param array<string, array<string, mixed>> $arr The input array.
176-
* @param string $key The key to rekey by.
177-
* @return array<string, array<string, mixed>> The rekeyed array.
178-
*/
179-
function wp_array_rekey( array $arr, string $key ): array {
180-
return f\Array_Extra::rekey( $arr, $key );
181-
}
182-
endif;
183-
184-
if ( ! function_exists( 'xwp_array_slice_assoc' ) ) :
185-
/**
186-
* Extracts a slice of an array.
187-
*
188-
* @template T The type of the elements in the input array.
189-
*
190-
* @param array<string, T> $input_array Input array.
191-
* @param string ...$keys Keys to include.
192-
* @return array<string, T> Array with only the keys specified.
193-
*/
194-
function xwp_array_slice_assoc( array $input_array, string ...$keys ) {
195-
return f\Array_Extra::slice_assoc( $input_array, $keys );
196-
}
197-
endif;
198-
19984
if ( ! function_exists( 'xwp_deregister_blocks' ) ) :
20085
/**
20186
* Deregister all blocks.
@@ -273,3 +158,40 @@ function xwp_format_term_name( WP_Term|int|string|null|array|\WP_Error $term, ar
273158
return f\Term::format_hierarhical_name( $term, $args );
274159
}
275160
endif;
161+
162+
163+
if ( ! function_exists( 'xwp_str_to_bool' ) ) :
164+
/**
165+
* Convert a string to a boolean.
166+
*
167+
* @param string|bool|null $str The string to convert.
168+
* @return bool
169+
*/
170+
function xwp_str_to_bool( string|bool|null $str = '' ): bool {
171+
if ( is_bool( $str ) ) {
172+
return $str;
173+
}
174+
175+
if ( xwp_is_int_str( $str ) ) {
176+
return intval( $str ) > 0;
177+
}
178+
179+
return match ( strtolower( $str ) ) {
180+
'yes', 'true', 'on' => true,
181+
'no', 'false', 'off' => false,
182+
default => false,
183+
};
184+
}
185+
endif;
186+
187+
if ( ! function_exists( 'xwp_bool_to_str' ) ) :
188+
/**
189+
* Convert a boolean to a string.
190+
*
191+
* @param bool $boolean The boolean to convert.
192+
* @return 'yes'|'no'
193+
*/
194+
function xwp_bool_to_str( bool|string $boolean ): string {
195+
return xwp_str_to_bool( $boolean ) ? 'yes' : 'no';
196+
}
197+
endif;

0 commit comments

Comments
 (0)