vnn/keyper

Do things based on the presence of a key

1.3.1 2015-09-24 19:08 UTC

This package is not auto-updated.

Last update: 2024-03-16 13:05:12 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