Return the first element of an array, or of any iterable.
- Service:
CleverAge\ProcessBundle\Transformer\Array\ArrayFirstTransformer - Transformer code:
array_first
iterable (array, \Traversable). Any other value throws an UnexpectedValueException, unless
allow_not_iterable is true. A generator already run (it cannot be rewound) also throws an
UnexpectedValueException.
any: the first element of the iterablefalseif the iterable is empty- the input value itself if it is not iterable and
allow_not_iterableistrue
| Code | Type | Required | Default | Description |
|---|---|---|---|---|
allow_not_iterable |
bool |
false |
When true, a non-iterable input is returned unchanged instead of throwing an exception |
['foo', 'bar', 'baz']becomes'foo'
# Transformer options level
array_first: ~- Get the first element of a list, or keep a single value as is:
['foo', 'bar']becomes'foo','foo'stays'foo'
# Transformer options level
array_first:
allow_not_iterable: trueSince v6.0, a non-iterable input throws an exception by default. From v4.0 to v5.x, the option was inverted (a
non-iterable input was returned unchanged by default, and with allow_not_iterable: true a scalar or null threw a
TypeError, an object returned its first property): to keep the former default behaviour, set
allow_not_iterable: true. Before v6.0, an iterable object (\Traversable) did not return its first element: see
UPGRADE.md.