From 9cc3cd9de3d36dc5ef43e0f8aa2613c1acf350a8 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 30 Mar 2024 23:02:48 +0700 Subject: [PATCH 1/9] [Finder] Add optional pass limit to Finder::rows() to allow only gather maximum until provided limit --- src/Finder.php | 10 +++++++++- tests/FinderTest.php | 13 +++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Finder.php b/src/Finder.php index 33c56df..d069030 100644 --- a/src/Finder.php +++ b/src/Finder.php @@ -16,6 +16,8 @@ use function key; use function prev; +use const PHP_INT_MAX; + final class Finder { /** @@ -123,10 +125,11 @@ public static function last( * @param callable(mixed $datum, int|string|null $key=): bool $filter * @return mixed[] */ - public static function rows(iterable $data, callable $filter, bool $preserveKey = false): array + public static function rows(iterable $data, callable $filter, bool $preserveKey = false, int $limit = PHP_INT_MAX): array { $rows = []; $newKey = 0; + $total = 0; foreach ($data as $key => $datum) { $isFound = $filter($datum, $key); @@ -146,6 +149,11 @@ public static function rows(iterable $data, callable $filter, bool $preserveKey } $rows[$rowKey] = $datum; + + ++$total; + if ($total === $limit) { + break; + } } return $rows; diff --git a/tests/FinderTest.php b/tests/FinderTest.php index a36ab12..a58b450 100644 --- a/tests/FinderTest.php +++ b/tests/FinderTest.php @@ -323,4 +323,17 @@ public static function rowsDataProviderPreserveKey(): array ], ]; } + + public function testRowsWithLimit(): void + { + $data = [1, 2]; + $filter = static fn ($datum): bool => $datum >= 0; + + $this->assertSame( + [ + 1, + ], + Finder::rows($data, $filter, limit: 1) + ); + } } From fb7e50c9ffe827872d3849495ad8c7375fe9a3df Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 30 Mar 2024 23:05:18 +0700 Subject: [PATCH 2/9] cs fix --- src/Finder.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Finder.php b/src/Finder.php index d069030..8b7c862 100644 --- a/src/Finder.php +++ b/src/Finder.php @@ -125,8 +125,12 @@ public static function last( * @param callable(mixed $datum, int|string|null $key=): bool $filter * @return mixed[] */ - public static function rows(iterable $data, callable $filter, bool $preserveKey = false, int $limit = PHP_INT_MAX): array - { + public static function rows( + iterable $data, + callable $filter, + bool $preserveKey = false, + int $limit = PHP_INT_MAX + ): array { $rows = []; $newKey = 0; $total = 0; From f68f5a90541e5dd04ae2faacc70ae7b2784a8df8 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 30 Mar 2024 23:15:20 +0700 Subject: [PATCH 3/9] make limit nullable --- src/Finder.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Finder.php b/src/Finder.php index 8b7c862..e412e5b 100644 --- a/src/Finder.php +++ b/src/Finder.php @@ -129,7 +129,7 @@ public static function rows( iterable $data, callable $filter, bool $preserveKey = false, - int $limit = PHP_INT_MAX + ?int $limit = null ): array { $rows = []; $newKey = 0; @@ -154,6 +154,10 @@ public static function rows( $rows[$rowKey] = $datum; + if ($limit === null) { + continue; + } + ++$total; if ($total === $limit) { break; From a386f4ee8634b537630984ad7f27827f48d28d81 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 30 Mar 2024 23:15:57 +0700 Subject: [PATCH 4/9] cs fix --- src/Finder.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Finder.php b/src/Finder.php index e412e5b..e7923e8 100644 --- a/src/Finder.php +++ b/src/Finder.php @@ -16,8 +16,6 @@ use function key; use function prev; -use const PHP_INT_MAX; - final class Finder { /** From 04b29efeef0af2467d2ce4cfa4a7f1cb79933ded Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 30 Mar 2024 23:19:12 +0700 Subject: [PATCH 5/9] rename var --- src/Finder.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Finder.php b/src/Finder.php index e7923e8..754988e 100644 --- a/src/Finder.php +++ b/src/Finder.php @@ -129,9 +129,9 @@ public static function rows( bool $preserveKey = false, ?int $limit = null ): array { - $rows = []; - $newKey = 0; - $total = 0; + $rows = []; + $newKey = 0; + $totalFound = 0; foreach ($data as $key => $datum) { $isFound = $filter($datum, $key); @@ -156,8 +156,8 @@ public static function rows( continue; } - ++$total; - if ($total === $limit) { + ++$totalFound; + if ($totalFound === $limit) { break; } } From bcc2970884b849520bf5b3022db80d6e32949cc3 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 30 Mar 2024 23:23:01 +0700 Subject: [PATCH 6/9] doc --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index a10d8e6..61de3fc 100644 --- a/README.md +++ b/README.md @@ -356,4 +356,12 @@ var_dump(Finder::rows( $data, static fn($datum, $key): bool => $datum > 6 && $key > 1 )); // [8, 9] + + +// WITH gather only limited found data +$data = [1, 2]; +var_dump(Finder::rows( + $data, + static fn($datum): bool => $datum >= 0 +)); // [1] ``` \ No newline at end of file From a3205c87e0c7e6c8b6f805bafea0def6a29b2914 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 30 Mar 2024 23:23:52 +0700 Subject: [PATCH 7/9] doc --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 61de3fc..fe408fb 100644 --- a/README.md +++ b/README.md @@ -362,6 +362,7 @@ var_dump(Finder::rows( $data = [1, 2]; var_dump(Finder::rows( $data, - static fn($datum): bool => $datum >= 0 + static fn($datum): bool => $datum >= 0, + limit: 1 )); // [1] ``` \ No newline at end of file From 9594786f8e656a3359f1114f95d5da0e063181bf Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 30 Mar 2024 23:24:46 +0700 Subject: [PATCH 8/9] doc --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index fe408fb..3645372 100644 --- a/README.md +++ b/README.md @@ -360,9 +360,9 @@ var_dump(Finder::rows( // WITH gather only limited found data $data = [1, 2]; -var_dump(Finder::rows( - $data, - static fn($datum): bool => $datum >= 0, - limit: 1 -)); // [1] +$filter = static fn($datum): bool => $datum >= 0; + +var_dump( + Finder::rows($data, $filter, limit: 1) +); // [1] ``` \ No newline at end of file From 0e257eea5a95a77e2115c44e1c76f5ddee58be12 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 30 Mar 2024 23:25:16 +0700 Subject: [PATCH 9/9] doc --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3645372..a7f2c69 100644 --- a/README.md +++ b/README.md @@ -361,8 +361,9 @@ var_dump(Finder::rows( // WITH gather only limited found data $data = [1, 2]; $filter = static fn($datum): bool => $datum >= 0; +$limit = 1; var_dump( - Finder::rows($data, $filter, limit: 1) + Finder::rows($data, $filter, limit: $limit) ); // [1] ``` \ No newline at end of file