Skip to content

Commit 6f2c28c

Browse files
authored
Merge pull request #21 from worksolutions/first-element-filter
First element filter support
2 parents f0c7765 + 8de3bbd commit 6f2c28c

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

src/WS/Utils/Collections/DummyStreamDecorator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function findAny()
6363
return $this->decoratedStream->findAny();
6464
}
6565

66-
public function findFirst()
66+
public function findFirst(callable $filter = null)
6767
{
6868
return $this->decoratedStream->findFirst();
6969
}

src/WS/Utils/Collections/SerialStream.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,20 @@ public function findAny()
205205
/**
206206
* @inheritDoc
207207
*/
208-
public function findFirst()
208+
public function findFirst(callable $filter = null)
209209
{
210+
if (!$filter) {
211+
/** @noinspection LoopWhichDoesNotLoopInspection */
212+
foreach ($this->list as $item) {
213+
return $item;
214+
}
215+
return null;
216+
}
210217
/** @noinspection LoopWhichDoesNotLoopInspection */
211218
foreach ($this->list as $item) {
212-
return $item;
219+
if ($filter($item)) {
220+
return $item;
221+
}
213222
}
214223
return null;
215224
}

src/WS/Utils/Collections/Stream.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,10 @@ public function findAny();
7171

7272
/**
7373
* Returns first collection element or null if absent
74+
* @param callable|null $filter
7475
* @return mixed
7576
*/
76-
public function findFirst();
77+
public function findFirst(callable $filter = null);
7778

7879
/**
7980
* Returns last collection element or null if absent

tests/WS/Utils/Collections/SerialStreamTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,15 @@ public function firstLastElementCases(): array
391391
];
392392
}
393393

394+
public function firstFirstElementWithFilterCases(): array
395+
{
396+
return [
397+
[ [1, 2, 3], 2, 2],
398+
[ [1], 2, null],
399+
[[], 2, null]
400+
];
401+
}
402+
394403
/**
395404
* @dataProvider firstLastElementCases
396405
* @test
@@ -405,6 +414,23 @@ public function findFirstElement($input, $first): void
405414
$this->assertEquals($first, $actual);
406415
}
407416

417+
/**
418+
* @dataProvider firstFirstElementWithFilterCases
419+
* @test
420+
* @param array $input
421+
* @param mixed $first
422+
* @param mixed $expected
423+
*/
424+
public function findFirstElementWithFilter(array $input, $first, $expected): void
425+
{
426+
$actual = $this->createCollection($input)
427+
->stream()
428+
->findFirst(function ($item) use ($first) {
429+
return $item === $first;
430+
});
431+
$this->assertEquals($expected, $actual);
432+
}
433+
408434
/** @noinspection PhpUnusedParameterInspection */
409435
/**
410436
* @dataProvider firstLastElementCases

0 commit comments

Comments
 (0)