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

ContainsOnlyAllowedMembersConstraint   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 42
ccs 13
cts 14
cp 0.9286
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

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