Skip to content

Commit cc9cfac

Browse files
authored
Allow optional when on Collector class
1 parent d3c90d4 commit cc9cfac

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/Collector.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ final class Collector
1212
/** @var array<int|string, mixed>|Traversable<int|string, mixed> */
1313
private iterable $data = [];
1414

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

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

2121
private ?int $limit = null;
@@ -62,20 +62,22 @@ public function withTransform(callable $transform): self
6262
*/
6363
public function getResults(): array
6464
{
65-
// ensure when property is set early via ->when() and ->withTransform() method
66-
Assert::isCallable($this->when);
65+
// ensure transform property is set early ->withTransform() method
6766
Assert::isCallable($this->transform);
6867

6968
$count = 0;
7069
$collectedData = [];
70+
$isCallableWhen = is_callable($this-when);
7171

7272
foreach ($this->data as $key => $datum) {
73-
$isFound = ($this->when)($datum, $key);
73+
if ($isCallableWhen) {
74+
$isFound = ($this->when)($datum, $key);
7475

75-
Assert::boolean($isFound);
76+
Assert::boolean($isFound);
7677

77-
if (! $isFound) {
78-
continue;
78+
if (! $isFound) {
79+
continue;
80+
}
7981
}
8082

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

0 commit comments

Comments
 (0)