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

ContainsAtLeastOneConstraint   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 47
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 3 1
A toString() 0 5 1
A __construct() 0 3 1
A matches() 0 13 4
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