Skip to content

Commit 375b35b

Browse files
committed
feat: add find method
1 parent 08ef1e5 commit 375b35b

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/Type/Collection.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Constructo\Type;
66

7+
use Closure;
78
use Constructo\Contract\Collectable;
89
use Constructo\Contract\Exportable;
910
use Constructo\Support\Datum;
@@ -37,6 +38,20 @@ final public function push(object $datum): void
3738
$this->data[] = $this->validate($datum);
3839
}
3940

41+
/**
42+
* @return T|null
43+
*/
44+
public function find(Closure $closure): mixed
45+
{
46+
foreach ($this->data as $datum) {
47+
$matches = $closure($datum);
48+
if ($matches === true) {
49+
return $datum;
50+
}
51+
}
52+
return null;
53+
}
54+
4055
abstract protected function validate(mixed $datum): mixed;
4156

4257
protected function exception(string $type, mixed $datum): DomainException

0 commit comments

Comments
 (0)