vnn / keyper
基于键的存在执行操作
1.3.1
2015-09-24 19:08 UTC
Requires
- php: >=5.4
Requires (Dev)
- phpunit/phpunit: ~3.7
README
当数组有键时执行操作
使用方法
$data = [ 'key1' => 'hello', 'nested' => [ 'one' => 1, 'two' => 2, 'three' => [ 'four' => 5 ] ] ]; $keyper = Keyper::create($data); //do something with a single value $keyper->when('key1', function($value) { //$value == 'hello' print $value; }); //drill down a nested array $keyper->when('nested.three.four', function($value) { //$value == 5 print $value; }); //do something with multiple keys $keyper->when(['nested.one', 'nested.two'], function($one, $two) { //$one == 1 //$two == 2 print $one + $two; }); //compose several functions $keyper->when(['nested.one', 'nested.two'], function($sum) { //$sum == 3 print $sum; }, function($one, $two) { //$one == 1 //$two == 2 return $one + $two; //this result gets passed to the function using $sum }); //if you need all the specified keys to be present, use whenAll $keyper->whenAll(['nested.one', 'nested.two'], function($one, $two) { //$one == 1 //$two == 2 print $one + $two; });
运行测试
composer install
vendor/bin/phpunit