PluginProbe
Gutenberg / 10.3.2
Gutenberg v10.3.2
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / utils.php

utils.php in Gutenberg 10.3.2, at lib/utils.php

66 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * General utilities.
4 *
5 * @package gutenberg
6 */
7
8 /**
9 * Sets an array in depth based on a path of keys.
10 *
11 * It is the PHP equivalent of JavaScript's `lodash.set()` and mirroring it may help other components
12 * retain some symmetry between client and server implementations.
13 *
14 * Example usage:
15 *
16 * $array = array();
17 * _wp_array_set( $array, array( 'a', 'b', 'c', 1 );
18 * $array becomes:
19 * array(
20 * 'a' => array(
21 * 'b' => array(
22 * 'c' => 1,
23 * ),
24 * ),
25 * );
26 *
27 * @param array $array An array that we want to mutate to include a specific value in a path.
28 * @param array $path An array of keys describing the path that we want to mutate.
29 * @param mixed $value The value that will be set.
30 */
31 function gutenberg_experimental_set( &$array, $path, $value = null ) {
32 // Confirm $array is valid.
33 if ( ! is_array( $array ) ) {
34 return;
35 }
36
37 // Confirm $path is valid.
38 if ( ! is_array( $path ) ) {
39 return;
40 }
41 $path_length = count( $path );
42 if ( 0 === $path_length ) {
43 return;
44 }
45 foreach ( $path as $path_element ) {
46 if (
47 ! is_string( $path_element ) && ! is_integer( $path_element ) &&
48 ! is_null( $path_element )
49 ) {
50 return;
51 }
52 }
53
54 for ( $i = 0; $i < $path_length - 1; ++$i ) {
55 $path_element = $path[ $i ];
56 if (
57 ! array_key_exists( $path_element, $array ) ||
58 ! is_array( $array[ $path_element ] )
59 ) {
60 $array[ $path_element ] = array();
61 }
62 $array = &$array[ $path_element ]; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.VariableRedeclaration
63 }
64 $array[ $path[ $i ] ] = $value;
65 }
66