Skip to content

Commit

Permalink
add Generator example
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Jan 9, 2023
1 parent 80d653a commit ab214bf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace ArrayLookup;

use ArrayIterator;
use ArrayObject;
use Traversable;
use Webmozart\Assert\Assert;

Expand Down Expand Up @@ -44,7 +45,7 @@ public static function first(iterable $data, callable $filter): mixed
*/
private static function resolveArrayFromTraversable(Traversable $traversable): array
{
if ($traversable instanceof ArrayIterator) {
if ($traversable instanceof ArrayIterator || $traversable instanceof ArrayObject) {
return $traversable->getArrayCopy();
}

Expand Down
18 changes: 18 additions & 0 deletions tests/FinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use ArrayIterator;
use ArrayLookup\Finder;
use ArrayObject;
use Generator;
use PHPUnit\Framework\TestCase;

final class FinderTest extends TestCase
Expand Down Expand Up @@ -51,6 +52,13 @@ public function testLast(iterable $data, callable $filter, mixed $expected): voi

public function lastDataProvider(): array
{
$generator = static function (): Generator {
yield 6;
yield 7;
yield 8;
yield 9;
};

return [
[
[6, 7, 8, 9],
Expand Down Expand Up @@ -82,6 +90,16 @@ public function lastDataProvider(): array
static fn($datum): bool => $datum < 5,
null,
],
[
$generator(),
static fn($datum): bool => $datum > 5,
9,
],
[
$generator(),
static fn($datum): bool => $datum < 5,
null,
],
];
}
}

0 comments on commit ab214bf

Please sign in to comment.