vnn/keyper

Do things based on the presence of a key

Installs: 3 844

Dependents: 0

Stars: 3

Watchers: 3

Forks: 1

Language: PHP

1.3.0 2015-02-05 00:32 UTC

README

Build Status

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