Completed
Push — master ( d0b5dd...691c19 )
by Stéphane
7s
created

Registry::hasKeyword()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 16
Ratio 100 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 16
loc 16
ccs 11
cts 11
cp 1
rs 9.2
cc 4
eloc 8
nc 2
nop 2
crap 4
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;
11
12
use JVal\Exception\UnsupportedVersionException;
13
14
/**
15
 * Stores and exposes validation constraints per version.
16
 */
17
class Registry
18
{
19
    const VERSION_CURRENT = 'http://json-schema.org/schema#';
20
    const VERSION_DRAFT_3 = 'http://json-schema.org/draft-03/schema#';
21
    const VERSION_DRAFT_4 = 'http://json-schema.org/draft-04/schema#';
22
23
    private static $commonConstraints = [
24
        'Maximum',
25
        'Minimum',
26
        'MaxLength',
27
        'MinLength',
28
        'Pattern',
29
        'Items',
30
        'MaxItems',
31
        'MinItems',
32
        'UniqueItems',
33
        'Required',
34
        'Properties',
35
        'Dependencies',
36
        'Enum',
37
        'Type',
38
        'Format',
39
    ];
40
41
    private static $draft4Constraints = [
42
        'MultipleOf',
43
        'MinProperties',
44
        'MaxProperties',
45
        'AllOf',
46
        'AnyOf',
47
        'OneOf',
48
        'Not',
49
    ];
50
51
    /**
52
     * @var Constraint[][]
53
     */
54
    private $constraints = [];
55
56
    /**
57
     * @var Constraint[][]
58
     */
59
    private $constraintsForTypeCache = [];
60
61
    /**
62
     * @var array
63
     */
64
    private $keywordsCache = [];
65
66
    /**
67
     * Returns the constraints associated with a given JSON Schema version.
68
     *
69
     * @param string $version
70
     *
71
     * @return Constraint[]
72
     *
73
     * @throws UnsupportedVersionException if the version is not supported
74
     */
75 371
    public function getConstraints($version)
76
    {
77 371
        if (!isset($this->constraints[$version])) {
78 371
            $this->constraints[$version] = $this->createConstraints($version);
79 370
        }
80
81 370
        return $this->constraints[$version];
82
    }
83
84
    /**
85
     * Returns the constraints associated with a given JSON Schema version
86
     * supporting a given primitive type.
87
     *
88
     * @param string $version
89
     * @param string $type
90
     *
91
     * @return Constraint[]
92
     *
93
     * @throws UnsupportedVersionException if the version is not supported
94
     */
95 354 View Code Duplication
    public function getConstraintsForType($version, $type)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
    {
97 354
        $cache = & $this->constraintsForTypeCache[$version.$type];
98
99 354
        if ($cache === null) {
100 354
            $cache = [];
101
102 354
            foreach ($this->getConstraints($version) as $constraint) {
103 354
                if ($constraint->supports($type)) {
104 354
                    $cache[] = $constraint;
105 354
                }
106 354
            }
107 354
        }
108
109 354
        return $cache;
110
    }
111
112
    /**
113
     * Returns whether a keyword is supported in a given JSON Schema version.
114
     *
115
     * @param string $version
116
     * @param string $keyword
117
     *
118
     * @return bool
119
     *
120
     */
121 306 View Code Duplication
    public function hasKeyword($version, $keyword)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
    {
123 306
        $cache = & $this->keywordsCache[$version];
124
125 306
        if ($cache === null) {
126 306
            $cache = [];
127
128 306
            foreach ($this->getConstraints($version) as $constraint) {
129 306
                foreach ($constraint->keywords() as $constraintKeyword) {
130 306
                    $cache[$constraintKeyword] = true;
131 306
                }
132 306
            }
133 306
        }
134
135 306
        return isset($cache[$keyword]);
136
    }
137
138
    /**
139
     * Loads the constraints associated with a given JSON Schema version.
140
     *
141
     * @param string $version
142
     *
143
     * @return Constraint[]
144
     *
145
     * @throws UnsupportedVersionException if the version is not supported
146
     */
147 371
    protected function createConstraints($version)
148
    {
149
        switch ($version) {
150 371
            case self::VERSION_CURRENT:
151 371
            case self::VERSION_DRAFT_4:
152 370
                return $this->createBuiltInConstraints(
153 370
                    array_merge(
154 370
                        self::$commonConstraints,
155
                        self::$draft4Constraints
156 370
                    )
157 370
                );
158 1
            default:
159 1
                throw new UnsupportedVersionException(
160 1
                    "Schema version '{$version}' not supported"
161 1
                );
162 1
        }
163
    }
164
165
    private function createBuiltInConstraints(array $constraintNames)
166
    {
167 370
        return array_map(function ($name) {
168 370
            $class = "JVal\\Constraint\\{$name}Constraint";
169
170 370
            return new $class();
171 370
        }, $constraintNames);
172
    }
173
}
174