Completed
Push — master ( bfd868...ee3a72 )
by Vincent
12:29 queued 10:26
created

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 ContainsOnlyAllowedMembersConstraint extends Constraint
7
{
8
    /**
9
     * @var array
10
     */
11
    private $members;
12
13 64
    public function __construct(array $members)
14
    {
15 64
        $this->members = $members;
16 64
    }
17
18
    /**
19
     * Returns a string representation of the constraint.
20
     */
21 14
    public function toString(): string
22
    {
23 14
        return \sprintf(
24 14
            'contains only elements of "%s"',
25 14
            \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 64
    protected function matches($other): bool
36
    {
37 64
        if (!is_array($other)) {
38
            return false;
39
        }
40
41 64
        foreach ($other as $key => $value) {
42 64
            if (!in_array($key, $this->members)) {
43 14
                return false;
44
            }
45
        }
46
47 52
        return true;
48
    }
49
}
50