Merge pull request #4619 from ntd/pr2

Allow to handle nested params in search context
This commit is contained in:
Damian Mooyman 2015-09-24 12:37:49 +12:00
commit 0b5a57389b
3 changed files with 41 additions and 1 deletions

View File

@ -212,7 +212,7 @@ abstract class ModelAdmin extends LeftAndMain {
$params = $this->getRequest()->requestVar('q');
if(is_array($params)) {
$params = array_map('trim', $params);
$params = ArrayLib::array_map_recursive('trim', $params);
}
$list = $context->getResults($params);

View File

@ -163,6 +163,23 @@ class ArrayLib {
return false;
}
/**
* Similar to array_map, but recurses when arrays are encountered.
*
* Actually only one array argument is supported.
*
* @param $f callback to apply
* @param $array array
* @return array
*/
public static function array_map_recursive($f, $array) {
$applyOrRecurse = function($v) use($f) {
return is_array($v) ? ArrayLib::array_map_recursive($f, $v) : call_user_func($f, $v);
};
return array_map($applyOrRecurse, $array);
}
/**
* Recursively merges two or more arrays.
*

View File

@ -48,6 +48,29 @@ class ArrayLibTest extends SapphireTest {
);
}
public function testArrayMapRecursive() {
$array = array(
'a ',
array(' b', 'c'),
);
$strtoupper = array(
'A ',
array(' B', 'C'),
);
$trim = array(
'a',
array('b', 'c'),
);
$this->assertEquals(
$strtoupper,
ArrayLib::array_map_recursive('strtoupper', $array)
);
$this->assertEquals(
$trim,
ArrayLib::array_map_recursive('trim', $array)
);
}
public function testArrayMergeRecursive() {
$first = array(
'first' => 'a',