2
2
3
3
namespace Yaoi \Schema ;
4
4
5
-
6
5
use Yaoi \Schema \Base ;
7
6
use Yaoi \Schema \Constraint \Properties ;
8
7
use Yaoi \Schema \Constraint \Ref ;
@@ -21,6 +20,11 @@ class SchemaLoader extends Base
21
20
const ADDITIONAL_ITEMS = 'additionalItems ' ;
22
21
const UNIQUE_ITEMS = 'uniqueItems ' ;
23
22
23
+ const MINIMUM = 'minimum ' ;
24
+ const EXCLUSIVE_MINIMUM = 'exclusiveMinimum ' ;
25
+ const MAXIMUM = 'maximum ' ;
26
+ const EXCLUSIVE_MAXIMUM = 'exclusiveMaximum ' ;
27
+
24
28
/** @var Schema */
25
29
private $ rootSchema ;
26
30
@@ -36,32 +40,32 @@ public function readSchema($schemaData)
36
40
}
37
41
38
42
39
- protected function readSchemaDeeper ($ schemaData , Schema $ parentSchema = null )
43
+ protected function readSchemaDeeper ($ schemaArray , Schema $ parentSchema = null )
40
44
{
41
45
$ schema = new Schema ();
42
46
if (null === $ this ->rootSchema ) {
43
47
$ this ->rootSchema = $ schema ;
44
- $ this ->rootData = $ schemaData ;
48
+ $ this ->rootData = $ schemaArray ;
45
49
}
46
50
47
- if ($ schemaData instanceof \stdClass) {
48
- $ schemaData = (array )$ schemaData ;
51
+ if ($ schemaArray instanceof \stdClass) {
52
+ $ schemaArray = (array )$ schemaArray ;
49
53
}
50
54
51
- if (isset ($ schemaData [self ::TYPE ])) {
52
- $ schema ->type = new Type ($ schemaData [self ::TYPE ]);
55
+ if (isset ($ schemaArray [self ::TYPE ])) {
56
+ $ schema ->type = new Type ($ schemaArray [self ::TYPE ]);
53
57
}
54
58
55
- if (isset ($ schemaData [self ::PROPERTIES ])) {
59
+ if (isset ($ schemaArray [self ::PROPERTIES ])) {
56
60
$ properties = new Properties ();
57
61
$ schema ->properties = $ properties ;
58
- foreach ($ schemaData [self ::PROPERTIES ] as $ name => $ data ) {
62
+ foreach ($ schemaArray [self ::PROPERTIES ] as $ name => $ data ) {
59
63
$ properties ->__set ($ name , $ this ->readSchemaDeeper ($ data , $ schema ));
60
64
}
61
65
}
62
66
63
- if (isset ($ schemaData [self ::ADDITIONAL_PROPERTIES ])) {
64
- $ additionalProperties = $ schemaData [self ::ADDITIONAL_PROPERTIES ];
67
+ if (isset ($ schemaArray [self ::ADDITIONAL_PROPERTIES ])) {
68
+ $ additionalProperties = $ schemaArray [self ::ADDITIONAL_PROPERTIES ];
65
69
if ($ additionalProperties instanceof \stdClass) {
66
70
$ schema ->additionalProperties = $ this ->readSchemaDeeper ($ additionalProperties , $ schema );
67
71
} else {
@@ -70,8 +74,8 @@ protected function readSchemaDeeper($schemaData, Schema $parentSchema = null)
70
74
}
71
75
72
76
73
- if (isset ($ schemaData [self ::ITEMS ])) {
74
- $ items = $ schemaData [self ::ITEMS ];
77
+ if (isset ($ schemaArray [self ::ITEMS ])) {
78
+ $ items = $ schemaArray [self ::ITEMS ];
75
79
if (is_array ($ items )) {
76
80
$ schema ->items = array ();
77
81
foreach ($ items as $ item ) {
@@ -83,22 +87,37 @@ protected function readSchemaDeeper($schemaData, Schema $parentSchema = null)
83
87
}
84
88
85
89
86
- if (isset ($ schemaData [self ::ADDITIONAL_ITEMS ])) {
87
- $ additionalItems = $ schemaData [self ::ADDITIONAL_ITEMS ];
90
+ if (isset ($ schemaArray [self ::ADDITIONAL_ITEMS ])) {
91
+ $ additionalItems = $ schemaArray [self ::ADDITIONAL_ITEMS ];
88
92
if ($ additionalItems instanceof \stdClass) {
89
93
$ schema ->additionalItems = $ this ->readSchemaDeeper ($ additionalItems , $ schema );
90
94
} else {
91
95
$ schema ->additionalItems = $ additionalItems ;
92
96
}
93
97
}
94
98
95
- if (isset ($ schemaData [self ::UNIQUE_ITEMS ]) && $ schemaData [self ::UNIQUE_ITEMS ] === true ) {
99
+ if (isset ($ schemaArray [self ::UNIQUE_ITEMS ]) && $ schemaArray [self ::UNIQUE_ITEMS ] === true ) {
96
100
$ schema ->uniqueItems = true ;
97
101
}
98
102
103
+
104
+ if (isset ($ schemaArray [self ::MINIMUM ])) {
105
+ $ schema ->minimum = $ schemaArray [self ::MINIMUM ];
106
+ }
107
+ if (isset ($ schemaArray [self ::EXCLUSIVE_MINIMUM ])) {
108
+ $ schema ->exclusiveMinimum = $ schemaArray [self ::EXCLUSIVE_MINIMUM ];
109
+ }
110
+ if (isset ($ schemaArray [self ::MAXIMUM ])) {
111
+ $ schema ->maximum = $ schemaArray [self ::MAXIMUM ];
112
+ }
113
+ if (isset ($ schemaArray [self ::EXCLUSIVE_MAXIMUM ])) {
114
+ $ schema ->exclusiveMaximum = $ schemaArray [self ::EXCLUSIVE_MAXIMUM ];
115
+ }
116
+
117
+
99
118
// should resolve references on load
100
- if (isset ($ schemaData [self ::REF ])) {
101
- $ schema ->ref = $ this ->resolveReference ($ schemaData [self ::REF ]);
119
+ if (isset ($ schemaArray [self ::REF ])) {
120
+ $ schema ->ref = $ this ->resolveReference ($ schemaArray [self ::REF ]);
102
121
}
103
122
104
123
return $ schema ;
@@ -114,7 +133,14 @@ private function resolveReference($referencePath)
114
133
{
115
134
$ ref = &$ this ->refs [$ referencePath ];
116
135
if (null === $ ref ) {
117
- if ($ referencePath === '# ' ) {
136
+ if ($ referencePath === 'http://json-schema.org/draft-04/schema# ' ) {
137
+ $ ref = new Ref (
138
+ $ referencePath ,
139
+ SchemaLoader::create ()->readSchema (json_decode (file_get_contents (__DIR__ . '/../spec/json-schema.json ' )))
140
+ );
141
+ }
142
+
143
+ elseif ($ referencePath === '# ' ) {
118
144
$ ref = new Ref ($ referencePath , $ this ->rootSchema );
119
145
}
120
146
@@ -123,8 +149,8 @@ private function resolveReference($referencePath)
123
149
$ branch = &$ this ->rootData ;
124
150
while ($ path ) {
125
151
$ folder = array_shift ($ path );
126
- if (isset ($ branch[ $ folder] )) {
127
- $ branch = &$ branch[ $ folder] ;
152
+ if (isset ($ branch-> $ folder )) {
153
+ $ branch = &$ branch-> $ folder ;
128
154
} else {
129
155
throw new \Exception ('Could not resolve ' . $ referencePath . ', ' . $ folder );
130
156
}
@@ -144,4 +170,9 @@ public function writeSchema()
144
170
145
171
}
146
172
147
- }
173
+ }
174
+
175
+ /**
176
+ * @property $minimum
177
+ */
178
+ class __stubJsonSchema {}
0 commit comments