22
22
*/
23
23
abstract class Constraint implements ConstraintInterface
24
24
{
25
- protected $ schemaStorage ;
26
25
protected $ checkMode = self ::CHECK_MODE_NORMAL ;
27
- protected $ uriRetriever ;
28
26
protected $ errors = array ();
29
27
protected $ inlineSchemaProperty = '$schema ' ;
30
28
31
29
const CHECK_MODE_NORMAL = 1 ;
32
30
const CHECK_MODE_TYPE_CAST = 2 ;
33
31
34
32
/**
35
- * @var null| Factory
33
+ * @var Factory
36
34
*/
37
35
private $ factory ;
38
36
39
37
/**
40
- * @param int $checkMode
41
- * @param SchemaStorage $schemaStorage
42
- * @param UriRetrieverInterface $uriRetriever
43
38
* @param Factory $factory
44
39
*/
45
- public function __construct (
46
- $ checkMode = self ::CHECK_MODE_NORMAL ,
47
- SchemaStorage $ schemaStorage = null ,
48
- UriRetrieverInterface $ uriRetriever = null ,
49
- Factory $ factory = null
50
- ) {
51
- $ this ->checkMode = $ checkMode ;
52
- $ this ->uriRetriever = $ uriRetriever ;
53
- $ this ->factory = $ factory ;
54
- $ this ->schemaStorage = $ schemaStorage ;
40
+ public function __construct (Factory $ factory = null )
41
+ {
42
+ $ this ->factory = $ factory ? : $ this ->getFactory ();
43
+ $ this ->checkMode = $ this ->factory ->getCheckMode ();
55
44
}
56
45
57
46
/**
58
47
* @return UriRetrieverInterface $uriRetriever
59
48
*/
60
49
public function getUriRetriever ()
61
50
{
62
- if (is_null ($ this ->uriRetriever )) {
63
- $ this ->setUriRetriever (new UriRetriever );
64
- }
51
+ return $ this ->factory ->getUriRetriever ();
52
+ }
65
53
66
- return $ this ->uriRetriever ;
54
+ public function setUriRetriever (UriRetrieverInterface $ retriever )
55
+ {
56
+ $ this ->factory ->setUriRetriever ($ retriever );
67
57
}
68
58
69
59
/**
@@ -72,7 +62,7 @@ public function getUriRetriever()
72
62
public function getFactory ()
73
63
{
74
64
if (!$ this ->factory ) {
75
- $ this ->factory = new Factory ($ this -> getSchemaStorage (), $ this -> getUriRetriever (), $ this -> checkMode );
65
+ $ this ->factory = new Factory ();
76
66
}
77
67
78
68
return $ this ->factory ;
@@ -83,19 +73,7 @@ public function getFactory()
83
73
*/
84
74
public function getSchemaStorage ()
85
75
{
86
- if (is_null ($ this ->schemaStorage )) {
87
- $ this ->schemaStorage = new SchemaStorage ($ this ->getUriRetriever ());
88
- }
89
-
90
- return $ this ->schemaStorage ;
91
- }
92
-
93
- /**
94
- * @param UriRetrieverInterface $uriRetriever
95
- */
96
- public function setUriRetriever (UriRetrieverInterface $ uriRetriever )
97
- {
98
- $ this ->uriRetriever = $ uriRetriever ;
76
+ return $ this ->getFactory ()->getSchemaStorage ();
99
77
}
100
78
101
79
/**
@@ -123,7 +101,9 @@ public function addError(JsonPointer $path = null, $message, $constraint='', arr
123
101
*/
124
102
public function addErrors (array $ errors )
125
103
{
126
- $ this ->errors = array_merge ($ this ->errors , $ errors );
104
+ if ($ errors ) {
105
+ $ this ->errors = array_merge ($ this ->errors , $ errors );
106
+ }
127
107
}
128
108
129
109
/**
@@ -181,7 +161,7 @@ protected function incrementPath(JsonPointer $path = null, $i)
181
161
*/
182
162
protected function checkArray ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
183
163
{
184
- $ validator = $ this ->getFactory () ->createInstanceFor ('collection ' );
164
+ $ validator = $ this ->factory ->createInstanceFor ('collection ' );
185
165
$ validator ->check ($ value , $ schema , $ path , $ i );
186
166
187
167
$ this ->addErrors ($ validator ->getErrors ());
@@ -198,7 +178,7 @@ protected function checkArray($value, $schema = null, JsonPointer $path = null,
198
178
*/
199
179
protected function checkObject ($ value , $ schema = null , JsonPointer $ path = null , $ i = null , $ patternProperties = null )
200
180
{
201
- $ validator = $ this ->getFactory () ->createInstanceFor ('object ' );
181
+ $ validator = $ this ->factory ->createInstanceFor ('object ' );
202
182
$ validator ->check ($ value , $ schema , $ path , $ i , $ patternProperties );
203
183
204
184
$ this ->addErrors ($ validator ->getErrors ());
@@ -214,7 +194,7 @@ protected function checkObject($value, $schema = null, JsonPointer $path = null,
214
194
*/
215
195
protected function checkType ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
216
196
{
217
- $ validator = $ this ->getFactory () ->createInstanceFor ('type ' );
197
+ $ validator = $ this ->factory ->createInstanceFor ('type ' );
218
198
$ validator ->check ($ value , $ schema , $ path , $ i );
219
199
220
200
$ this ->addErrors ($ validator ->getErrors ());
@@ -230,8 +210,8 @@ protected function checkType($value, $schema = null, JsonPointer $path = null, $
230
210
*/
231
211
protected function checkUndefined ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
232
212
{
233
- $ validator = $ this ->getFactory () ->createInstanceFor ('undefined ' );
234
- $ validator ->check ($ value , $ this ->schemaStorage ->resolveRefSchema ($ schema ), $ path , $ i );
213
+ $ validator = $ this ->factory ->createInstanceFor ('undefined ' );
214
+ $ validator ->check ($ value , $ this ->getSchemaStorage () ->resolveRefSchema ($ schema ), $ path , $ i );
235
215
236
216
$ this ->addErrors ($ validator ->getErrors ());
237
217
}
@@ -246,7 +226,7 @@ protected function checkUndefined($value, $schema = null, JsonPointer $path = nu
246
226
*/
247
227
protected function checkString ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
248
228
{
249
- $ validator = $ this ->getFactory () ->createInstanceFor ('string ' );
229
+ $ validator = $ this ->factory ->createInstanceFor ('string ' );
250
230
$ validator ->check ($ value , $ schema , $ path , $ i );
251
231
252
232
$ this ->addErrors ($ validator ->getErrors ());
@@ -262,7 +242,7 @@ protected function checkString($value, $schema = null, JsonPointer $path = null,
262
242
*/
263
243
protected function checkNumber ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
264
244
{
265
- $ validator = $ this ->getFactory () ->createInstanceFor ('number ' );
245
+ $ validator = $ this ->factory ->createInstanceFor ('number ' );
266
246
$ validator ->check ($ value , $ schema , $ path , $ i );
267
247
268
248
$ this ->addErrors ($ validator ->getErrors ());
@@ -278,7 +258,7 @@ protected function checkNumber($value, $schema = null, JsonPointer $path = null,
278
258
*/
279
259
protected function checkEnum ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
280
260
{
281
- $ validator = $ this ->getFactory () ->createInstanceFor ('enum ' );
261
+ $ validator = $ this ->factory ->createInstanceFor ('enum ' );
282
262
$ validator ->check ($ value , $ schema , $ path , $ i );
283
263
284
264
$ this ->addErrors ($ validator ->getErrors ());
@@ -294,7 +274,7 @@ protected function checkEnum($value, $schema = null, JsonPointer $path = null, $
294
274
*/
295
275
protected function checkFormat ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
296
276
{
297
- $ validator = $ this ->getFactory () ->createInstanceFor ('format ' );
277
+ $ validator = $ this ->factory ->createInstanceFor ('format ' );
298
278
$ validator ->check ($ value , $ schema , $ path , $ i );
299
279
300
280
$ this ->addErrors ($ validator ->getErrors ());
@@ -307,7 +287,7 @@ protected function checkFormat($value, $schema = null, JsonPointer $path = null,
307
287
*/
308
288
protected function getTypeCheck ()
309
289
{
310
- return $ this ->getFactory () ->getTypeCheck ();
290
+ return $ this ->factory ->getTypeCheck ();
311
291
}
312
292
313
293
/**
0 commit comments