Array Expression Language


What is it ?

This package extends the ExpressionLanguage component of Symfony to compile and evaluate arrays with custom functions.

Installation

composer require php-etl/array-expression-language

Usage

To use the functions provided in this package, you need to add the expression_language key to your plugin configuration and use the Kiboko\Component\ArrayExpressionLanguage\ArrayExpressionLanguageProvider Provider.

expression_language:
  - Kiboko\Component\ArrayExpressionLanguage\ArrayExpressionLanguageProvider

To determine that a value in your configuration will be a language expression, you must use the @ annotation.

foo: '@=map(input["attributes"], extractData("[fr_FR][data]"))'

List of available functions

Generic functions

Name Description
firstKey(array input): int|string|null Gets the first key of an array
lastKey(array input): int|string|null Gets the last key of an array
keyExists(string|int key, array input): bool Checks if the given key or index exists in the array
merge(array|list …inputs): array Merge one or more arrays
count(Countable|array input ): int Count all elements in an array, or something in an object
combine(list<int|string> keys, list<mixed> values ): array Creates an array by using one array for keys and another for its values
map(callable callback, iterable input ): iterable Copy the iterator into an array
iterableToArray(Traversable iterator, bool useKeys = true ): array Applies the callback to the elements of the given iterable
reduce(iterable input, closure callback): string Iteratively reduce the iterable to a single value using a callback function
list(int length, mixed value): iterable Fill an array with values
intersect(array array, array …arrays): array Computes the intersection of arrays
filterList(string iterator, string callback): FilterIterator Filters elements of an array using a callback function
mapValues(array input, array{pattern: replacement} values): array Finds multiple matches in the input and replaces them with a value, with the format {pattern: replacement, pattern: replacement ... }
implode(string $separator, array $array): string Turns a list into a string

Closures that can be provided to reduce

Name Description
join(string separator): closure Join array elements with a string

Closures that can be provided to map

Name Description
extractData(string path): closure Retrieves the value of a key in an array or the property of an object