Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
TraitCollectionSearch
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
4 / 4
10
100.00% covered (success)
100.00%
1 / 1
 find
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 every
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 some
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 contains
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3// ╔════════════════════════════════════════════════════════════╗
4// ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║
5// ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com>     ║
6// ╚════════════════════════════════════════════════════════════╝
7
8declare(strict_types=1);
9
10namespace Phexium\Domain\Collection\Trait;
11
12trait TraitCollectionSearch
13{
14    public function find(callable $fn): mixed
15    {
16        foreach ($this->collection as $index => $element) {
17            if ($fn($element, $index)) {
18                return $element;
19            }
20        }
21
22        return false;
23    }
24
25    public function every(callable $fn): bool
26    {
27        foreach ($this->collection as $index => $element) {
28            if (!$fn($element, $index)) {
29                return false;
30            }
31        }
32
33        return true;
34    }
35
36    public function some(callable $fn): bool
37    {
38        foreach ($this->collection as $index => $element) {
39            if ($fn($element, $index, $this->collection)) {
40                return true;
41            }
42        }
43
44        return false;
45    }
46
47    public function contains(mixed $element): bool
48    {
49        return $this->some(fn ($e): bool => $e === $element);
50    }
51}