Skip to content

Commit 1f4027d

Browse files
authored
Merge pull request #25 from samsonasik/samsonasik-patch-1
Allow optional when on Collector class
2 parents d3c90d4 + f423102 commit 1f4027d

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ $limit = 2;
396396
$transform = fn ($datum): string => trim($datum);
397397

398398
$newArray = Collector::setUp($data)
399-
->when($when)
399+
->when($when) // optional, can just transform without filtering
400400
->withLimit(2) // optional to only collect some data provided by limit config
401401
->withTransform($transform)
402402
->getResults();

src/Collector.php

+20-12
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@
77
use Traversable;
88
use Webmozart\Assert\Assert;
99

10+
use function is_callable;
11+
1012
final class Collector
1113
{
1214
/** @var array<int|string, mixed>|Traversable<int|string, mixed> */
1315
private iterable $data = [];
1416

15-
/** @var callable(mixed $datum, int|string|null $key=): bool|null */
17+
/** @var null|callable(mixed $datum, int|string|null $key=): bool */
1618
private $when;
1719

18-
/** @var callable(mixed $datum, int|string|null $key=): mixed */
20+
/** @var null|callable(mixed $datum, int|string|null $key=): mixed */
1921
private $transform;
2022

2123
private ?int $limit = null;
@@ -62,20 +64,26 @@ public function withTransform(callable $transform): self
6264
*/
6365
public function getResults(): array
6466
{
65-
// ensure when property is set early via ->when() and ->withTransform() method
66-
Assert::isCallable($this->when);
67+
// ensure transform property is set early ->withTransform() method
6768
Assert::isCallable($this->transform);
6869

69-
$count = 0;
70-
$collectedData = [];
70+
$count = 0;
71+
$collectedData = [];
72+
$isCallableWhen = is_callable($this->when);
7173

7274
foreach ($this->data as $key => $datum) {
73-
$isFound = ($this->when)($datum, $key);
74-
75-
Assert::boolean($isFound);
76-
77-
if (! $isFound) {
78-
continue;
75+
if ($isCallableWhen) {
76+
/**
77+
* @var callable(mixed $datum, int|string|null $key=): bool $when
78+
*/
79+
$when = $this->when;
80+
$isFound = ($when)($datum, $key);
81+
82+
Assert::boolean($isFound);
83+
84+
if (! $isFound) {
85+
continue;
86+
}
7987
}
8088

8189
$collectedData[] = ($this->transform)($datum, $key);

tests/CollectorTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@
1313

1414
final class CollectorTest extends TestCase
1515
{
16+
public function testWithoutWhen(): void
17+
{
18+
$data = [
19+
' a ',
20+
' b ',
21+
' c ',
22+
];
23+
24+
$results = Collector::setUp($data)
25+
->withTransform(fn (string $datum): string => trim($datum))
26+
->getResults();
27+
28+
$this->assertSame(['a', 'b', 'c'], $results);
29+
}
30+
1631
public function testWithoutLimit(): void
1732
{
1833
$data = [

0 commit comments

Comments
 (0)