-
-
Notifications
You must be signed in to change notification settings - Fork 28
Implement value retrieval and checks by predicate callback #142
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
base: master
Are you sure you want to change the base?
Changes from all commits
a4e40b3
a3817b6
c7092fb
de81749
14f0839
c60cd4b
2f81148
5b1c936
c86c0fa
20da553
0cb4658
b3dde8e
c04e4e8
6402803
1131c20
a4affab
f47a0fa
160ae19
0cc7a60
6135f7f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -753,7 +753,7 @@ | |
foreach ($array as $element) { | ||
if (!is_array($element) && !is_object($element)) { | ||
throw new InvalidArgumentException( | ||
'index() can not get value from ' . gettype($element) . | ||
Check warning on line 756 in src/ArrayHelper.php
|
||
'. The $array should be either multidimensional array or an array of objects.' | ||
); | ||
} | ||
|
@@ -937,7 +937,7 @@ | |
* | ||
* @return bool Whether the array contains the specified key. | ||
*/ | ||
public static function keyExists(array $array, array|float|int|string $key, bool $caseSensitive = true): bool | ||
Check warning on line 940 in src/ArrayHelper.php
|
||
{ | ||
if (is_array($key)) { | ||
if (count($key) === 1) { | ||
|
@@ -1010,7 +1010,7 @@ | |
public static function pathExists( | ||
array $array, | ||
array|float|int|string $path, | ||
bool $caseSensitive = true, | ||
Check warning on line 1013 in src/ArrayHelper.php
|
||
string $delimiter = '.' | ||
): bool { | ||
return self::keyExists($array, self::parseMixedPath($path, $delimiter), $caseSensitive); | ||
|
@@ -1311,7 +1311,7 @@ | |
$numNestedKeys = count($keys) - 1; | ||
foreach ($keys as $i => $key) { | ||
if (!is_array($excludeNode) || !array_key_exists($key, $excludeNode)) { | ||
continue 2; // Jump to next filter. | ||
Check warning on line 1314 in src/ArrayHelper.php
|
||
} | ||
|
||
if ($i < $numNestedKeys) { | ||
|
@@ -1319,7 +1319,7 @@ | |
$excludeNode = &$excludeNode[$key]; | ||
} else { | ||
unset($excludeNode[$key]); | ||
break; | ||
Check warning on line 1322 in src/ArrayHelper.php
|
||
} | ||
} | ||
} | ||
|
@@ -1436,4 +1436,80 @@ | |
|
||
return is_string($path) ? StringHelper::parsePath($path, $delimiter) : $path; | ||
} | ||
|
||
/** | ||
* Get the first element in an array that pass the test implemented by the provided callback. | ||
* | ||
* @param array $array The array that should be searched. | ||
* @param Closure $predicate The predicate callback to call to check each element. The first parameter contains the value, the second parameter contains the corresponding key. If this function returns truthy value, the value is returned from `find()` and the callback will not be called for further elements. | ||
* | ||
* @return mixed The value of the first element for which the `$predicate` callback returns true. If no matching element is found the function returns `null`. | ||
*/ | ||
public static function find(array $array, callable $predicate): mixed | ||
{ | ||
foreach ($array as $key => $value) { | ||
if ($predicate($value, $key)) { | ||
return $value; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Get the key of the first element in an array that pass the test implemented by the provided callback. | ||
* | ||
* @param array The array that should be searched. | ||
* @param Closure $predicate The predicate callback to call to check each element. The first parameter contains the value, the second parameter contains the corresponding key. If this function returns truthy value, the key is returned from `findKey()` and the callback will not be called for further elements. | ||
* | ||
* @return int|string|null The key of the first element for which the `$predicate` callback returns `true`. If no matching element is found the function returns `null`. | ||
*/ | ||
public static function findKey(array $array, callable $predicate): int|string|null | ||
{ | ||
foreach ($array as $key => $value) { | ||
if ($predicate($value, $key)) { | ||
return $key; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Check whether at least one element in an array pass the test implemented by the provided callback. | ||
* | ||
* @param array The array which each element will be tested against callback. | ||
* @param Closure $predicate The predicate callback to call to check each element. The first parameter contains the value, the second parameter contains the corresponding key. If this function returns truthy value, `true` is returned from `any()` and the callback will not be called for further elements. | ||
* | ||
* @return bool `true` if one element for which predicate callback returns truthy value. Otherwise the function returns `false`. | ||
*/ | ||
public static function any(array $array, callable $predicate): bool | ||
{ | ||
foreach ($array as $key => $value) { | ||
if ($predicate($value, $key)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Check whether all elements in an array pass the test implemented by the provided callback. | ||
* | ||
* @param array The array which each element will be tested against callback. | ||
* @param Closure $predicate The predicate callback to call to check each element. The first parameter contains the value, the second parameter contains the corresponding key. If this function returns falsy value, `false` is returned from `all()` and the callback will not be called for further elements. | ||
* | ||
* @return bool `false` if one element for which predicate callback returns falsy value. Otherwise the function returns `true`. | ||
*/ | ||
public static function all(array $array, callable $predicate): bool | ||
{ | ||
foreach ($array as $key => $value) { | ||
if (!$predicate($value, $key)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Arrays\Tests\ArrayHelper; | ||
|
||
use Closure; | ||
use PHPUnit\Framework\TestCase; | ||
use Yiisoft\Arrays\ArrayHelper; | ||
|
||
final class FindTest extends TestCase | ||
{ | ||
private array $array = [ | ||
[ | ||
'a' => 1, | ||
'b' => 2, | ||
'c' => 3, | ||
'd' => 4, | ||
'e' => 5, | ||
], | ||
[ | ||
1, 2, 3, 4, 5, | ||
], | ||
]; | ||
|
||
public function dataProviderFindFromArray(): array | ||
{ | ||
return [ | ||
[$this->array[0], fn ($value) => $value > 3, 4], | ||
[$this->array[1], fn ($value) => $value > 3, 4], | ||
[$this->array[1], fn ($value) => $value > 5, null], | ||
[$this->array[0], fn ($value, $key) => $key === 'c', 3], | ||
[$this->array[0], fn () => false, null], | ||
[[], fn () => true, null], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider dataProviderFindFromArray | ||
* | ||
* @param Closure $predicate | ||
* @param $expected | ||
*/ | ||
public function testFind($array, $predicate, $expected): void | ||
{ | ||
$this->assertEquals($expected, ArrayHelper::find($array, $predicate)); | ||
} | ||
|
||
public function dataProviderFindKeyFromArray(): array | ||
{ | ||
return [ | ||
[$this->array[0], fn ($value) => $value > 3, 'd'], | ||
[$this->array[1], fn ($value) => $value > 3, 3], | ||
[$this->array[1], fn ($value) => $value > 5, null], | ||
[$this->array[0], fn ($value, $key) => $key === 'c', 'c'], | ||
[$this->array[0], fn () => false, null], | ||
[[], fn () => true, null], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider dataProviderFindKeyFromArray | ||
* | ||
* @param Closure $predicate | ||
* @param $expected | ||
*/ | ||
public function testFindKey($array, $predicate, $expected): void | ||
{ | ||
$this->assertEquals($expected, ArrayHelper::findKey($array, $predicate)); | ||
} | ||
|
||
public function dataProviderAnyFromArray(): array | ||
{ | ||
return [ | ||
[$this->array[0], fn ($value) => $value > 3, true], | ||
[$this->array[1], fn ($value) => $value > 3, true], | ||
[$this->array[1], fn ($value) => $value > 5, false], | ||
[$this->array[0], fn ($value, $key) => $key === 'c', true], | ||
[$this->array[0], fn () => false, false], | ||
[[], fn () => true, false], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider dataProviderAnyFromArray | ||
* | ||
* @param Closure $predicate | ||
* @param $expected | ||
*/ | ||
public function testAny($array, $predicate, $expected): void | ||
{ | ||
$this->assertEquals($expected, ArrayHelper::any($array, $predicate)); | ||
} | ||
|
||
public function dataProviderAllFromArray(): array | ||
{ | ||
return [ | ||
[$this->array[0], fn ($value) => $value > 0, true], | ||
[$this->array[1], fn ($value) => $value > 0, true], | ||
[$this->array[1], fn ($value) => $value > 1, false], | ||
[[], fn () => true, true], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider dataProviderAllFromArray | ||
* | ||
* @param Closure $predicate | ||
* @param $expected | ||
*/ | ||
public function testAll($array, $predicate, $expected): void | ||
{ | ||
$this->assertEquals($expected, ArrayHelper::all($array, $predicate)); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.