From c39cf2d55fadab448a303a72bee7757cc62844b0 Mon Sep 17 00:00:00 2001 From: Nicola Fontana Date: Mon, 21 Sep 2015 14:30:12 +0200 Subject: [PATCH] Do not hang on nested parameters in search context --- admin/code/ModelAdmin.php | 2 +- core/ArrayLib.php | 17 +++++++++++++++++ tests/core/ArrayLibTest.php | 23 +++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/admin/code/ModelAdmin.php b/admin/code/ModelAdmin.php index 3f35a7d2e..037e9d6af 100644 --- a/admin/code/ModelAdmin.php +++ b/admin/code/ModelAdmin.php @@ -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); diff --git a/core/ArrayLib.php b/core/ArrayLib.php index e51eed23e..e582be118 100644 --- a/core/ArrayLib.php +++ b/core/ArrayLib.php @@ -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. * diff --git a/tests/core/ArrayLibTest.php b/tests/core/ArrayLibTest.php index da79a3672..f335c849a 100644 --- a/tests/core/ArrayLibTest.php +++ b/tests/core/ArrayLibTest.php @@ -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',