Vale is a helper utility that lets you get and set values in arbitrary nested arrays and objects.
Developed by Florian Eckerstorfer in Vienna, Europe.
Get and set values in complex nested arrays and objects. You can write
$baz = Vale::get($data, ['foo', 'bar', 'baz', 0]);
instead of writing:
$baz = (isset($data['foo']->bar['baz'][0])) ? $data['foo']->bar['baz'][0] : null;
You can install Vale using Composer:
$ composer require cocur/vale:dev-master
You can use the static get()
and set()
methods.
use Cocur\Vale\Vale;
Vale::get(['name' => 'Tyrion'], ['name']); // -> "Tyrion"
Vale::set([], ['name'], 'Tyrion'); // -> ["name" => "Tyrion"]
In addition you can create an instance of Vale
and use the getValue()
and setValue()
methods:
use Cocur\Vale\Vale;
$vale = new Vale();
$vale->getValue(['name' => 'Tyrion'], ['name']); // -> "Tyrion"
$vale->setValue([], ['name'], 'Tyrion'); // -> ["name" => "Tyrion"]
If the $keys
parameter is an empty array, an empty string or null
, the original $data
is returned.
Vale::get(['name' => 'Florian'], []); // -> ['name' => 'Florian']
If $data
is an array, each element in $keys
is used to navigate deeper into the nested $data
array; if $data
is an object, each key is tried as property, method, getter, hasser and isser. The order is like this:
$data[$key]
$data->$key()
$data->get$Key()
$data->has$Key()
$data->is$Key()
$data->$key
The set()
and setValue()
methods always return the $data
object or array. If the array or object is nested the
same means of navigating through the array and object as in get()
are used. When it comes to setting the value
the following versions are tried.
$data[$key] = $value
$data->$key($value)
$data->set$Key($value)
$data->$key = $value
(if$key
does not exists in$data
)
No version released yet.
The MIT license applies to Vale. For the full copyright and license information, please view the LICENSE file distributed with this source code.