PHP Class lithium\util\Set

- '/User/id': Similar to the classic {n}.User.id. - '/User[2]/name': Selects the name of the second User. - '/User[id>2]': Selects all Users with an id > 2. - '/User[id>2][<5]': Selects all Users with an id > 2 but < 5. - '/Post/Comment[author_name=John]/../name': Selects the name of all posts that have at least one comment written by John. - '/Posts[name]': Selects all Posts that have a 'name' key. - '/Comment/.[1]': Selects the contents of the first comment. - '/Comment/.[:last]': Selects the last comment. - '/Comment/.[:first]': Selects the first comment. - '/Comment[text=/lithium/i]': Selects the all comments that have a text matching the regex /lithium/i. - '/Comment/@*': Selects all key names of all comments.
Datei anzeigen Open project: unionofrad/lithium Class Usage Examples

Public Methods

Method Description
append ( array $array, array $array2 ) : array Add the keys/values in $array2 that are not found in $array onto the end of $array.
check ( mixed $data, mixed $path = null ) : boolean Checks if a particular path is set in an array. Tests by key name, or dot-delimited key name, i.e.:
combine ( array $data, mixed $path1 = null, mixed $path2 = null, string $groupPath = null ) : array Creates an associative array using a $path1 as the path to build its keys, and optionally $path2 as path to get the values. If $path2 is not specified, all values will be initialized to null (useful for Set::merge()). You can optionally group the values by what is obtained when following the path specified in $groupPath.
contains ( array $array1, array $array2 ) : boolean Determines if the array elements in $array2 are wholly contained within $array1. Works recursively.
depth ( array $data, array $options = [] ) : integer Counts the dimensions of an array. If $all is set to false (which is the default) it will only consider the dimension of the first element in the array.
diff ( array $val1, array $val2 ) : array Computes the difference between two arrays.
expand ( array $data, array $options = [] ) : array Accepts a one-dimensional array where the keys are separated by a delimiter.
extract ( array $data, string $path = null, array $options = [] ) : array Implements partial support for XPath 2.0.
flatten ( array $data, array $options = [] ) : array Collapses a multi-dimensional array into a single dimension, using a delimited array path for each array element's key, i.e. array(array('Foo' => array('Bar' => 'Far'))) becomes array('0.Foo.Bar' => 'Far').
format ( array $data, string $format, array $keys ) : array Returns a series of values extracted from an array, formatted in a format string.
insert ( mixed $list, mixed $path, array $data = [] ) : array Inserts $data into an array as defined by $path.
isNumeric ( array $array = null ) : mixed Checks to see if all the values in the array are numeric.
matches ( array $data, mixed $conditions, integer $i = null, integer $length = null ) : boolean This function can be used to see if a single item or a given XPath match certain conditions.
merge ( array $array1, array $array2 ) : array This method can be thought of as a hybrid between PHP's array_merge() and array_merge_recursive(). The difference to the two is that if an array key contains another array then the function behaves recursive (unlike array_merge()) but does not do if for keys containing strings (unlike array_merge_recursive()). Please note: This function will work with an unlimited amount of arguments and typecasts non-array parameters into arrays.
normalize ( string | array $list, boolean $assoc = true, string $sep = ',', boolean $trim = true ) : array Normalizes a string or array list.
remove ( mixed $list, mixed $path = null ) : array Removes an element from an array as defined by $path.
slice ( array $data, array | string $keys ) : array Slices an array into two, separating them determined by an array of keys.
sort ( array $data, string $path, string $dir = 'asc' ) : array Sorts an array by any value, determined by a Set-compatible path.

Method Details

append() public static method

Add the keys/values in $array2 that are not found in $array onto the end of $array.
public static append ( array $array, array $array2 ) : array
$array array Original array.
$array2 array Second array to add onto the original.
return array An array containing all the keys of the second array not already present in the first.

check() public static method

embed:lithium\tests\cases\util\SetTest::testCheck(1-4)
public static check ( mixed $data, mixed $path = null ) : boolean
$data mixed Data to check on.
$path mixed A dot-delimited string.
return boolean `true` if path is found, `false` otherwise.

combine() public static method

Creates an associative array using a $path1 as the path to build its keys, and optionally $path2 as path to get the values. If $path2 is not specified, all values will be initialized to null (useful for Set::merge()). You can optionally group the values by what is obtained when following the path specified in $groupPath.
public static combine ( array $data, mixed $path1 = null, mixed $path2 = null, string $groupPath = null ) : array
$data array Array from where to extract keys and values.
$path1 mixed As an array, or as a dot-delimited string.
$path2 mixed As an array, or as a dot-delimited string.
$groupPath string As an array, or as a dot-delimited string.
return array Combined array.

contains() public static method

Determines if the array elements in $array2 are wholly contained within $array1. Works recursively.
public static contains ( array $array1, array $array2 ) : boolean
$array1 array First value.
$array2 array Second value.
return boolean Returns `true` if `$array1` wholly contains the keys and values of `$array2`, otherwise, returns `false`. Returns `false` if either array is empty.

depth() public static method

Counts the dimensions of an array. If $all is set to false (which is the default) it will only consider the dimension of the first element in the array.
public static depth ( array $data, array $options = [] ) : integer
$data array Array to count dimensions on.
$options array
return integer The number of dimensions in `$array`.

