|
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
|
|
|
* Returns the constraints associated with a given JSON Schema version. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $version |
|
60
|
|
|
* |
|
61
|
|
|
* @return Constraint[] |
|
62
|
|
|
* |
|
63
|
|
|
* @throws \Exception if the version is not supported |
|
64
|
|
|
*/ |
|
65
|
358 |
|
public function getConstraints($version) |
|
66
|
|
|
{ |
|
67
|
358 |
|
if (!isset($this->constraints[$version])) { |
|
68
|
358 |
|
$this->constraints[$version] = $this->createConstraints($version); |
|
69
|
357 |
|
} |
|
70
|
|
|
|
|
71
|
357 |
|
return $this->constraints[$version]; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Loads the constraints associated with a given JSON Schema version. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $version |
|
78
|
|
|
* |
|
79
|
|
|
* @return Constraint[] |
|
80
|
|
|
* |
|
81
|
|
|
* @throws \Exception if the version is not supported |
|
82
|
|
|
*/ |
|
83
|
358 |
|
protected function createConstraints($version) |
|
84
|
|
|
{ |
|
85
|
|
|
switch ($version) { |
|
86
|
358 |
|
case self::VERSION_CURRENT: |
|
87
|
358 |
|
case self::VERSION_DRAFT_4: |
|
88
|
357 |
|
return $this->createBuiltInConstraints( |
|
89
|
357 |
|
array_merge( |
|
90
|
357 |
|
self::$commonConstraints, |
|
91
|
|
|
self::$draft4Constraints |
|
92
|
357 |
|
) |
|
93
|
357 |
|
); |
|
94
|
1 |
|
default: |
|
95
|
1 |
|
throw new UnsupportedVersionException( |
|
96
|
1 |
|
"Schema version '{$version}' not supported" |
|
97
|
1 |
|
); |
|
98
|
1 |
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
private function createBuiltInConstraints(array $constraintNames) |
|
102
|
|
|
{ |
|
103
|
357 |
|
return array_map(function ($name) { |
|
104
|
357 |
|
$class = "JVal\\Constraint\\{$name}Constraint"; |
|
105
|
|
|
|
|
106
|
357 |
|
return new $class(); |
|
107
|
357 |
|
}, $constraintNames); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|