Skip to content

Commit ab214bf

Browse files
committedJan 9, 2023
add Generator example
1 parent 80d653a commit ab214bf

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed
 

‎src/Finder.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace ArrayLookup;
66

77
use ArrayIterator;
8+
use ArrayObject;
89
use Traversable;
910
use Webmozart\Assert\Assert;
1011

@@ -44,7 +45,7 @@ public static function first(iterable $data, callable $filter): mixed
4445
*/
4546
private static function resolveArrayFromTraversable(Traversable $traversable): array
4647
{
47-
if ($traversable instanceof ArrayIterator) {
48+
if ($traversable instanceof ArrayIterator || $traversable instanceof ArrayObject) {
4849
return $traversable->getArrayCopy();
4950
}
5051

‎tests/FinderTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use ArrayIterator;
88
use ArrayLookup\Finder;
99
use ArrayObject;
10+
use Generator;
1011
use PHPUnit\Framework\TestCase;
1112

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

5253
public function lastDataProvider(): array
5354
{
55+
$generator = static function (): Generator {
56+
yield 6;
57+
yield 7;
58+
yield 8;
59+
yield 9;
60+
};
61+
5462
return [
5563
[
5664
[6, 7, 8, 9],
@@ -82,6 +90,16 @@ public function lastDataProvider(): array
8290
static fn($datum): bool => $datum < 5,
8391
null,
8492
],
93+
[
94+
$generator(),
95+
static fn($datum): bool => $datum > 5,
96+
9,
97+
],
98+
[
99+
$generator(),
100+
static fn($datum): bool => $datum < 5,
101+
null,
102+
],
85103
];
86104
}
87105
}

0 commit comments

Comments
 (0)