Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Utilitie...
File: ArrayUtil.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* A class of utilities for dealing with arrays.
[2] Fix | Delete
*/
[3] Fix | Delete
[4] Fix | Delete
declare( strict_types = 1 );
[5] Fix | Delete
namespace Automattic\WooCommerce\Utilities;
[6] Fix | Delete
[7] Fix | Delete
/**
[8] Fix | Delete
* A class of utilities for dealing with arrays.
[9] Fix | Delete
*/
[10] Fix | Delete
class ArrayUtil {
[11] Fix | Delete
[12] Fix | Delete
/**
[13] Fix | Delete
* Automatic selector type for the 'select' method.
[14] Fix | Delete
*/
[15] Fix | Delete
public const SELECT_BY_AUTO = 0;
[16] Fix | Delete
[17] Fix | Delete
/**
[18] Fix | Delete
* Object method selector type for the 'select' method.
[19] Fix | Delete
*/
[20] Fix | Delete
public const SELECT_BY_OBJECT_METHOD = 1;
[21] Fix | Delete
[22] Fix | Delete
/**
[23] Fix | Delete
* Object property selector type for the 'select' method.
[24] Fix | Delete
*/
[25] Fix | Delete
public const SELECT_BY_OBJECT_PROPERTY = 2;
[26] Fix | Delete
[27] Fix | Delete
/**
[28] Fix | Delete
* Array key selector type for the 'select' method.
[29] Fix | Delete
*/
[30] Fix | Delete
public const SELECT_BY_ARRAY_KEY = 3;
[31] Fix | Delete
[32] Fix | Delete
/**
[33] Fix | Delete
* Get a value from an nested array by specifying the entire key hierarchy with '::' as separator.
[34] Fix | Delete
*
[35] Fix | Delete
* E.g. for [ 'foo' => [ 'bar' => [ 'fizz' => 'buzz' ] ] ] the value for key 'foo::bar::fizz' would be 'buzz'.
[36] Fix | Delete
*
[37] Fix | Delete
* @param array $items The array to get the value from.
[38] Fix | Delete
* @param string $key The complete key hierarchy, using '::' as separator.
[39] Fix | Delete
* @param mixed $default_value The value to return if the key doesn't exist in the array.
[40] Fix | Delete
*
[41] Fix | Delete
* @return mixed The retrieved value, or the supplied default value.
[42] Fix | Delete
* @throws \Exception $array is not an array.
[43] Fix | Delete
*/
[44] Fix | Delete
public static function get_nested_value( array $items, string $key, $default_value = null ) {
[45] Fix | Delete
$key_stack = explode( '::', $key );
[46] Fix | Delete
$subkey = array_shift( $key_stack );
[47] Fix | Delete
[48] Fix | Delete
if ( isset( $items[ $subkey ] ) ) {
[49] Fix | Delete
$value = $items[ $subkey ];
[50] Fix | Delete
[51] Fix | Delete
if ( count( $key_stack ) ) {
[52] Fix | Delete
foreach ( $key_stack as $subkey ) {
[53] Fix | Delete
if ( is_array( $value ) && isset( $value[ $subkey ] ) ) {
[54] Fix | Delete
$value = $value[ $subkey ];
[55] Fix | Delete
} else {
[56] Fix | Delete
$value = $default_value;
[57] Fix | Delete
break;
[58] Fix | Delete
}
[59] Fix | Delete
}
[60] Fix | Delete
}
[61] Fix | Delete
} else {
[62] Fix | Delete
$value = $default_value;
[63] Fix | Delete
}
[64] Fix | Delete
[65] Fix | Delete
return $value;
[66] Fix | Delete
}
[67] Fix | Delete
[68] Fix | Delete
/**
[69] Fix | Delete
* Checks if a given key exists in an array and its value can be evaluated as 'true'.
[70] Fix | Delete
*
[71] Fix | Delete
* @param array $items The array to check.
[72] Fix | Delete
* @param string $key The key for the value to check.
[73] Fix | Delete
* @return bool True if the key exists in the array and the value can be evaluated as 'true'.
[74] Fix | Delete
*/
[75] Fix | Delete
public static function is_truthy( array $items, string $key ) {
[76] Fix | Delete
return isset( $items[ $key ] ) && $items[ $key ];
[77] Fix | Delete
}
[78] Fix | Delete
[79] Fix | Delete
/**
[80] Fix | Delete
* Gets the value for a given key from an array, or a default value if the key doesn't exist in the array.
[81] Fix | Delete
*
[82] Fix | Delete
* This is equivalent to "$array[$key] ?? $default" except in one case:
[83] Fix | Delete
* when they key exists, has a null value, and a non-null default is supplied:
[84] Fix | Delete
*
[85] Fix | Delete
* $array = ['key' => null]
[86] Fix | Delete
* $array['key'] ?? 'default' => 'default'
[87] Fix | Delete
* ArrayUtil::get_value_or_default($array, 'key', 'default') => null
[88] Fix | Delete
*
[89] Fix | Delete
* @param array $items The array to get the value from.
[90] Fix | Delete
* @param string $key The key to use to retrieve the value.
[91] Fix | Delete
* @param null $default_value The default value to return if the key doesn't exist in the array.
[92] Fix | Delete
* @return mixed|null The value for the key, or the default value passed.
[93] Fix | Delete
*/
[94] Fix | Delete
public static function get_value_or_default( array $items, string $key, $default_value = null ) {
[95] Fix | Delete
return array_key_exists( $key, $items ) ? $items[ $key ] : $default_value;
[96] Fix | Delete
}
[97] Fix | Delete
[98] Fix | Delete
/**
[99] Fix | Delete
* Converts an array of numbers to a human-readable range, such as "1,2,3,5" to "1-3, 5". It also supports
[100] Fix | Delete
* floating point numbers, however with some perhaps unexpected / undefined behaviour if used within a range.
[101] Fix | Delete
* Source: https://stackoverflow.com/a/34254663/4574
[102] Fix | Delete
*
[103] Fix | Delete
* @param array $items An array (in any order, see $sort) of individual numbers.
[104] Fix | Delete
* @param string $item_separator The string that separates sequential range groups. Defaults to ', '.
[105] Fix | Delete
* @param string $range_separator The string that separates ranges. Defaults to '-'. A plausible example otherwise would be ' to '.
[106] Fix | Delete
* @param bool|true $sort Sort the array prior to iterating? You'll likely always want to sort, but if not, you can set this to false.
[107] Fix | Delete
*
[108] Fix | Delete
* @return string
[109] Fix | Delete
*/
[110] Fix | Delete
public static function to_ranges_string( array $items, string $item_separator = ', ', string $range_separator = '-', bool $sort = true ): string {
[111] Fix | Delete
if ( $sort ) {
[112] Fix | Delete
sort( $items );
[113] Fix | Delete
}
[114] Fix | Delete
[115] Fix | Delete
$point = null;
[116] Fix | Delete
$range = false;
[117] Fix | Delete
$str = '';
[118] Fix | Delete
[119] Fix | Delete
foreach ( $items as $i ) {
[120] Fix | Delete
if ( null === $point ) {
[121] Fix | Delete
$str .= $i;
[122] Fix | Delete
} elseif ( ( $point + 1 ) === $i ) {
[123] Fix | Delete
$range = true;
[124] Fix | Delete
} else {
[125] Fix | Delete
if ( $range ) {
[126] Fix | Delete
$str .= $range_separator . $point;
[127] Fix | Delete
$range = false;
[128] Fix | Delete
}
[129] Fix | Delete
$str .= $item_separator . $i;
[130] Fix | Delete
}
[131] Fix | Delete
$point = $i;
[132] Fix | Delete
}
[133] Fix | Delete
[134] Fix | Delete
if ( $range ) {
[135] Fix | Delete
$str .= $range_separator . $point;
[136] Fix | Delete
}
[137] Fix | Delete
[138] Fix | Delete
return $str;
[139] Fix | Delete
}
[140] Fix | Delete
[141] Fix | Delete
/**
[142] Fix | Delete
* Helper function to generate a callback which can be executed on an array to select a value from each item.
[143] Fix | Delete
*
[144] Fix | Delete
* @param string $selector_name Field/property/method name to select.
[145] Fix | Delete
* @param int $selector_type Selector type.
[146] Fix | Delete
*
[147] Fix | Delete
* @return \Closure Callback to select the value.
[148] Fix | Delete
*/
[149] Fix | Delete
private static function get_selector_callback( string $selector_name, int $selector_type = self::SELECT_BY_AUTO ): \Closure {
[150] Fix | Delete
if ( self::SELECT_BY_OBJECT_METHOD === $selector_type ) {
[151] Fix | Delete
$callback = function ( $item ) use ( $selector_name ) {
[152] Fix | Delete
return $item->$selector_name();
[153] Fix | Delete
};
[154] Fix | Delete
} elseif ( self::SELECT_BY_OBJECT_PROPERTY === $selector_type ) {
[155] Fix | Delete
$callback = function ( $item ) use ( $selector_name ) {
[156] Fix | Delete
return $item->$selector_name;
[157] Fix | Delete
};
[158] Fix | Delete
} elseif ( self::SELECT_BY_ARRAY_KEY === $selector_type ) {
[159] Fix | Delete
$callback = function ( $item ) use ( $selector_name ) {
[160] Fix | Delete
return $item[ $selector_name ];
[161] Fix | Delete
};
[162] Fix | Delete
} else {
[163] Fix | Delete
$callback = function ( $item ) use ( $selector_name ) {
[164] Fix | Delete
if ( is_array( $item ) ) {
[165] Fix | Delete
return $item[ $selector_name ];
[166] Fix | Delete
} elseif ( method_exists( $item, $selector_name ) ) {
[167] Fix | Delete
return $item->$selector_name();
[168] Fix | Delete
} else {
[169] Fix | Delete
return $item->$selector_name;
[170] Fix | Delete
}
[171] Fix | Delete
};
[172] Fix | Delete
}
[173] Fix | Delete
return $callback;
[174] Fix | Delete
}
[175] Fix | Delete
[176] Fix | Delete
/**
[177] Fix | Delete
* Select one single value from all the items in an array of either arrays or objects based on a selector.
[178] Fix | Delete
* For arrays, the selector is a key name; for objects, the selector can be either a method name or a property name.
[179] Fix | Delete
*
[180] Fix | Delete
* @param array $items Items to apply the selection to.
[181] Fix | Delete
* @param string $selector_name Key, method or property name to use as a selector.
[182] Fix | Delete
* @param int $selector_type Selector type, one of the SELECT_BY_* constants.
[183] Fix | Delete
* @return array The selected values.
[184] Fix | Delete
*/
[185] Fix | Delete
public static function select( array $items, string $selector_name, int $selector_type = self::SELECT_BY_AUTO ): array {
[186] Fix | Delete
$callback = self::get_selector_callback( $selector_name, $selector_type );
[187] Fix | Delete
return array_map( $callback, $items );
[188] Fix | Delete
}
[189] Fix | Delete
[190] Fix | Delete
/**
[191] Fix | Delete
* Returns a new assoc array with format [ $key1 => $item1, $key2 => $item2, ... ] where $key is the value of the selector and items are original items passed.
[192] Fix | Delete
*
[193] Fix | Delete
* @param array $items Items to use for conversion.
[194] Fix | Delete
* @param string $selector_name Key, method or property name to use as a selector.
[195] Fix | Delete
* @param int $selector_type Selector type, one of the SELECT_BY_* constants.
[196] Fix | Delete
*
[197] Fix | Delete
* @return array The converted assoc array.
[198] Fix | Delete
*/
[199] Fix | Delete
public static function select_as_assoc( array $items, string $selector_name, int $selector_type = self::SELECT_BY_AUTO ): array {
[200] Fix | Delete
$selector_callback = self::get_selector_callback( $selector_name, $selector_type );
[201] Fix | Delete
$result = array();
[202] Fix | Delete
foreach ( $items as $item ) {
[203] Fix | Delete
$key = $selector_callback( $item );
[204] Fix | Delete
self::ensure_key_is_array( $result, $key );
[205] Fix | Delete
$result[ $key ][] = $item;
[206] Fix | Delete
}
[207] Fix | Delete
return $result;
[208] Fix | Delete
}
[209] Fix | Delete
[210] Fix | Delete
/**
[211] Fix | Delete
* Returns whether two assoc array are same. The comparison is done recursively by keys, and the functions returns on first difference found.
[212] Fix | Delete
*
[213] Fix | Delete
* @param array $array1 First array to compare.
[214] Fix | Delete
* @param array $array2 Second array to compare.
[215] Fix | Delete
* @param bool $strict Whether to use strict comparison.
[216] Fix | Delete
*
[217] Fix | Delete
* @return bool Whether the arrays are different.
[218] Fix | Delete
*/
[219] Fix | Delete
public static function deep_compare_array_diff( array $array1, array $array2, bool $strict = true ) {
[220] Fix | Delete
return self::deep_compute_or_compare_array_diff( $array1, $array2, true, $strict );
[221] Fix | Delete
}
[222] Fix | Delete
[223] Fix | Delete
/**
[224] Fix | Delete
* Computes difference between two assoc arrays recursively. Similar to PHP's native assoc_array_diff, but also supports nested arrays.
[225] Fix | Delete
*
[226] Fix | Delete
* @param array $array1 First array.
[227] Fix | Delete
* @param array $array2 Second array.
[228] Fix | Delete
* @param bool $strict Whether to also match type of values.
[229] Fix | Delete
*
[230] Fix | Delete
* @return array The difference between the two arrays.
[231] Fix | Delete
*/
[232] Fix | Delete
public static function deep_assoc_array_diff( array $array1, array $array2, bool $strict = true ): array {
[233] Fix | Delete
return self::deep_compute_or_compare_array_diff( $array1, $array2, false, $strict );
[234] Fix | Delete
}
[235] Fix | Delete
[236] Fix | Delete
/**
[237] Fix | Delete
* Helper method to compare to compute difference between two arrays. Comparison is done recursively.
[238] Fix | Delete
*
[239] Fix | Delete
* @param array $array1 First array.
[240] Fix | Delete
* @param array $array2 Second array.
[241] Fix | Delete
* @param bool $compare Whether to compare the arrays. If true, then function will return false on first difference, in order to be slightly more efficient.
[242] Fix | Delete
* @param bool $strict Whether to do string comparison.
[243] Fix | Delete
*
[244] Fix | Delete
* @return array|bool The difference between the two arrays, or if array are same, depending upon $compare param.
[245] Fix | Delete
*/
[246] Fix | Delete
private static function deep_compute_or_compare_array_diff( array $array1, array $array2, bool $compare, bool $strict = true ) {
[247] Fix | Delete
$diff = array();
[248] Fix | Delete
foreach ( $array1 as $key => $value ) {
[249] Fix | Delete
if ( is_array( $value ) ) {
[250] Fix | Delete
if ( ! array_key_exists( $key, $array2 ) || ! is_array( $array2[ $key ] ) ) {
[251] Fix | Delete
if ( $compare ) {
[252] Fix | Delete
return true;
[253] Fix | Delete
}
[254] Fix | Delete
$diff[ $key ] = $value;
[255] Fix | Delete
continue;
[256] Fix | Delete
}
[257] Fix | Delete
$new_diff = self::deep_assoc_array_diff( $value, $array2[ $key ], $strict );
[258] Fix | Delete
if ( ! empty( $new_diff ) ) {
[259] Fix | Delete
if ( $compare ) {
[260] Fix | Delete
return true;
[261] Fix | Delete
}
[262] Fix | Delete
$diff[ $key ] = $new_diff;
[263] Fix | Delete
}
[264] Fix | Delete
} elseif ( $strict ) {
[265] Fix | Delete
if ( ! array_key_exists( $key, $array2 ) || $value !== $array2[ $key ] ) {
[266] Fix | Delete
if ( $compare ) {
[267] Fix | Delete
return true;
[268] Fix | Delete
}
[269] Fix | Delete
$diff[ $key ] = $value;
[270] Fix | Delete
}
[271] Fix | Delete
// phpcs:ignore Universal.Operators.StrictComparisons.LooseNotEqual -- Intentional when $strict is false.
[272] Fix | Delete
} elseif ( ! array_key_exists( $key, $array2 ) || $value != $array2[ $key ] ) {
[273] Fix | Delete
if ( $compare ) {
[274] Fix | Delete
return true;
[275] Fix | Delete
}
[276] Fix | Delete
$diff[ $key ] = $value;
[277] Fix | Delete
}
[278] Fix | Delete
}
[279] Fix | Delete
[280] Fix | Delete
return $compare ? false : $diff;
[281] Fix | Delete
}
[282] Fix | Delete
[283] Fix | Delete
/**
[284] Fix | Delete
* Push a value to an array, but only if the value isn't in the array already.
[285] Fix | Delete
*
[286] Fix | Delete
* @param array $items The array.
[287] Fix | Delete
* @param mixed $value The value to maybe push.
[288] Fix | Delete
* @return bool True if the value has been added to the array, false if the value was already in the array.
[289] Fix | Delete
*/
[290] Fix | Delete
public static function push_once( array &$items, $value ): bool {
[291] Fix | Delete
if ( in_array( $value, $items, true ) ) {
[292] Fix | Delete
return false;
[293] Fix | Delete
}
[294] Fix | Delete
[295] Fix | Delete
$items[] = $value;
[296] Fix | Delete
return true;
[297] Fix | Delete
}
[298] Fix | Delete
[299] Fix | Delete
/**
[300] Fix | Delete
* Ensure that an associative array has a given key, and if not, set the key to an empty array.
[301] Fix | Delete
*
[302] Fix | Delete
* @param array $items The array to check.
[303] Fix | Delete
* @param string $key The key to check.
[304] Fix | Delete
* @param bool $throw_if_existing_is_not_array If true, an exception will be thrown if the key already exists in the array but the value is not an array.
[305] Fix | Delete
* @return bool True if the key has been added to the array, false if not (the key already existed).
[306] Fix | Delete
* @throws \Exception The key already exists in the array but the value is not an array.
[307] Fix | Delete
*/
[308] Fix | Delete
public static function ensure_key_is_array( array &$items, string $key, bool $throw_if_existing_is_not_array = false ): bool {
[309] Fix | Delete
if ( ! isset( $items[ $key ] ) ) {
[310] Fix | Delete
$items[ $key ] = array();
[311] Fix | Delete
return true;
[312] Fix | Delete
}
[313] Fix | Delete
[314] Fix | Delete
if ( $throw_if_existing_is_not_array && ! is_array( $items[ $key ] ) ) {
[315] Fix | Delete
$type = is_object( $items[ $key ] ) ? get_class( $items[ $key ] ) : gettype( $items[ $key ] );
[316] Fix | Delete
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
[317] Fix | Delete
throw new \Exception( "Array key exists but it's not an array, it's a {$type}" );
[318] Fix | Delete
}
[319] Fix | Delete
[320] Fix | Delete
return false;
[321] Fix | Delete
}
[322] Fix | Delete
[323] Fix | Delete
/**
[324] Fix | Delete
* Given an array of associative arrays, all having a shared key name ("column"), generates a new array in which
[325] Fix | Delete
* keys are the distinct column values found, and values are arrays with all the matches found
[326] Fix | Delete
* (or only the last matching array found, if $single_values is true).
[327] Fix | Delete
* See ArrayUtilTest for examples.
[328] Fix | Delete
*
[329] Fix | Delete
* @param array $items The array to process.
[330] Fix | Delete
* @param string $column The name of the key to group by.
[331] Fix | Delete
* @param bool $single_values True to only return the last suitable array found for each column value.
[332] Fix | Delete
* @return array The grouped array.
[333] Fix | Delete
*/
[334] Fix | Delete
public static function group_by_column( array $items, string $column, bool $single_values = false ): array {
[335] Fix | Delete
if ( $single_values ) {
[336] Fix | Delete
return array_combine( array_column( $items, $column ), array_values( $items ) );
[337] Fix | Delete
}
[338] Fix | Delete
[339] Fix | Delete
$distinct_column_values = array_unique( array_column( $items, $column ), SORT_REGULAR );
[340] Fix | Delete
$result = array_fill_keys( $distinct_column_values, array() );
[341] Fix | Delete
[342] Fix | Delete
foreach ( $items as $value ) {
[343] Fix | Delete
$result[ $value[ $column ] ][] = $value;
[344] Fix | Delete
}
[345] Fix | Delete
[346] Fix | Delete
return $result;
[347] Fix | Delete
}
[348] Fix | Delete
[349] Fix | Delete
/**
[350] Fix | Delete
* Check if all items in an array pass a callback.
[351] Fix | Delete
*
[352] Fix | Delete
* @param array $items The array to check.
[353] Fix | Delete
* @param callable $callback The callback to check each item.
[354] Fix | Delete
*
[355] Fix | Delete
* @return bool true if all items pass the callback, false otherwise.
[356] Fix | Delete
*/
[357] Fix | Delete
public static function array_all( array $items, callable $callback ): bool {
[358] Fix | Delete
if ( function_exists( 'array_all' ) ) {
[359] Fix | Delete
return array_all( $items, $callback );
[360] Fix | Delete
}
[361] Fix | Delete
foreach ( $items as $item ) {
[362] Fix | Delete
if ( ! $callback( $item ) ) {
[363] Fix | Delete
return false;
[364] Fix | Delete
}
[365] Fix | Delete
}
[366] Fix | Delete
return true;
[367] Fix | Delete
}
[368] Fix | Delete
}
[369] Fix | Delete
[370] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function