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

ListExactly::evaluate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 4
nop 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace hanneskod\yaysondb\Expression\Set;
6
7
use hanneskod\yaysondb\Expression\ExpressionInterface;
8
use hanneskod\yaysondb\Expression\Set;
9
10
/**
11
 * Expression must evaluate to true for exact numer of items in list
12
 */
13
class ListExactly extends Set
14
{
15
    /**
16
     * @var int Target count
17
     */
18
    private $count;
19
20
    /**
21
     * Set target count
22
     *
23
     * @param int $count
24
     * @param ExpressionInterface $expr
25
     */
26
    public function __construct(int $count, ExpressionInterface $expr)
27
    {
28
        $this->count = $count;
29
        parent::__construct($expr);
30
    }
31
32
    /**
33
     * Expression must evaluate to true for exact numer of items in list
34
     */
35
    public function evaluate($list): bool
36
    {
37
        if (!is_array($list)) {
38
            return false;
39
        }
40
41
        $count = 0;
42
43
        foreach ($list as $item) {
44
            if ($this->expr->evaluate($item)) {
45
                $count++;
46
            }
47
        }
48
49
        return $count == $this->count;
50
    }
51
}
52