| 1 |
<?php |
| 2 |
|
| 3 |
namespace FakerPress; |
| 4 |
|
| 5 |
/** |
| 6 |
* Set key/value within an array, can set a key nested inside a multidimensional array. |
| 7 |
* |
| 8 |
* Example: set( $a, [ 0, 1, 2 ], 'hi' ) sets $a[0][1][2] = 'hi' and returns $a. |
| 9 |
* |
| 10 |
* @since 0.6.0 |
| 11 |
* |
| 12 |
* @param mixed $array The array containing the key this sets. |
| 13 |
* @param string|array $key To set a key nested multiple levels deep pass an array |
| 14 |
* specifying each key in order as a value. |
| 15 |
* Example: array( 'lvl1', 'lvl2', 'lvl3' ); |
| 16 |
* @param mixed $value The value. |
| 17 |
* |
| 18 |
* @return array Full array with the key set to the specified value. |
| 19 |
*/ |
| 20 |
function set( array $array, $key, $value ) { |
| 21 |
// Convert strings and such to array. |
| 22 |
$key = (array) $key; |
| 23 |
|
| 24 |
// Setup a pointer that we can point to the key specified. |
| 25 |
$key_pointer = &$array; |
| 26 |
|
| 27 |
// Iterate through every key, setting the pointer one level deeper each time. |
| 28 |
foreach ( $key as $i ) { |
| 29 |
|
| 30 |
// Ensure current array depth can have children set. |
| 31 |
if ( ! is_array( $key_pointer ) ) { |
| 32 |
// $key_pointer is set but is not an array. Converting it to an array |
| 33 |
// would likely lead to unexpected problems for whatever first set it. |
| 34 |
$error = sprintf( |
| 35 |
'Attempted to set $array[%1$s] but %2$s is already set and is not an array.', |
| 36 |
implode( $key, '][' ), |
| 37 |
$i |
| 38 |
); |
| 39 |
|
| 40 |
_doing_it_wrong( __FUNCTION__, esc_html( $error ), '0.1.0' ); |
| 41 |
break; |
| 42 |
} elseif ( ! isset( $key_pointer[ $i ] ) ) { |
| 43 |
$key_pointer[ $i ] = []; |
| 44 |
} |
| 45 |
|
| 46 |
// Dive one level deeper into the nested array. |
| 47 |
$key_pointer = &$key_pointer[ $i ]; |
| 48 |
} |
| 49 |
|
| 50 |
// Set the value for the specified key |
| 51 |
$key_pointer = $value; |
| 52 |
|
| 53 |
return $array; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Find a value inside an array or object, including one nested a few levels deep. |
| 58 |
* |
| 59 |
* Example: get( $a, [ 0, 1, 2 ] ) returns the value of $a[0][1][2] or the default. |
| 60 |
* |
| 61 |
* @since 0.6.0 |
| 62 |
* |
| 63 |
* @param array|object $variable Array or object to search within. |
| 64 |
* @param array|string $indexes Specify each nested index in order. |
| 65 |
* Example: array( 'lvl1', 'lvl2' ); |
| 66 |
* @param mixed $default Default value if the search finds nothing. |
| 67 |
* |
| 68 |
* @return mixed The value of the specified index or the default if not found. |
| 69 |
*/ |
| 70 |
function get( $variable, $indexes, $default = null ) { |
| 71 |
if ( is_object( $variable ) ) { |
| 72 |
$variable = (array) $variable; |
| 73 |
} |
| 74 |
|
| 75 |
if ( ! is_array( $variable ) ) { |
| 76 |
return $default; |
| 77 |
} |
| 78 |
|
| 79 |
foreach ( (array) $indexes as $index ) { |
| 80 |
if ( ! is_array( $variable ) || ! isset( $variable[ $index ] ) ) { |
| 81 |
$variable = $default; |
| 82 |
break; |
| 83 |
} |
| 84 |
|
| 85 |
$variable = $variable[ $index ]; |
| 86 |
} |
| 87 |
|
| 88 |
return $variable; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Find a value inside a list of array or objects, including one nested a few levels deep. |
| 93 |
* |
| 94 |
* @since 0.6.0 |
| 95 |
* |
| 96 |
* Example: get( [$a, $b, $c], [ 0, 1, 2 ] ) returns the value of $a[0][1][2] found in $a, $b or $c |
| 97 |
* or the default. |
| 98 |
* |
| 99 |
* @param array $variables Array of arrays or objects to search within. |
| 100 |
* @param array|string $indexes Specify each nested index in order. |
| 101 |
* Example: array( 'lvl1', 'lvl2' ); |
| 102 |
* @param mixed $default Default value if the search finds nothing. |
| 103 |
* |
| 104 |
* @return mixed The value of the specified index or the default if not found. |
| 105 |
*/ |
| 106 |
function get_in_any( array $variables, $indexes, $default = null ) { |
| 107 |
foreach ( $variables as $variable ) { |
| 108 |
$found = get( $variable, $indexes, '__not_found__' ); |
| 109 |
if ( '__not_found__' !== $found ) { |
| 110 |
return $found; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return $default; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Tests to see if the requested variable is set either as a post field or as a URL |
| 119 |
* param and returns the value if so. |
| 120 |
* |
| 121 |
* Post data takes priority over fields passed in the URL query. If the field is not |
| 122 |
* set then $default (null unless a different value is specified) will be returned. |
| 123 |
* |
| 124 |
* The variable being tested for can be an array if you wish to find a nested value. |
| 125 |
* |
| 126 |
* @see \StellarWP\Jobvite\get_in_any() |
| 127 |
* |
| 128 |
* @since 0.6.0 |
| 129 |
* |
| 130 |
* @param string|array $indexes |
| 131 |
* @param mixed $default |
| 132 |
* |
| 133 |
* @return mixed |
| 134 |
*/ |
| 135 |
function get_request_var( $indexes, $default = null ) { |
| 136 |
return get_in_any( [ $_GET, $_POST, $_REQUEST ], $indexes, $default ); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Fetches the Request method used for this request. |
| 141 |
* |
| 142 |
* @since 0.6.0 |
| 143 |
* |
| 144 |
* @return string |
| 145 |
*/ |
| 146 |
function get_request_method(): string { |
| 147 |
return strtoupper( wp_unslash( get( $_SERVER, 'REQUEST_METHOD', 'GET' ) ) ); |
| 148 |
} |
| 149 |
|
| 150 |
|