Completed
Pull Request — master (#116)
by
unknown
07:10
created

Type::validateType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
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\ValidationError;
8
use League\JsonGuard\Validator;
9
use function League\JsonGuard\error;
10
11
final class Type implements ConstraintInterface
12
{
13
    const KEYWORD = 'type';
14
15
    /**
16
     * {@inheritdoc}
17
     */
18 50
    public function validate($value, $type, Validator $validator)
19
    {
20 50
        Assert::type($type, ['array', 'string'], self::KEYWORD, $validator->getSchemaPath());
21
22 48
        if (is_array($type)) {
23 4
            return $this->anyType($value, $type, $validator);
24
        }
25
26
        switch ($type) {
27 48
            case 'object':
28 16
                return $this->validateType($value, 'is_object', $validator);
29 46
            case 'array':
30 8
                return $this->validateType($value, 'is_array', $validator);
31 44
            case 'boolean':
32 8
                return $this->validateType($value, 'is_bool', $validator);
33 42
            case 'null':
34 4
                return $this->validateType($value, 'is_null', $validator);
35 42
            case 'number':
36 10
                return $this->validateType($value, 'League\JsonGuard\is_json_number', $validator);
37 38
            case 'integer':
38 32
                return $this->validateType($value, 'League\JsonGuard\is_json_integer', $validator);
39 28
            case 'string':
40 28
                return $this->validateType($value,'is_string', $validator);
41
        }
42
    }
43
44
    /**
45
     * @param mixed                       $value
46
     * @param callable                    $callable
47
     * @param \League\JsonGuard\Validator $validator
48
     *
49
     * @return \League\JsonGuard\ValidationError|null
50
     *
51
     */
52 48
    private function validateType($value, callable $callable, Validator $validator)
53
    {
54 48
        if (call_user_func($callable, $value) === true) {
55 44
            return null;
56
        }
57
58 36
        return error('The data must be a(n) {parameter}.', $validator);
59
    }
60
61
    /**
62
     * @param mixed $value
63
     * @param array $choices
64
     *
65
     * @param Validator $validator
66
     *
67
     * @return ValidationError|null
68
     */
69 4
    private function anyType($value, array $choices, Validator $validator)
70
    {
71 4
        foreach ($choices as $type) {
72 4
            $error = $this->validate($value, $type, $validator);
73 4
            if (is_null($error)) {
74 4
                return null;
75
            }
76 2
        }
77
78 4
        return error('The data must be one of {parameter}.', $validator);
79
    }
80
}
81