vnn / keyper
Do things based on the presence of a key
Installs: 67 331
Dependents: 0
Suggesters: 0
Security: 0
Stars: 6
Watchers: 11
Forks: 1
Open Issues: 0
Requires
- php: >=5.4
Requires (Dev)
- phpunit/phpunit: ~3.7
README
Do things when an array has a key
Usage
$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; });
Running tests
composer install
vendor/bin/phpunit