Do not hang on nested parameters in search context

This commit is contained in:
Nicola Fontana 2015-09-21 14:30:12 +02:00
parent d0fe105045
commit c39cf2d55f
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',