https://github.com/peridot-php/object-path
A string syntax to fetch values from array and object hierarchies
https://github.com/peridot-php/object-path
Last synced: 3 months ago
JSON representation
A string syntax to fetch values from array and object hierarchies
- Host: GitHub
- URL: https://github.com/peridot-php/object-path
- Owner: peridot-php
- License: mit
- Created: 2015-07-24T11:31:48.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2015-07-24T11:38:47.000Z (almost 11 years ago)
- Last Synced: 2025-10-26T00:46:44.032Z (8 months ago)
- Language: PHP
- Size: 117 KB
- Stars: 10
- Watchers: 2
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ObjectPath [](https://travis-ci.org/peridot-php/object-path)
Allows traversal of objects and arrays with a simple string syntax. Extracted from
Peridot's matcher library: [Leo](https://github.com/peridot-php/leo).
## Usage
```php
$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/
```