ContainsOnlyAllowedMembers   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

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