vicary / object-path
peridot-php/object-path 的分支。用于从数组对象层级中获取值的一个字符串语法
1.1.0
2016-07-26 13:59 UTC
Requires
- php: >=5.4.0
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-26 03:33:03 UTC
README
允许使用简单的字符串语法遍历对象和数组。从 Peridot 的匹配器库中提取:Leo。
用法
use Peridot\ObjectPath\ObjectPath; // Works with nested arrays, objects, or a mixture of both. $data = [ 'name' => 'Brian', 'hobbies' => [ 'reading', 'programming', 'lion taming' ], 'address' => new stdClass() [ 'street' => '1234 Lane', 'zip' => '12345' ] ]; $data['address']->street = '1234 Lane'; $data['address']->zip = 12345; // Wraps the value with ObjectPath $path = new ObjectPath($data); // Get the value directly $reading = $path->{'hobbies[0]'}; $zip = $path->{'address[zip]'}; // Sets the value $path->{'address->street'} = '12345 Street'; // Removes the property unset($path->{'hobbies[2]'}); // backward compatible with peridot-php/object-path $reading = $path->get('hobbies[0]'); $zip = $path->get('address[zip]'); // the result of get() is an ObjectPathValue instance $value = $reading->getPropertyValue();
测试
$ composer install
$ vendor/bin/peridot specs/