diff --git a/src/AtLeast.php b/src/AtLeast.php index b2dcb61..a3802ae 100644 --- a/src/AtLeast.php +++ b/src/AtLeast.php @@ -48,6 +48,10 @@ private static function atLeastFoundTimes( // usage must be higher than 0 Assert::greaterThan($maxCount, 0); + if (Counter::count($data) < $maxCount) { + return false; + } + $totalFound = 0; foreach ($data as $key => $datum) { $isFound = $filter($datum, $key); diff --git a/src/Counter.php b/src/Counter.php new file mode 100644 index 0000000..9a57e60 --- /dev/null +++ b/src/Counter.php @@ -0,0 +1,30 @@ +|Traversable $data + */ + public static function count(iterable $data): int + { + // php 8.1 compat + if (is_array($data)) { + return count($data); + } + + return iterator_count($data); + } +} diff --git a/src/Only.php b/src/Only.php index 2f712a8..25181ce 100644 --- a/src/Only.php +++ b/src/Only.php @@ -48,6 +48,10 @@ private static function onlyFoundTimes( // usage must be higher than 0 Assert::greaterThan($maxCount, 0); + if (Counter::count($data) < $maxCount) { + return false; + } + $totalFound = 0; foreach ($data as $key => $datum) { $isFound = $filter($datum, $key); diff --git a/tests/AtLeastTest.php b/tests/AtLeastTest.php index a054dfd..df85c08 100644 --- a/tests/AtLeastTest.php +++ b/tests/AtLeastTest.php @@ -4,6 +4,7 @@ namespace ArrayLookup\Tests; +use ArrayIterator; use ArrayLookup\AtLeast; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; @@ -46,7 +47,7 @@ public static function onceDataProvider(): array } #[DataProvider('twiceDataProvider')] - public function testTwice(array $data, callable $filter, bool $expected): void + public function testTwice(iterable $data, callable $filter, bool $expected): void { $this->assertSame( $expected, @@ -78,6 +79,46 @@ public static function twiceDataProvider(): array static fn(string $datum, int $key): bool => $datum !== 'abc' && $key > 1, false ], + [ + [1], + static fn($datum): bool => $datum == 1, + false, + ], + [ + new ArrayIterator([1]), + static fn($datum): bool => $datum == 1, + false, + ], + [ + [1, "1"], + static fn($datum): bool => $datum == 1, + true, + ], + [ + new ArrayIterator([1, "1"]), + static fn($datum): bool => $datum == 1, + true, + ], + [ + [1, true], + static fn($datum): bool => $datum == 1, + true, + ], + [ + new ArrayIterator([1, true]), + static fn($datum): bool => $datum == 1, + true, + ], + [ + [1, true, "1"], + static fn($datum): bool => $datum == 1, + true, + ], + [ + new ArrayIterator([1, true, "1"]), + static fn($datum): bool => $datum == 1, + true, + ], ]; } diff --git a/tests/OnlyTest.php b/tests/OnlyTest.php index d9c3bc5..50cef18 100644 --- a/tests/OnlyTest.php +++ b/tests/OnlyTest.php @@ -4,6 +4,7 @@ namespace ArrayLookup\Tests; +use ArrayIterator; use ArrayLookup\Only; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; @@ -49,7 +50,7 @@ public static function onceDataProvider(): array // phpcs:enable #[DataProvider('twiceDataProvider')] - public function testTwice(array $data, callable $filter, bool $expected): void + public function testTwice(iterable $data, callable $filter, bool $expected): void { $this->assertSame( $expected, @@ -81,6 +82,46 @@ public static function twiceDataProvider(): array static fn(string $datum, int $key): bool => $datum !== 'abc' && $key > 1, false ], + [ + [1], + static fn($datum): bool => $datum == 1, + false, + ], + [ + new ArrayIterator([1]), + static fn($datum): bool => $datum == 1, + false, + ], + [ + [1, "1"], + static fn($datum): bool => $datum == 1, + true, + ], + [ + new ArrayIterator([1, "1"]), + static fn($datum): bool => $datum == 1, + true, + ], + [ + [1, true], + static fn($datum): bool => $datum == 1, + true, + ], + [ + new ArrayIterator([1, true]), + static fn($datum): bool => $datum == 1, + true, + ], + [ + [1, true, "1"], + static fn($datum): bool => $datum == 1, + false, + ], + [ + new ArrayIterator([1, true, "1"]), + static fn($datum): bool => $datum == 1, + false, + ], ]; }