| 1 |
<?php |
| 2 |
/** |
| 3 |
* General utilities. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* This function allows to easily access a part from a php array. |
| 10 |
* It is equivalent to want lodash get provides for JavaScript and is useful to have something similar |
| 11 |
* in php so functions that do the same thing on the client and sever can have identical code. |
| 12 |
* |
| 13 |
* @param array $array An array from where we want to retrieve some information from. |
| 14 |
* @param array $path An array containing the path we want to retrieve. |
| 15 |
* @param array $default The return value if $array or $path is not expected input type. |
| 16 |
* |
| 17 |
* @return array An array matching the path specified. |
| 18 |
*/ |
| 19 |
function gutenberg_experimental_get( $array, $path, $default = array() ) { |
| 20 |
// Confirm input values are expected type to avoid notice warnings. |
| 21 |
if ( ! is_array( $array ) || ! is_array( $path ) ) { |
| 22 |
return $default; |
| 23 |
} |
| 24 |
|
| 25 |
$path_length = count( $path ); |
| 26 |
for ( $i = 0; $i < $path_length; ++$i ) { |
| 27 |
if ( ! isset( $array[ $path[ $i ] ] ) ) { |
| 28 |
return $default; |
| 29 |
} |
| 30 |
$array = $array[ $path[ $i ] ]; |
| 31 |
} |
| 32 |
return $array; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Sets an array in depth based on a path of keys. |
| 37 |
* |
| 38 |
* It is the PHP equivalent of JavaScript's `lodash.set()` and mirroring it may help other components |
| 39 |
* retain some symmetry between client and server implementations. |
| 40 |
* |
| 41 |
* Example usage: |
| 42 |
* |
| 43 |
* $array = array(); |
| 44 |
* _wp_array_set( $array, array( 'a', 'b', 'c', 1 ); |
| 45 |
* $array becomes: |
| 46 |
* array( |
| 47 |
* 'a' => array( |
| 48 |
* 'b' => array( |
| 49 |
* 'c' => 1, |
| 50 |
* ), |
| 51 |
* ), |
| 52 |
* ); |
| 53 |
* |
| 54 |
* @param array $array An array that we want to mutate to include a specific value in a path. |
| 55 |
* @param array $path An array of keys describing the path that we want to mutate. |
| 56 |
* @param mixed $value The value that will be set. |
| 57 |
*/ |
| 58 |
function gutenberg_experimental_set( &$array, $path, $value = null ) { |
| 59 |
// Confirm $array is valid. |
| 60 |
if ( ! is_array( $array ) ) { |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
// Confirm $path is valid. |
| 65 |
if ( ! is_array( $path ) ) { |
| 66 |
return; |
| 67 |
} |
| 68 |
$path_length = count( $path ); |
| 69 |
if ( 0 === $path_length ) { |
| 70 |
return; |
| 71 |
} |
| 72 |
foreach ( $path as $path_element ) { |
| 73 |
if ( |
| 74 |
! is_string( $path_element ) && ! is_integer( $path_element ) && |
| 75 |
! is_null( $path_element ) |
| 76 |
) { |
| 77 |
return; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
$i; |
| 82 |
for ( $i = 0; $i < $path_length - 1; ++$i ) { |
| 83 |
$path_element = $path[ $i ]; |
| 84 |
if ( |
| 85 |
! array_key_exists( $path_element, $array ) || |
| 86 |
! is_array( $array[ $path_element ] ) |
| 87 |
) { |
| 88 |
$array[ $path_element ] = array(); |
| 89 |
} |
| 90 |
$array = &$array[ $path_element ]; |
| 91 |
} |
| 92 |
$array[ $path[ $i ] ] = $value; |
| 93 |
} |
| 94 |
|