mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge pull request #4619 from ntd/pr2
Allow to handle nested params in search context
This commit is contained in:
commit
0b5a57389b
@ -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);
|
||||
|
@ -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.
|
||||
*
|
||||
|
@ -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',
|
||||
|
Loading…
Reference in New Issue
Block a user