From cc9cfacc7459f2257e0c9f236946d2d75d1099bc Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 7 Sep 2024 22:33:43 +0700 Subject: [PATCH 1/4] Allow optional when on Collector class --- src/Collector.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Collector.php b/src/Collector.php index 1376203..4fd8317 100644 --- a/src/Collector.php +++ b/src/Collector.php @@ -12,10 +12,10 @@ final class Collector /** @var array|Traversable */ private iterable $data = []; - /** @var callable(mixed $datum, int|string|null $key=): bool|null */ + /** @var null|callable(mixed $datum, int|string|null $key=): bool */ private $when; - /** @var callable(mixed $datum, int|string|null $key=): mixed */ + /** @var null|callable(mixed $datum, int|string|null $key=): mixed */ private $transform; private ?int $limit = null; @@ -62,20 +62,22 @@ public function withTransform(callable $transform): self */ public function getResults(): array { - // ensure when property is set early via ->when() and ->withTransform() method - Assert::isCallable($this->when); + // ensure transform property is set early ->withTransform() method Assert::isCallable($this->transform); $count = 0; $collectedData = []; + $isCallableWhen = is_callable($this-when); foreach ($this->data as $key => $datum) { - $isFound = ($this->when)($datum, $key); + if ($isCallableWhen) { + $isFound = ($this->when)($datum, $key); - Assert::boolean($isFound); + Assert::boolean($isFound); - if (! $isFound) { - continue; + if (! $isFound) { + continue; + } } $collectedData[] = ($this->transform)($datum, $key); From 1d7ad8d6f1f919f3748ab10f7897a8625082fd6b Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 7 Sep 2024 23:28:38 +0700 Subject: [PATCH 2/4] add test --- README.md | 2 +- src/Collector.php | 8 +++++--- tests/CollectorTest.php | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f372d5b..f59b939 100644 --- a/README.md +++ b/README.md @@ -396,7 +396,7 @@ $limit = 2; $transform = fn ($datum): string => trim($datum); $newArray = Collector::setUp($data) - ->when($when) + ->when($when) // optional, can just transform without filtering ->withLimit(2) // optional to only collect some data provided by limit config ->withTransform($transform) ->getResults(); diff --git a/src/Collector.php b/src/Collector.php index 4fd8317..47c0d10 100644 --- a/src/Collector.php +++ b/src/Collector.php @@ -7,6 +7,8 @@ use Traversable; use Webmozart\Assert\Assert; +use function is_callable; + final class Collector { /** @var array|Traversable */ @@ -65,9 +67,9 @@ public function getResults(): array // ensure transform property is set early ->withTransform() method Assert::isCallable($this->transform); - $count = 0; - $collectedData = []; - $isCallableWhen = is_callable($this-when); + $count = 0; + $collectedData = []; + $isCallableWhen = is_callable($this->when); foreach ($this->data as $key => $datum) { if ($isCallableWhen) { diff --git a/tests/CollectorTest.php b/tests/CollectorTest.php index 6b1b82d..af4a551 100644 --- a/tests/CollectorTest.php +++ b/tests/CollectorTest.php @@ -13,6 +13,21 @@ final class CollectorTest extends TestCase { + public function testWithoutWhen(): void + { + $data = [ + ' a ', + ' b ', + ' c ', + ]; + + $results = Collector::setUp($data) + ->withTransform(fn (string $datum): string => trim($datum)) + ->getResults(); + + $this->assertSame(['a', 'b', 'c'], $results); + } + public function testWithoutLimit(): void { $data = [ From 28455dd70befffdbe54712905ff6f530e966b6b2 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 7 Sep 2024 23:34:40 +0700 Subject: [PATCH 3/4] Fix phpstan --- src/Collector.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Collector.php b/src/Collector.php index 47c0d10..18a866c 100644 --- a/src/Collector.php +++ b/src/Collector.php @@ -70,10 +70,12 @@ public function getResults(): array $count = 0; $collectedData = []; $isCallableWhen = is_callable($this->when); + $when = $this->when; foreach ($this->data as $key => $datum) { if ($isCallableWhen) { - $isFound = ($this->when)($datum, $key); + /** @var callable(mixed $datum, int|string|null $key=): bool $when */ + $isFound = ($when)($datum, $key); Assert::boolean($isFound); From f423102b7fed8b443939296ec80ce3764ffc31ce Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 7 Sep 2024 23:36:16 +0700 Subject: [PATCH 4/4] Fix phpstan --- src/Collector.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Collector.php b/src/Collector.php index 18a866c..46d2106 100644 --- a/src/Collector.php +++ b/src/Collector.php @@ -70,11 +70,13 @@ public function getResults(): array $count = 0; $collectedData = []; $isCallableWhen = is_callable($this->when); - $when = $this->when; foreach ($this->data as $key => $datum) { if ($isCallableWhen) { - /** @var callable(mixed $datum, int|string|null $key=): bool $when */ + /** + * @var callable(mixed $datum, int|string|null $key=): bool $when + */ + $when = $this->when; $isFound = ($when)($datum, $key); Assert::boolean($isFound);