Passed
Push — master ( 1ec89a...1818e9 )
by Vincent
06:54 queued 04:35
created

ContainsOnlyAllowedMembers   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 45
ccs 12
cts 12
cp 1
rs 10
c 1
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A handle() 0 13 4
A default() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VGirol\JsonApiStructure\Constraint;
6
7
use VGirol\JsonApiStructure\Messages;
8
9
class ContainsOnlyAllowedMembers extends Constraint
10
{
11
    /**
12
     * @var array
13
     */
14
    private $members;
15
16
    /**
17
     * Class constructor.
18
     *
19
     * @param array $members
20
     */
21 381
    public function __construct(array $members)
22
    {
23 381
        $this->members = $members;
24 381
    }
25
26
    /**
27
     * Returns a string representation of the constraint.
28
     */
29 66
    public function default(): string
30
    {
31 66
        return Messages::ONLY_ALLOWED_MEMBERS;
32
    }
33
34
    /**
35
     * Evaluates the constraint for parameter $json. Returns true if the constraint is met, false otherwise.
36
     *
37
     * @param mixed $json Value or object to evaluate
38
     *
39
     * @return boolean
40
     */
41 381
    protected function handle($json): bool
42
    {
43 381
        if (!\is_array($json)) {
44 3
            return false;
45
        }
46
47 378
        foreach (\array_keys($json) as $key) {
48 378
            if (!\in_array($key, $this->members)) {
49 69
                return false;
50
            }
51
        }
52
53 324
        return true;
54
    }
55
}
56