Passed
Push — master ( 6f2c28...9e5d31 )
by Maxim
02:59 queued 11s
created

BoolExpression::or()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * @author Anton Lytkin <[email protected]>
4
 */
5
6
namespace WS\Utils\Collections\Functions\Expression;
7
8
use WS\Utils\Collections\Functions\Expression\Operator\AbstractOperator;
9
use WS\Utils\Collections\Functions\Expression\Operator\AndOperator;
10
use WS\Utils\Collections\Functions\Expression\Operator\OrOperator;
11
12
class BoolExpression
13
{
14
15
    /** @var AbstractOperator[] */
16
    private $operators = [];
17
18
    public function __construct(callable $checker)
19
    {
20
        $this->operators[] = new AndOperator($checker);
21
    }
22
23
    public static function with(callable $checker): self
24
    {
25
        return new self($checker);
26
    }
27
28 1
    public function __invoke($item): bool
29
    {
30 1
        $operand = null;
31 1
        foreach ($this->operators as $operator) {
32 1
            if ($operand === null) {
33 1
                if (!$operand = $operator->getChecker()($item)) {
34
                    return false;
35
                }
36 1
                continue;
37
            }
38 1
            if (!$operand = $operator($operand, $item)) {
39 1
                return false;
40
            }
41
        }
42 1
        return true;
43
    }
44
45
    public function and(callable $checker): self
46
    {
47
        $this->operators[] = new AndOperator($checker);
48
        return $this;
49
    }
50
51
    public function or(callable $checker): self
52
    {
53
        $this->operators[] = new OrOperator($checker);
54
        return $this;
55
    }
56
57
}
58