TypeConstraint::normalize()   C
last analyzed

Complexity

Conditions 8
Paths 7

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 32
c 0
b 0
f 0
ccs 20
cts 20
cp 1
rs 5.3846
cc 8
eloc 18
nc 7
nop 3
crap 8
1
<?php
2
3
/*
4
 * This file is part of the JVal package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace JVal\Constraint;
11
12
use JVal\Constraint;
13
use JVal\Context;
14
use JVal\Exception\Constraint\InvalidTypeException;
15
use JVal\Exception\Constraint\NotPrimitiveTypeException;
16
use JVal\Exception\Constraint\NotUniqueException;
17
use JVal\Types;
18
use JVal\Walker;
19
use stdClass;
20
21
/**
22
 * Constraint for the "type" keyword.
23
 */
24
class TypeConstraint implements Constraint
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29 373
    public function keywords()
30
    {
31 373
        return ['type'];
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 357
    public function supports($type)
38
    {
39 357
        return true;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 212
    public function normalize(stdClass $schema, Context $context, Walker $walker)
46
    {
47 212
        $context->enterNode('type');
48
49 212
        if (is_string($schema->type)) {
50 193
            if (!Types::isPrimitive($schema->type)) {
51 1
                throw new NotPrimitiveTypeException($context);
52
            }
53 211
        } elseif (is_array($schema->type)) {
54 18
            foreach ($schema->type as $index => $type) {
55 18
                $context->enterNode($index);
56
57 18
                if (!is_string($type)) {
58 1
                    throw new InvalidTypeException($context, Types::TYPE_STRING);
59
                }
60
61 18
                if (!Types::isPrimitive($type)) {
62 1
                    throw new NotPrimitiveTypeException($context);
63
                }
64
65 18
                $context->leaveNode();
66 18
            }
67
68 16
            if (count(array_unique($schema->type)) !== count($schema->type)) {
69 1
                throw new NotUniqueException($context);
70
            }
71 15
        } else {
72 1
            throw new InvalidTypeException($context, [Types::TYPE_STRING, Types::TYPE_ARRAY]);
73
        }
74
75 207
        $context->leaveNode();
76 207
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 191
    public function apply($instance, stdClass $schema, Context $context, Walker $walker)
82
    {
83 191
        if (is_string($schema->type)) {
84 176
            if (!Types::isA($instance, $schema->type)) {
85 94
                $context->addViolation('instance must be of type %s', [$schema->type]);
86 94
            }
87 176
        } else {
88 15
            if (!Types::isOneOf($instance, $schema->type)) {
89 7
                $context->addViolation('instance does not match any of the expected types');
90 7
            }
91
        }
92 191
    }
93
}
94