peridot-php / object-path
一种字符串语法,用于从数组和对象层次结构中获取值
1.0.0
2015-07-24 11:35 UTC
Requires
- php: >=5.4.0
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-14 18:04:12 UTC
README
允许通过简单的字符串语法遍历对象和数组。从Peridot的匹配器库中提取: Leo。
使用方法
$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');
测试
$ composer install
$ vendor/bin/peridot specs/