Failed Conditions
Pull Request — master (#16)
by Stéphane
02:43
created

Registry::createBuiltInConstraints()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
crap 1
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 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...
121
    {
122 306
        $cache = &$this->keywordsCache[$version];
123
124 306
        if ($cache === null) {
125 306
            $cache = [];
126
127 306
            foreach ($this->getConstraints($version) as $constraint) {
128 306
                foreach ($constraint->keywords() as $constraintKeyword) {
129 306
                    $cache[$constraintKeyword] = true;
130 306
                }
131 306
            }
132 306
        }
133
134 306
        return isset($cache[$keyword]);
135
    }
136
137
    /**
138
     * Loads the constraints associated with a given JSON Schema version.
139
     *
140
     * @param string $version
141
     *
142
     * @return Constraint[]
143
     *
144
     * @throws UnsupportedVersionException if the version is not supported
145
     */
146 371
    protected function createConstraints($version)
147
    {
148
        switch ($version) {
149 371
            case self::VERSION_CURRENT:
150 371
            case self::VERSION_DRAFT_4:
151 370
                return $this->createBuiltInConstraints(
152 370
                    array_merge(
153 370
                        self::$commonConstraints,
154
                        self::$draft4Constraints
155 370
                    )
156 370
                );
157 1
            default:
158 1
                throw new UnsupportedVersionException(
159 1
                    "Schema version '{$version}' not supported"
160 1
                );
161 1
        }
162
    }
163
164
    private function createBuiltInConstraints(array $constraintNames)
165
    {
166 370
        return array_map(function ($name) {
167 370
            $class = "JVal\\Constraint\\{$name}Constraint";
168
169 370
            return new $class();
170 370
        }, $constraintNames);
171
    }
172
}
173