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

ContainsOnlyAllowedMembersConstraint::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
use PHPUnit\Framework\ExpectationFailedException;
6
7
class ContainsOnlyAllowedMembersConstraint extends Constraint
8
{
9
    /**
10
     * @var array
11
     */
12
    private $members;
13
14 99
    public function __construct(array $members)
15
    {
16 99
        $this->members = $members;
17 99
    }
18
19
    /**
20
     * Returns a string representation of the constraint.
21
     */
22 25
    public function toString(): string
23
    {
24 25
        return \sprintf(
25 25
            'contains only elements of "%s"',
26 25
            \implode(', ', $this->members)
27
        );
28
    }
29
30
    /**
31
     * Evaluates the constraint for parameter $other. Returns true if the
32
     * constraint is met, false otherwise.
33
     *
34
     * @param mixed $other value or object to evaluate
35
     */
36 99
    protected function matches($other): bool
37
    {
38 99
        if (!is_array($other)) {
39 1
            return false;
40
        }
41
42 98
        foreach (array_keys($other) as $key) {
43 98
            if (!in_array($key, $this->members)) {
44 26
                return false;
45
            }
46
        }
47
48 77
        return true;
49
    }
50
}
51