Skip to content

Commit 0430cff

Browse files
committed
feat: Various features and tweaks
1 parent edd258d commit 0430cff

5 files changed

+251
-164
lines changed

Array_Extra.php

+54-25
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,70 @@
33
namespace XWP\Helper\Functions;
44

55
class Array_Extra {
6+
final public static function mergemap( callable $callback, array $arr ): array {
7+
$result = array();
8+
9+
foreach ( $arr as $value ) {
10+
$mapped = $callback( $value );
11+
12+
if ( ! \is_array( $mapped ) ) {
13+
continue;
14+
}
15+
16+
$result[] = $mapped;
17+
}
18+
19+
return \array_merge( ...\array_values( \array_filter( $result ) ) );
20+
}
21+
622
final public static function rekey( array $arr, string $key ): array {
7-
return \array_combine(
8-
\array_column( $arr, $key ),
9-
\array_map(
10-
static fn( $v ) => \array_diff_key( $v, array( $key => null ) ),
11-
$arr,
12-
),
13-
);
23+
$result = array();
24+
25+
foreach ( $arr as $item ) {
26+
if ( ! isset( $item[ $key ] ) ) {
27+
continue; // Skip items without the key
28+
}
29+
30+
$new = $item[ $key ];
31+
unset( $item[ $key ] ); // Remove the key
32+
$result[ $new ] = $item;
33+
}
34+
35+
return $result;
1436
}
1537

1638
final public static function flatmap( callable $callback, $arr ): array {
17-
return \array_merge( array(), ...\array_map( $callback, \array_values( $arr ) ) );
18-
}
39+
$res = array();
1940

20-
final public static function flatmap_assoc( callable $callback, array $assoc, string $key, $remove_key = true ): array {
21-
$arr_cb = $remove_key
22-
? static fn( $arr ) => \array_diff_key( $arr, array( $key => null ) )
23-
: static fn( $arr ) => $arr;
24-
25-
return \array_merge(
26-
array(),
27-
...\array_map(
28-
static fn( $v, $k ) => array( $k => \array_map( $callback, $arr_cb( $v ) ) ),
29-
$assoc,
30-
\array_column( $assoc, $key ),
31-
),
32-
);
41+
foreach ( $arr as $v ) {
42+
$mapped = $callback( $v );
43+
44+
$res[] = (array) $mapped;
45+
}
46+
47+
return \array_merge( ...$res );
3348
}
3449

3550
final public static function diff_assoc( array $input_array, array $keys ): array {
36-
return \array_diff_key( $input_array, \array_flip( $keys ) );
51+
return $keys
52+
? \array_diff_key( $input_array, \array_flip( $keys ) )
53+
: $input_array;
3754
}
3855

39-
final public static function slice_assoc( array $input_array, array $keys ) {
40-
return \array_intersect_key( $input_array, \array_flip( $keys ) );
56+
/**
57+
* Slice the array
58+
*
59+
* @template TArr of array
60+
* @template TKey of string
61+
*
62+
* @param TArr $input_array The input array.
63+
* @param array<int,TKey> $keys The keys to extract.
64+
* @return TArr
65+
*/
66+
final public static function slice_assoc( array $input_array, array $keys ): array {
67+
return $keys
68+
? \array_intersect_key( $input_array, \array_flip( $keys ) )
69+
: $input_array;
4170
}
4271

4372
final public static function from_string( string|array $target, string $delim ): array {

Request.php

+70-34
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ public static function should_load_rest_ns( string $space, ?string $route = null
2828
$space = \trailingslashit( $space );
2929
$known = \array_map( 'trailingslashit', $known );
3030

31-
/**
32-
* Known namespaces that we know are safe to not load if the request is not for them.
33-
* Namespaces not in this namespace should always be loaded, because we don't know if they won't be making another internal REST request to an unloaded namespace.
34-
*
35-
* @param array<string> $known_ns Known namespaces that we know are safe to not load if the request is not for them.
36-
* @param string $space The namespace to check.
37-
* @param string $route The REST route being checked.
38-
* @return array<string>
39-
*
40-
* @since 1.16.0
41-
*/
31+
/**
32+
* Known namespaces that we know are safe to not load if the request is not for them.
33+
* Namespaces not in this namespace should always be loaded, because we don't know if they won't be making another internal REST request to an unloaded namespace.
34+
*
35+
* @param array<string> $known_ns Known namespaces that we know are safe to not load if the request is not for them.
36+
* @param string $space The namespace to check.
37+
* @param string $route The REST route being checked.
38+
* @return array<string>
39+
*
40+
* @since 1.16.0
41+
*/
4242
foreach ( \apply_filters( 'xwp_known_rest_namespaces', $known, $space, $route ) as $k ) {
4343
if ( \str_starts_with( $route, $k ) ) {
4444
return true;
@@ -47,18 +47,18 @@ public static function should_load_rest_ns( string $space, ?string $route = null
4747

4848
$load = \str_starts_with( $route, $space );
4949

50-
/**
51-
* Filters whether a namespace should be loaded.
52-
*
53-
* @param bool $load True if the namespace should be loaded, false otherwise.
54-
* @param string $space The namespace to check.
55-
* @param string $route The REST route being checked.
56-
* @param array $known Known namespaces that we know are safe to not load if the request is not for them.
57-
* @return bool
58-
*
59-
* @since 1.16.0
60-
*/
61-
return \apply_filters( 'xwp_rest_can_load_namespace', $load, $space, $route, $known );
50+
/**
51+
* Filters whether a namespace should be loaded.
52+
*
53+
* @param bool $load True if the namespace should be loaded, false otherwise.
54+
* @param string $space The namespace to check.
55+
* @param string $route The REST route being checked.
56+
* @param array $known Known namespaces that we know are safe to not load if the request is not for them.
57+
* @return bool
58+
*
59+
* @since 1.16.0
60+
*/
61+
return \apply_filters( 'xwp_rest_can_load_namespace', $load, $space, $route, $known );
6262
}
6363

6464
/**
@@ -93,7 +93,38 @@ public static function uclean( $input ) {
9393
* @return mixed The fetched value.
9494
*/
9595
private static function fetch_var( &$val, $def = null ) {
96-
return self::uclean( $val ?? $def );
96+
$retval = self::uclean( $val ?? $def );
97+
$valtype = \gettype( $retval );
98+
$deftype = \gettype( $def );
99+
100+
if ( 'NULL' === $deftype || $valtype === $deftype ) {
101+
return $retval;
102+
}
103+
104+
return match ( $deftype ) {
105+
'boolean' => \xwp_str_to_bool( $retval ),
106+
'integer' => \intval( $retval ),
107+
'double' => \floatval( $retval ),
108+
'float' => \floatval( $retval ),
109+
'real' => \floatval( $retval ),
110+
'string' => \strval( $retval ),
111+
'array' => \is_array( $retval ) ? \xwp_parse_args( $retval, $def ) : $def,
112+
default => $retval,
113+
};
114+
}
115+
116+
/**
117+
* Fetch, unslash, and clean an array.
118+
*
119+
* @param array<string,mixed> $arr The array to fetch.
120+
* @param array<string,mixed> $def The default values.
121+
* @return array<string,mixed>
122+
*/
123+
private static function fetch_arr( array &$arr, array $def = array() ): array {
124+
return \xwp_array_slice_assoc(
125+
\xwp_parse_args( self::uclean( $arr ), $def ),
126+
...\array_keys( $def ),
127+
);
97128
}
98129

99130
/**
@@ -165,46 +196,51 @@ public static function fetch_files_var( $key, $def = null ) {
165196
/**
166197
* Fetch `$_GET` superglobal array.
167198
*
199+
* @param array<string, mixed> $def Default values.
168200
* @return array<string, mixed>
169201
*/
170-
public static function fetch_get_arr() {
171-
return self::fetch_var( $_GET, array() );
202+
public static function fetch_get_arr( array $def = array() ): array {
203+
return self::fetch_arr( $_GET, $def );
172204
}
173205

174206
/**
175207
* Fetch `$_POST` superglobal array.
176208
*
209+
* @param array<string, mixed> $def Default values.
177210
* @return array<string, mixed>
178211
*/
179-
public static function fetch_post_arr() {
180-
return self::fetch_var( $_POST, array() );
212+
public static function fetch_post_arr( array $def = array() ) {
213+
return self::fetch_arr( $_POST, $def );
181214
}
182215

183216
/**
184217
* Fetch `$_REQUEST` superglobal array.
185218
*
219+
* @param array<string, mixed> $def Default values.
186220
* @return array<string, mixed>
187221
*/
188-
public static function fetch_req_arr() {
189-
return self::fetch_var( $_REQUEST, array() );
222+
public static function fetch_req_arr( array $def = array() ): array {
223+
return self::fetch_arr( $_REQUEST, $def );
190224
}
191225

192226
/**
193227
* Fetch `$_SERVER` superglobal array.
194228
*
229+
* @param array<string, mixed> $def Default values.
195230
* @return array<string, mixed>
196231
*/
197-
public static function fetch_server_arr() {
198-
return self::fetch_var( $_SERVER, array() );
232+
public static function fetch_server_arr( array $def = array() ): array {
233+
return self::fetch_arr( $_SERVER, $def );
199234
}
200235

201236
/**
202237
* Fetch `$_COOKIE` superglobal array.
203238
*
239+
* @param array<string, string> $def Default values.
204240
* @return array<string, mixed>
205241
*/
206-
public static function fetch_cookie_arr() {
207-
return self::fetch_var( $_COOKIE, array() );
242+
public static function fetch_cookie_arr( $def = array() ): array {
243+
return self::fetch_arr( $_COOKIE, $def );
208244
}
209245

210246
/**

0 commit comments

Comments
 (0)