Skip to content

Commit e5fee8c

Browse files
committed
Added first element filter support.
1 parent 9c2afaa commit e5fee8c

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-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

0 commit comments

Comments
 (0)