ClosureSearching::checkWith()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Stratadox\ImmutableCollection;
5
6
use Closure;
7
use Stratadox\Collection\ClosureSearchable;
8
use Stratadox\Collection\Collection;
9
use Stratadox\Collection\NotFound;
10
11
trait ClosureSearching
12
{
13
    /**
14
     * @see ClosureSearchable::findOneWith()
15
     * @throws NotFound
16
     * @param Closure $function
17
     * @return mixed|object
18
     */
19
    public function findOneWith(Closure $function)
20
    {
21
        foreach ($this->items() as $item) {
22
            if ($function->call($item, $item)) {
23
                return $item;
24
            }
25
        }
26
        /** @var Collection $this */
27
        throw NoSuchValue::noneOfTheValuesMatched($this);
28
    }
29
30
    /**
31
     * @see ClosureSearchable::findOneWith()
32
     * @param Closure $function
33
     * @return bool
34
     */
35
    public function checkWith(Closure $function): bool
36
    {
37
        foreach ($this->items() as $item) {
38
            if ($function->call($item, $item)) {
39
                return true;
40
            }
41
        }
42
        return false;
43
    }
44
45
    /** @see Collection::items() */
46
    abstract public function items(): array;
47
}
48