Completed
Push — master ( df9952...f86661 )
by Hannes
04:17 queued 02:11
created

Exactly::evaluate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace hanneskod\yaysondb\Expression\Counter;
6
7
use hanneskod\yaysondb\Expression\Counter;
8
use hanneskod\yaysondb\Expression\ExpressionInterface;
9
10
/**
11
 * Match exact number of contained expressions evaluating to true
12
 */
13
class Exactly extends Counter
14
{
15
    /**
16
     * @var int Target count
17
     */
18
    private $count;
19
20
    /**
21
     * Load expressions
22
     *
23
     * @param int $count
24
     * @param ExpressionInterface ...$exprs Any number of expressions
25
     */
26
    public function __construct(int $count, ExpressionInterface ...$exprs)
27
    {
28
        $this->count = $count;
29
        parent::__construct(...$exprs);
30
    }
31
32
    /**
33
     * Evaluate that the exact number of contained expressions returns true
34
     */
35
    public function evaluate($operand): bool
36
    {
37
        $count = 0;
38
39
        foreach ($this->exprs as $expr) {
40
            if ($expr->evaluate($operand)) {
41
                $count++;
42
            }
43
        }
44
45
        return $count == $this->count;
46
    }
47
}
48