From fb1fbc23d6306dcda5407a6e1529e22872eb6182 Mon Sep 17 00:00:00 2001 From: Basil Date: Mon, 9 Dec 2019 20:17:10 +0100 Subject: [PATCH 1/2] add keys --- core/CHANGELOG.md | 1 + core/helpers/ArrayHelper.php | 20 +++++++++++++++++--- tests/core/helpers/ArrayHelperTest.php | 9 +++++++++ tests/data/configs/web.php | 13 +++++++++++++ 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index 2628eee3d..8ee864314 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -5,6 +5,7 @@ In order to read more about upgrading and BC breaks have a look at the [UPGRADE ## 1.0.24 ++ [#]() Added `ArrayHelper::search()` keys option to search only in certain array keys. + [#1969](https://github.com/luyadev/luya/pull/1969) Fixed exception handling while loading empty theme directories. + [#1977](https://github.com/luyadev/luya/pull/1977) Added new `ArrayHelper::combine()` method to generate an array with the same keys and values. + [#1977](https://github.com/luyadev/luya/pull/1978) Added support for ActiveForm context to SubmitButtonWidget. Supporting multi form (including pjax) on same page. diff --git a/core/helpers/ArrayHelper.php b/core/helpers/ArrayHelper.php index 02add2c4a..5605fd066 100644 --- a/core/helpers/ArrayHelper.php +++ b/core/helpers/ArrayHelper.php @@ -140,21 +140,35 @@ public static function typeCast(array $array) * ``` * * Searching for the string `Bar` would return the the orignal array is bar would be found in both. + * + * In order to search only in certain keys defined $keys attribute: + * + * ```php + * ArrayHelper::search($data, 'Foo', false, ['name']); + * ``` + * + * The above example will search only in the array key `name` for the `Foo` expression. * * @param array $array The multidimensional array keys. * @param string $searchText The text you where search inside the rows. * @param boolean $sensitive Whether to use strict sensitive search (true) or case insenstivie search (false). + * @param array $keys A list of array keys which should be taken to search in, if empty or not provided it will lookup all array keys by default. {@since 2.0.14} * @return array The modified array depending on the search result hits. */ - public static function search(array $array, $searchText, $sensitive = false) + public static function search(array $array, $searchText, $sensitive = false, array $keys = []) { - $function = ($sensitive) ? 'strpos' : 'stripos'; - return array_filter($array, function ($item) use ($searchText, $function) { + $function = $sensitive ? 'strpos' : 'stripos'; + return array_filter($array, function ($item) use ($searchText, $function, $keys) { $response = false; foreach ($item as $key => $value) { if ($response) { continue; } + + if (!empty($keys) && !in_array($key, $keys)) { + continue; + } + if ($function($value, "$searchText") !== false) { $response = true; } diff --git a/tests/core/helpers/ArrayHelperTest.php b/tests/core/helpers/ArrayHelperTest.php index 1c5076a1a..d82c879be 100644 --- a/tests/core/helpers/ArrayHelperTest.php +++ b/tests/core/helpers/ArrayHelperTest.php @@ -106,6 +106,15 @@ public function testSearch() $this->assertSame(2, count(ArrayHelper::search($data, 'fo'))); $this->assertSame(1, count(ArrayHelper::search($data, 'Foo', true))); + + $this->assertSame([], ArrayHelper::search($data, 1, false, ['name'])); + $this->assertSame([ + [ + 'name' => 'Foo Bar', + 'description' => 'same', + 'id' => 1, + ] + ], ArrayHelper::search($data, 1, false, ['id'])); } public function testSearchColumn() diff --git a/tests/data/configs/web.php b/tests/data/configs/web.php index f37a8b017..e9765ba38 100644 --- a/tests/data/configs/web.php +++ b/tests/data/configs/web.php @@ -1,4 +1,17 @@ 'testenv', 'siteTitle' => 'Luya Tests', From 52fe65269996440aab1d23a17e219666416d7567 Mon Sep 17 00:00:00 2001 From: Basil Date: Mon, 9 Dec 2019 20:17:53 +0100 Subject: [PATCH 2/2] update changelog --- core/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index 8ee864314..618bbb5f4 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -5,7 +5,7 @@ In order to read more about upgrading and BC breaks have a look at the [UPGRADE ## 1.0.24 -+ [#]() Added `ArrayHelper::search()` keys option to search only in certain array keys. ++ [#1980](https://github.com/luyadev/luya/pull/1980) Added `ArrayHelper::search()` keys option to search only in certain array keys. + [#1969](https://github.com/luyadev/luya/pull/1969) Fixed exception handling while loading empty theme directories. + [#1977](https://github.com/luyadev/luya/pull/1977) Added new `ArrayHelper::combine()` method to generate an array with the same keys and values. + [#1977](https://github.com/luyadev/luya/pull/1978) Added support for ActiveForm context to SubmitButtonWidget. Supporting multi form (including pjax) on same page.