Completed
Pull Request — master (#101)
by Marcio
02:15
created

Enum   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 94.12%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 32
ccs 16
cts 17
cp 0.9412
rs 10
wmc 6
lcom 0
cbo 3

1 Method

Rating   Name   Duplication   Size   Complexity  
B validate() 0 24 6
1
<?php
2
3
namespace League\JsonGuard\Constraints;
4
5
use League\JsonGuard\Assert;
6
use League\JsonGuard\ValidationError;
7
use League\JsonGuard\Validator;
8
9
class Enum implements Constraint
10
{
11
    const KEYWORD = 'enum';
12
13
    /**
14
     * {@inheritdoc}
15
     */
16 12
    public function validate($value, $parameter, Validator $validator)
17
    {
18 12
        Assert::type($parameter, 'array', self::KEYWORD, $validator->getPointer());
19
20 8
        if (is_object($value)) {
21 4
            foreach ($parameter as $i) {
22 4
                if (is_object($i) && $value == $i) {
23
                    return null;
24
                }
25 4
            }
26 4
        } else {
27 8
            if (in_array($value, $parameter, true)) {
28 8
                return null;
29
            }
30
        }
31
32 8
        return new ValidationError(
33 8
            'Value {value} is not one of: {choices}',
34 8
            self::KEYWORD,
35 8
            $value,
36 8
            $validator->getPointer(),
37 8
            ['choices' => $parameter, 'value' => $value]
38 8
        );
39
    }
40
}
41