-
-
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
Open
yus-ham
wants to merge
20
commits into
yiisoft:master
Choose a base branch
from
yus-ham:enh-getValue
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
a4e40b3
Implement value retrieval by matcher
yus-ham a3817b6
changelog
yus-ham c7092fb
Fix failing tests
yus-ham de81749
Merge branch 'master' into enh-getValue
vjik 14f0839
Merge branch 'master' of https://github.com/yiisoft/arrays into enh-g…
yus-ham c60cd4b
fix object data handling
yus-ham 2f81148
fix psalm fails
yus-ham 5b1c936
Merge branch 'enh-getValue' of github.com:yus-ham/yii-arrays into enh…
yus-ham c86c0fa
fix phpdocs
yus-ham 20da553
fix psalm fails
yus-ham 0cb4658
Merge remote-tracking branch yii/master into enh-getValue
yus-ham b3dde8e
Revert "fix object data handling"
yus-ham c04e4e8
Revert "Implement value retrieval by matcher"
yus-ham 6402803
Add failing tests
yus-ham 1131c20
Implement `find()`, `findKey()`, `any()` and `all()` methods
yus-ham a4affab
Update readme
yus-ham f47a0fa
fix
yus-ham 160ae19
fix changelog
yus-ham 0cc7a60
Fix API docs
yus-ham 6135f7f
use `callable`
yus-ham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.