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
@@ -33,70 +30,16 @@ abstract class Constraint implements ConstraintInterface
33
30
const CHECK_MODE_COERCE = 0x00000004 ;
34
31
35
32
/**
36
- * @var null| Factory
33
+ * @var Factory
37
34
*/
38
- private $ factory ;
35
+ protected $ factory ;
39
36
40
37
/**
41
- * @param int $checkMode
42
- * @param SchemaStorage $schemaStorage
43
- * @param UriRetrieverInterface $uriRetriever
44
38
* @param Factory $factory
45
39
*/
46
- public function __construct (
47
- $ checkMode = self ::CHECK_MODE_NORMAL ,
48
- SchemaStorage $ schemaStorage = null ,
49
- UriRetrieverInterface $ uriRetriever = null ,
50
- Factory $ factory = null
51
- ) {
52
- $ this ->checkMode = $ checkMode ;
53
- $ this ->uriRetriever = $ uriRetriever ;
54
- $ this ->factory = $ factory ;
55
- $ this ->schemaStorage = $ schemaStorage ;
56
- }
57
-
58
- /**
59
- * @return UriRetrieverInterface $uriRetriever
60
- */
61
- public function getUriRetriever ()
62
- {
63
- if (is_null ($ this ->uriRetriever )) {
64
- $ this ->setUriRetriever (new UriRetriever );
65
- }
66
-
67
- return $ this ->uriRetriever ;
68
- }
69
-
70
- /**
71
- * @return Factory
72
- */
73
- public function getFactory ()
40
+ public function __construct (Factory $ factory = null )
74
41
{
75
- if (!$ this ->factory ) {
76
- $ this ->factory = new Factory ($ this ->getSchemaStorage (), $ this ->getUriRetriever (), $ this ->checkMode );
77
- }
78
-
79
- return $ this ->factory ;
80
- }
81
-
82
- /**
83
- * @return SchemaStorage
84
- */
85
- public function getSchemaStorage ()
86
- {
87
- if (is_null ($ this ->schemaStorage )) {
88
- $ this ->schemaStorage = new SchemaStorage ($ this ->getUriRetriever ());
89
- }
90
-
91
- return $ this ->schemaStorage ;
92
- }
93
-
94
- /**
95
- * @param UriRetrieverInterface $uriRetriever
96
- */
97
- public function setUriRetriever (UriRetrieverInterface $ uriRetriever )
98
- {
99
- $ this ->uriRetriever = $ uriRetriever ;
42
+ $ this ->factory = $ factory ? : new Factory ();
100
43
}
101
44
102
45
/**
@@ -124,7 +67,9 @@ public function addError(JsonPointer $path = null, $message, $constraint='', arr
124
67
*/
125
68
public function addErrors (array $ errors )
126
69
{
127
- $ this ->errors = array_merge ($ this ->errors , $ errors );
70
+ if ($ errors ) {
71
+ $ this ->errors = array_merge ($ this ->errors , $ errors );
72
+ }
128
73
}
129
74
130
75
/**
@@ -182,7 +127,7 @@ protected function incrementPath(JsonPointer $path = null, $i)
182
127
*/
183
128
protected function checkArray ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
184
129
{
185
- $ validator = $ this ->getFactory () ->createInstanceFor ('collection ' );
130
+ $ validator = $ this ->factory ->createInstanceFor ('collection ' );
186
131
$ validator ->check ($ value , $ schema , $ path , $ i );
187
132
188
133
$ this ->addErrors ($ validator ->getErrors ());
@@ -199,7 +144,7 @@ protected function checkArray($value, $schema = null, JsonPointer $path = null,
199
144
*/
200
145
protected function checkObject ($ value , $ schema = null , JsonPointer $ path = null , $ i = null , $ patternProperties = null )
201
146
{
202
- $ validator = $ this ->getFactory () ->createInstanceFor ('object ' );
147
+ $ validator = $ this ->factory ->createInstanceFor ('object ' );
203
148
$ validator ->check ($ value , $ schema , $ path , $ i , $ patternProperties );
204
149
205
150
$ this ->addErrors ($ validator ->getErrors ());
@@ -215,7 +160,7 @@ protected function checkObject($value, $schema = null, JsonPointer $path = null,
215
160
*/
216
161
protected function checkType ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
217
162
{
218
- $ validator = $ this ->getFactory () ->createInstanceFor ('type ' );
163
+ $ validator = $ this ->factory ->createInstanceFor ('type ' );
219
164
$ validator ->check ($ value , $ schema , $ path , $ i );
220
165
221
166
$ this ->addErrors ($ validator ->getErrors ());
@@ -231,8 +176,9 @@ protected function checkType($value, $schema = null, JsonPointer $path = null, $
231
176
*/
232
177
protected function checkUndefined ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
233
178
{
234
- $ validator = $ this ->getFactory ()->createInstanceFor ('undefined ' );
235
- $ validator ->check ($ value , $ this ->schemaStorage ->resolveRefSchema ($ schema ), $ path , $ i );
179
+ $ validator = $ this ->factory ->createInstanceFor ('undefined ' );
180
+
181
+ $ validator ->check ($ value , $ this ->factory ->getSchemaStorage ()->resolveRefSchema ($ schema ), $ path , $ i );
236
182
237
183
$ this ->addErrors ($ validator ->getErrors ());
238
184
}
@@ -247,7 +193,7 @@ protected function checkUndefined($value, $schema = null, JsonPointer $path = nu
247
193
*/
248
194
protected function checkString ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
249
195
{
250
- $ validator = $ this ->getFactory () ->createInstanceFor ('string ' );
196
+ $ validator = $ this ->factory ->createInstanceFor ('string ' );
251
197
$ validator ->check ($ value , $ schema , $ path , $ i );
252
198
253
199
$ this ->addErrors ($ validator ->getErrors ());
@@ -263,7 +209,7 @@ protected function checkString($value, $schema = null, JsonPointer $path = null,
263
209
*/
264
210
protected function checkNumber ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
265
211
{
266
- $ validator = $ this ->getFactory () ->createInstanceFor ('number ' );
212
+ $ validator = $ this ->factory ->createInstanceFor ('number ' );
267
213
$ validator ->check ($ value , $ schema , $ path , $ i );
268
214
269
215
$ this ->addErrors ($ validator ->getErrors ());
@@ -279,7 +225,7 @@ protected function checkNumber($value, $schema = null, JsonPointer $path = null,
279
225
*/
280
226
protected function checkEnum ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
281
227
{
282
- $ validator = $ this ->getFactory () ->createInstanceFor ('enum ' );
228
+ $ validator = $ this ->factory ->createInstanceFor ('enum ' );
283
229
$ validator ->check ($ value , $ schema , $ path , $ i );
284
230
285
231
$ this ->addErrors ($ validator ->getErrors ());
@@ -295,7 +241,7 @@ protected function checkEnum($value, $schema = null, JsonPointer $path = null, $
295
241
*/
296
242
protected function checkFormat ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
297
243
{
298
- $ validator = $ this ->getFactory () ->createInstanceFor ('format ' );
244
+ $ validator = $ this ->factory ->createInstanceFor ('format ' );
299
245
$ validator ->check ($ value , $ schema , $ path , $ i );
300
246
301
247
$ this ->addErrors ($ validator ->getErrors ());
@@ -308,7 +254,7 @@ protected function checkFormat($value, $schema = null, JsonPointer $path = null,
308
254
*/
309
255
protected function getTypeCheck ()
310
256
{
311
- return $ this ->getFactory () ->getTypeCheck ();
257
+ return $ this ->factory ->getTypeCheck ();
312
258
}
313
259
314
260
/**
0 commit comments