Wallogit.com
2017 © Pedro Peláez
ArrayWalker, OOP wrapper for array_walk() and array_map()
An OOP wrapper for the built-in array_walk() and array_map()., (*1)
Using Composer, just $ composer require noi/array-walker package or:, (*2)
{
"require": {
"noi/array-walker": "dev-master"
}
}
Example 1:, (*3)
<?php
$names = array('*APPLE*', '*ORANGE*', '*LEMON*');
$walker = new \Noi\Util\ArrayWalker($names);
$result = $walker->trim('*')->strtolower()->ucfirst();
assert($result->getArrayCopy() === array('Apple', 'Orange', 'Lemon'));
assert((array) $result === array('Apple', 'Orange', 'Lemon'));
The following code returns the same result as the above:, (*4)
// ...
$result = $walker->map(function ($name) {
return ucfirst(strtolower(trim($name, '*')));
});
Example 2:, (*5)
<?php
$dom = new \DOMDocument();
$dom->loadXML('<users><user>Alice</user><user>Bob</user></users>');
$users = $dom->getElementsByTagName('user');
$walker = new \Noi\Util\ArrayWalker($users);
$walker->setAttribute('type', 'engineer');
assert(trim($dom->saveHtml()) ==
'<users><user type="engineer">Alice</user><user type="engineer">Bob</user></users>');
The following code returns the same result as the above:, (*6)
// ...
$walker->walk(function ($node) {
$node->setAttribute('type', 'engineer');
});
ArrayWalker is licensed under the MIT License - see the LICENSE file for details., (*7)