2222 */
2323abstract class Constraint implements ConstraintInterface
2424{
25- protected $ schemaStorage ;
26- protected $ checkMode = self ::CHECK_MODE_NORMAL ;
27- protected $ uriRetriever ;
2825 protected $ errors = array ();
2926 protected $ inlineSchemaProperty = '$schema ' ;
3027
@@ -33,70 +30,16 @@ abstract class Constraint implements ConstraintInterface
3330 const CHECK_MODE_COERCE = 0x00000004 ;
3431
3532 /**
36- * @var null| Factory
33+ * @var Factory
3734 */
38- private $ factory ;
35+ protected $ factory ;
3936
4037 /**
41- * @param int $checkMode
42- * @param SchemaStorage $schemaStorage
43- * @param UriRetrieverInterface $uriRetriever
4438 * @param Factory $factory
4539 */
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 )
7441 {
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 ();
10043 }
10144
10245 /**
@@ -124,7 +67,9 @@ public function addError(JsonPointer $path = null, $message, $constraint='', arr
12467 */
12568 public function addErrors (array $ errors )
12669 {
127- $ this ->errors = array_merge ($ this ->errors , $ errors );
70+ if ($ errors ) {
71+ $ this ->errors = array_merge ($ this ->errors , $ errors );
72+ }
12873 }
12974
13075 /**
@@ -182,7 +127,7 @@ protected function incrementPath(JsonPointer $path = null, $i)
182127 */
183128 protected function checkArray ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
184129 {
185- $ validator = $ this ->getFactory () ->createInstanceFor ('collection ' );
130+ $ validator = $ this ->factory ->createInstanceFor ('collection ' );
186131 $ validator ->check ($ value , $ schema , $ path , $ i );
187132
188133 $ this ->addErrors ($ validator ->getErrors ());
@@ -199,7 +144,7 @@ protected function checkArray($value, $schema = null, JsonPointer $path = null,
199144 */
200145 protected function checkObject ($ value , $ schema = null , JsonPointer $ path = null , $ i = null , $ patternProperties = null )
201146 {
202- $ validator = $ this ->getFactory () ->createInstanceFor ('object ' );
147+ $ validator = $ this ->factory ->createInstanceFor ('object ' );
203148 $ validator ->check ($ value , $ schema , $ path , $ i , $ patternProperties );
204149
205150 $ this ->addErrors ($ validator ->getErrors ());
@@ -215,7 +160,7 @@ protected function checkObject($value, $schema = null, JsonPointer $path = null,
215160 */
216161 protected function checkType ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
217162 {
218- $ validator = $ this ->getFactory () ->createInstanceFor ('type ' );
163+ $ validator = $ this ->factory ->createInstanceFor ('type ' );
219164 $ validator ->check ($ value , $ schema , $ path , $ i );
220165
221166 $ this ->addErrors ($ validator ->getErrors ());
@@ -231,8 +176,9 @@ protected function checkType($value, $schema = null, JsonPointer $path = null, $
231176 */
232177 protected function checkUndefined ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
233178 {
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 );
236182
237183 $ this ->addErrors ($ validator ->getErrors ());
238184 }
@@ -247,7 +193,7 @@ protected function checkUndefined($value, $schema = null, JsonPointer $path = nu
247193 */
248194 protected function checkString ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
249195 {
250- $ validator = $ this ->getFactory () ->createInstanceFor ('string ' );
196+ $ validator = $ this ->factory ->createInstanceFor ('string ' );
251197 $ validator ->check ($ value , $ schema , $ path , $ i );
252198
253199 $ this ->addErrors ($ validator ->getErrors ());
@@ -263,7 +209,7 @@ protected function checkString($value, $schema = null, JsonPointer $path = null,
263209 */
264210 protected function checkNumber ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
265211 {
266- $ validator = $ this ->getFactory () ->createInstanceFor ('number ' );
212+ $ validator = $ this ->factory ->createInstanceFor ('number ' );
267213 $ validator ->check ($ value , $ schema , $ path , $ i );
268214
269215 $ this ->addErrors ($ validator ->getErrors ());
@@ -279,7 +225,7 @@ protected function checkNumber($value, $schema = null, JsonPointer $path = null,
279225 */
280226 protected function checkEnum ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
281227 {
282- $ validator = $ this ->getFactory () ->createInstanceFor ('enum ' );
228+ $ validator = $ this ->factory ->createInstanceFor ('enum ' );
283229 $ validator ->check ($ value , $ schema , $ path , $ i );
284230
285231 $ this ->addErrors ($ validator ->getErrors ());
@@ -295,7 +241,7 @@ protected function checkEnum($value, $schema = null, JsonPointer $path = null, $
295241 */
296242 protected function checkFormat ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
297243 {
298- $ validator = $ this ->getFactory () ->createInstanceFor ('format ' );
244+ $ validator = $ this ->factory ->createInstanceFor ('format ' );
299245 $ validator ->check ($ value , $ schema , $ path , $ i );
300246
301247 $ this ->addErrors ($ validator ->getErrors ());
@@ -308,7 +254,7 @@ protected function checkFormat($value, $schema = null, JsonPointer $path = null,
308254 */
309255 protected function getTypeCheck ()
310256 {
311- return $ this ->getFactory () ->getTypeCheck ();
257+ return $ this ->factory ->getTypeCheck ();
312258 }
313259
314260 /**
0 commit comments