Enum::validate()   B
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 10
nc 4
nop 3
dl 0
loc 18
ccs 9
cts 9
cp 1
crap 6
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace League\JsonGuard\Constraint\DraftFour;
4
5
use League\JsonGuard\Assert;
6
use League\JsonGuard\ConstraintInterface;
7
use League\JsonGuard\Validator;
8
use function League\JsonGuard\error;
9
10
final class Enum implements ConstraintInterface
11
{
12
    const KEYWORD = 'enum';
13
14
    /**
15
     * {@inheritdoc}
16
     */
17 8
    public function validate($value, $parameter, Validator $validator)
18
    {
19 8
        Assert::type($parameter, 'array', self::KEYWORD, $validator->getSchemaPath());
20
21 4
        if (is_object($value)) {
22 2
            foreach ($parameter as $i) {
23 2
                if (is_object($i) && $value == $i) {
24 2
                    return null;
25
                }
26
            }
27
        } else {
28 4
            if (in_array($value, $parameter, true)) {
29 4
                return null;
30
            }
31
        }
32
33 4
        return error('The value must be one of: {parameter}', $validator);
34
    }
35
}
36