Passed
Push — master ( 1e6084...cf392e )
by Vincent
02:40
created

ContainsAtLeastOneConstraint::matches()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 4
nop 1
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace VGirol\JsonApiAssert\Constraint;
3
4
use PHPUnit\Framework\Constraint\Constraint;
5
6
class ContainsAtLeastOneConstraint extends Constraint
7
{
8
    /**
9
     * @var array
10
     */
11
    private $members;
12
13 62
    public function __construct(array $members)
14
    {
15 62
        $this->members = $members;
16 62
    }
17
18
    /**
19
     * Returns a string representation of the constraint.
20
     */
21 7
    public function toString() : string
22
    {
23 7
        return \sprintf(
24 7
            'contains at least one element of "%s"',
25 7
            \implode(', ', $this->members)
26
        );
27
    }
28
29
    /**
30
     * Evaluates the constraint for parameter $other. Returns true if the
31
     * constraint is met, false otherwise.
32
     *
33
     * @param mixed $other value or object to evaluate
34
     */
35 62
    protected function matches($other) : bool
36
    {
37 62
        if (!is_array($other)) {
38 1
            return false;
39
        }
40
41 61
        foreach ($this->members as $member) {
42 61
            if (array_key_exists($member, $other)) {
43 45
                return true;
44
            }
45
        }
46
47 19
        return false;
48
    }
49
50 25
    public function check($other) : bool
51
    {
52 25
        return $this->matches($other);
53
    }
54
}
55