Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add keys #1980

Merged
merged 2 commits into from
Dec 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ In order to read more about upgrading and BC breaks have a look at the [UPGRADE

## 1.0.24

+ [#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.
Expand Down
20 changes: 17 additions & 3 deletions core/helpers/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
9 changes: 9 additions & 0 deletions tests/core/helpers/ArrayHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
13 changes: 13 additions & 0 deletions tests/data/configs/web.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
<?php

if (!defined('DB_DSN')) {
define('DB_DSN', 'foo'); // presevers errors
}

if (!defined('DB_USER')) {
define('DB_USER', 'foo'); // presevers errors
}

if (!defined('DB_PASS')) {
define('DB_PASS', 'foo'); // presevers errors
}

return [
'id' => 'testenv',
'siteTitle' => 'Luya Tests',
Expand Down