| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace Jet_Form_Builder\Classes\Arrayable; |
| 5 |
|
| 6 |
// If this file is called directly, abort. |
| 7 |
if ( ! defined( 'WPINC' ) ) { |
| 8 |
die; |
| 9 |
} |
| 10 |
|
| 11 |
class Array_Tools { |
| 12 |
|
| 13 |
/** |
| 14 |
* @param array|\Generator $payload |
| 15 |
* |
| 16 |
* @return array |
| 17 |
*/ |
| 18 |
public static function to_array( $payload ): array { |
| 19 |
if ( $payload instanceof \Generator ) { |
| 20 |
return iterator_to_array( $payload ); |
| 21 |
} |
| 22 |
|
| 23 |
return self::from_array( $payload ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* @param array|Collection $iterator |
| 28 |
* |
| 29 |
* @return \Generator |
| 30 |
*/ |
| 31 |
public static function reverse( $iterator ): \Generator { |
| 32 |
for ( $current = count( $iterator ) - 1; $current >= 0; $current-- ) { |
| 33 |
yield $iterator[ $current ]; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
public static function from_array( array $payload ): array { |
| 38 |
foreach ( $payload as $index => $object ) { |
| 39 |
if ( is_array( $object ) ) { |
| 40 |
continue; |
| 41 |
} |
| 42 |
|
| 43 |
/** @var Arrayable $object */ |
| 44 |
if ( is_object( $object ) && ! ( $object instanceof Arrayable ) ) { |
| 45 |
wp_die( 'Must implements Arrayable.', 'Illegal item of array' ); |
| 46 |
} |
| 47 |
|
| 48 |
try { |
| 49 |
$payload[ $index ] = $object->to_array(); |
| 50 |
} catch ( Array_Continue_Exception $exception ) { |
| 51 |
unset( $payload[ $index ] ); |
| 52 |
continue; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
return array_values( $payload ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Copy-paste from WP-core function |
| 61 |
* |
| 62 |
* @param array $input_array |
| 63 |
* @param array|string $path |
| 64 |
* @param mixed $default_value |
| 65 |
* |
| 66 |
* @return array|mixed |
| 67 |
* @see \_wp_array_get() |
| 68 |
* |
| 69 |
* @since 3.1.0 |
| 70 |
*/ |
| 71 |
public static function get( array $input_array, $path, $default_value = false ) { |
| 72 |
$path = self::path( $path ); |
| 73 |
|
| 74 |
foreach ( $path as $path_element ) { |
| 75 |
if ( |
| 76 |
( ! is_string( $path_element ) && ! is_integer( $path_element ) && ! is_null( $path_element ) ) || |
| 77 |
! array_key_exists( $path_element, $input_array ) |
| 78 |
) { |
| 79 |
return $default_value; |
| 80 |
} |
| 81 |
$input_array = $input_array[ $path_element ]; |
| 82 |
} |
| 83 |
|
| 84 |
return $input_array; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Copy-paste from WP-core function |
| 89 |
* |
| 90 |
* @param array $input_array |
| 91 |
* @param array|string $path |
| 92 |
* @param null $value |
| 93 |
* |
| 94 |
* @since 3.1.0 |
| 95 |
* |
| 96 |
* @see \_wp_array_set() |
| 97 |
*/ |
| 98 |
public static function set( array &$input_array, $path, $value = null ) { |
| 99 |
$path = self::path( $path ); |
| 100 |
$path_length = count( $path ); |
| 101 |
|
| 102 |
if ( 0 === $path_length ) { |
| 103 |
return; |
| 104 |
} |
| 105 |
|
| 106 |
foreach ( $path as $path_element ) { |
| 107 |
if ( |
| 108 |
! is_string( $path_element ) && ! is_integer( $path_element ) && |
| 109 |
! is_null( $path_element ) |
| 110 |
) { |
| 111 |
return; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
for ( $i = 0; $i < $path_length - 1; ++$i ) { |
| 116 |
$path_element = $path[ $i ]; |
| 117 |
if ( |
| 118 |
! array_key_exists( $path_element, $input_array ) || |
| 119 |
! is_array( $input_array[ $path_element ] ) |
| 120 |
) { |
| 121 |
$input_array[ $path_element ] = array(); |
| 122 |
} |
| 123 |
$input_array = &$input_array[ $path_element ]; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.VariableRedeclaration |
| 124 |
} |
| 125 |
|
| 126 |
$input_array[ $path[ $i ] ] = $value; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* @param $source |
| 131 |
* |
| 132 |
* @return int|string|null |
| 133 |
*/ |
| 134 |
public static function last_key( $source ) { |
| 135 |
if ( function_exists( 'array_key_last' ) ) { |
| 136 |
// phpcs:ignore PHPCompatibility.FunctionUse.NewFunctions |
| 137 |
return array_key_last( $source ); |
| 138 |
} |
| 139 |
|
| 140 |
if ( ! is_array( $source ) || empty( $source ) ) { |
| 141 |
return null; |
| 142 |
} |
| 143 |
|
| 144 |
end( $source ); |
| 145 |
|
| 146 |
return key( $source ); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* @param $source |
| 151 |
* |
| 152 |
* @return int|string|null |
| 153 |
*/ |
| 154 |
public static function last( $source ) { |
| 155 |
if ( ! is_array( $source ) || empty( $source ) ) { |
| 156 |
return null; |
| 157 |
} |
| 158 |
|
| 159 |
$key = self::last_key( $source ); |
| 160 |
|
| 161 |
return $source[ $key ]; |
| 162 |
} |
| 163 |
|
| 164 |
public static function path( $items ): array { |
| 165 |
return iterator_to_array( self::generate_path( $items ) ); |
| 166 |
} |
| 167 |
|
| 168 |
private static function generate_path( $items ): \Generator { |
| 169 |
if ( is_string( $items ) ) { |
| 170 |
$items = explode( '.', $items ); |
| 171 |
} |
| 172 |
foreach ( $items as $item ) { |
| 173 |
yield $item; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
} |
| 178 |
|