library object-path
A string syntax to fetch values from array and object hierarchies
peridot-php/object-path
A string syntax to fetch values from array and object hierarchies
- Friday, July 24, 2015
- by brianium
- Repository
- 3 Watchers
- 5 Stars
- 2,457 Installations
- PHP
- 1 Dependents
- 0 Suggesters
- 3 Forks
- 0 Open issues
- 2 Versions
- 12 % Grown
ObjectPath
Allows traversal of objects and arrays with a simple string syntax. Extracted from
Peridot's matcher library: Leo., (*1)
Usage
$data = [
'name' => 'Brian',
'hobbies' => [
'reading',
'programming',
'lion taming'
],
'address' => [
'street' => '1234 Lane',
'zip' => '12345'
]
];
use Peridot\ObjectPath\ObjectPath;
$path = new ObjectPath($data);
$reading = $path->get('hobbies[0]');
$zip = $path->get('address[zip]');
// the result of get() is an ObjectPathValue instance
$value = $reading->getPropertyValue();
// The syntax also works for objects and nested structures
$data = new stdClass();
$data->name = 'Brian';
$data->address = new stdClass();
$data->address->zip = '12345';
$hobby = new stdClass();
$hobby->name = 'reading';
$hobby->style = 'relaxing';
$data->hobbies = [$hobby];
$path = new ObjectPath($data);
$name = $path->get('name');
$zip = $path->get('address->zip');
$reading = $path->get('hobbies[0]->name');
Tests
$ composer install
$ vendor/bin/peridot specs/
dev-master
9999999-dev
A string syntax to fetch values from array and object hierarchies
Sources
Download
MIT
The Requires
The Development Requires
array
object
traversal
1.0.0
1.0.0.0
A string syntax to fetch values from array and object hierarchies
Sources
Download
MIT
The Requires
The Development Requires
array
object
traversal