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