diff() public static method

Computes the difference between two arrays.
public static diff ( array $val1, array $val2 ) : array
$val1 array First value.
$val2 array Second value.
return array Computed difference.

expand() public static method

Accepts a one-dimensional array where the keys are separated by a delimiter.
public static expand ( array $data, array $options = [] ) : array
$data array The one-dimensional array to expand.
$options array The options used when expanding the array: - `'separator'` _string_: The delimiter to use when separating keys. Defaults to `'.'`.
return array Returns a multi-dimensional array expanded from a one dimensional dot-separated array.

extract() public static method

Implements partial support for XPath 2.0.
public static extract ( array $data, string $path = null, array $options = [] ) : array
$data array An array of data to extract from.
$path string An absolute XPath 2.0 path. Only absolute paths starting with a single slash are supported right now. Implemented selectors: - `'/User/id'`: Similar to the classic {n}.User.id. - `'/User[2]/name'`: Selects the name of the second User. - `'/User[id>2]'`: Selects all Users with an id > 2. - `'/User[id>2][<5]'`: Selects all Users with an id > 2 but < 5. - `'/Post/Comment[author_name=John]/../name'`: Selects the name of all posts that have at least one comment written by John. - `'/Posts[name]'`: Selects all Posts that have a `'name'` key. - `'/Comment/.[1]'`: Selects the contents of the first comment. - `'/Comment/.[:last]'`: Selects the last comment. - `'/Comment/.[:first]'`: Selects the first comment. - `'/Comment[text=/lithium/i]`': Selects the all comments that have a text matching the regex `/lithium/i`. - `'/Comment/@*'`: Selects all key names of all comments.
$options array Currently only supports `'flatten'` which can be disabled for higher XPath-ness.
return array An array of matched items.

flatten() public static method

Collapses a multi-dimensional array into a single dimension, using a delimited array path for each array element's key, i.e. array(array('Foo' => array('Bar' => 'Far'))) becomes array('0.Foo.Bar' => 'Far').
public static flatten ( array $data, array $options = [] ) : array
$data array array to flatten
$options array Available options are: - `'separator'`: String to separate array keys in path (defaults to `'.'`). - `'path'`: Starting point (defaults to null).
return array

format() public static method

Returns a series of values extracted from an array, formatted in a format string.
public static format ( array $data, string $format, array $keys ) : array
$data array Source array from which to extract the data.
$format string Format string into which values will be inserted using `sprintf()`.
$keys array An array containing one or more `Set::extract()`-style key paths.
return array An array of strings extracted from `$keys` and formatted with `$format`.

insert() public static method

Inserts $data into an array as defined by $path.
public static insert ( mixed $list, mixed $path, array $data = [] ) : array
$list mixed Where to insert into.
$path mixed A dot-delimited string.
$data array Data to insert.
return array

isNumeric() public static method

Checks to see if all the values in the array are numeric.
public static isNumeric ( array $array = null ) : mixed
$array array The array to check. If null, the value of the current Set object.
return mixed `true` if values are numeric, `false` if not and `null` if the array to check is empty.

matches() public static method

This function can be used to see if a single item or a given XPath match certain conditions.
public static matches ( array $data, mixed $conditions, integer $i = null, integer $length = null ) : boolean
$data array An array of data to execute the match on.
$conditions mixed An array of condition strings or an XPath expression.
$i integer Optional: The 'nth'-number of the item being matched.
$length integer
return boolean

merge() public static method

This method can be thought of as a hybrid between PHP's array_merge() and array_merge_recursive(). The difference to the two is that if an array key contains another array then the function behaves recursive (unlike array_merge()) but does not do if for keys containing strings (unlike array_merge_recursive()). Please note: This function will work with an unlimited amount of arguments and typecasts non-array parameters into arrays.
public static merge ( array $array1, array $array2 ) : array
$array1 array The base array.
$array2 array The array to be merged on top of the base array.
return array Merged array of all passed params.

normalize() public static method

Set::normalize('foo,bar'); // returns array('foo' => null, 'bar' => null); Set::normalize(array('foo', 'bar' => 'baz'); // returns array('foo' => null, 'bar' => 'baz');
public static normalize ( string | array $list, boolean $assoc = true, string $sep = ',', boolean $trim = true ) : array
$list string | array List to normalize.
$assoc boolean If `true`, `$list` will be converted to an associative array.
$sep string If `$list` is a string, it will be split into an array with `$sep`.
$trim boolean If `true`, separated strings will be trimmed.
return array

remove() public static method

Removes an element from an array as defined by $path.
public static remove ( mixed $list, mixed $path = null ) : array
$list mixed From where to remove.
$path mixed A dot-delimited string.
return array Array with `$path` removed from its value.

slice() public static method

Usage examples: embed:lithium\tests\cases\util\SetTest::testSetSlice(1-4)
public static slice ( array $data, array | string $keys ) : array
$data array
$keys array | string An array of keys or a single key as string
return array An array containing both arrays, having the array with requested keys first and the remainder as second element

sort() public static method

Sorts an array by any value, determined by a Set-compatible path.
public static sort ( array $data, string $path, string $dir = 'asc' ) : array
$data array
$path string A `Set`-compatible path to the array value.
$dir string Either `'asc'` (the default) or `'desc'`.
return array