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
3128 const CHECK_MODE_NORMAL = 1 ;
3229 const CHECK_MODE_TYPE_CAST = 2 ;
3330
3431 /**
35- * @var null| Factory
32+ * @var Factory
3633 */
3734 private $ factory ;
3835
3936 /**
40- * @param int $checkMode
41- * @param SchemaStorage $schemaStorage
42- * @param UriRetrieverInterface $uriRetriever
4337 * @param Factory $factory
4438 */
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 ();
5542 }
5643
5744 /**
5845 * @return UriRetrieverInterface $uriRetriever
5946 */
6047 public function getUriRetriever ()
6148 {
62- if (is_null ($ this ->uriRetriever )) {
63- $ this ->setUriRetriever (new UriRetriever );
64- }
65-
66- return $ this ->uriRetriever ;
49+ return $ this ->factory ->getUriRetriever ();
6750 }
6851
6952 /**
@@ -72,7 +55,7 @@ public function getUriRetriever()
7255 public function getFactory ()
7356 {
7457 if (!$ this ->factory ) {
75- $ this ->factory = new Factory ($ this -> getSchemaStorage (), $ this -> getUriRetriever (), $ this -> checkMode );
58+ $ this ->factory = new Factory ();
7659 }
7760
7861 return $ this ->factory ;
@@ -83,19 +66,12 @@ public function getFactory()
8366 */
8467 public function getSchemaStorage ()
8568 {
86- if (is_null ($ this ->schemaStorage )) {
87- $ this ->schemaStorage = new SchemaStorage ($ this ->getUriRetriever ());
88- }
89-
90- return $ this ->schemaStorage ;
69+ return $ this ->factory ->getSchemaStorage ();
9170 }
9271
93- /**
94- * @param UriRetrieverInterface $uriRetriever
95- */
96- public function setUriRetriever (UriRetrieverInterface $ uriRetriever )
72+ public function getCheckMode ()
9773 {
98- $ this ->uriRetriever = $ uriRetriever ;
74+ return $ this ->factory -> getCheckMode () ;
9975 }
10076
10177 /**
@@ -123,7 +99,9 @@ public function addError(JsonPointer $path = null, $message, $constraint='', arr
12399 */
124100 public function addErrors (array $ errors )
125101 {
126- $ this ->errors = array_merge ($ this ->errors , $ errors );
102+ if ($ errors ) {
103+ $ this ->errors = array_merge ($ this ->errors , $ errors );
104+ }
127105 }
128106
129107 /**
@@ -181,7 +159,7 @@ protected function incrementPath(JsonPointer $path = null, $i)
181159 */
182160 protected function checkArray ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
183161 {
184- $ validator = $ this ->getFactory () ->createInstanceFor ('collection ' );
162+ $ validator = $ this ->factory ->createInstanceFor ('collection ' );
185163 $ validator ->check ($ value , $ schema , $ path , $ i );
186164
187165 $ this ->addErrors ($ validator ->getErrors ());
@@ -198,7 +176,7 @@ protected function checkArray($value, $schema = null, JsonPointer $path = null,
198176 */
199177 protected function checkObject ($ value , $ schema = null , JsonPointer $ path = null , $ i = null , $ patternProperties = null )
200178 {
201- $ validator = $ this ->getFactory () ->createInstanceFor ('object ' );
179+ $ validator = $ this ->factory ->createInstanceFor ('object ' );
202180 $ validator ->check ($ value , $ schema , $ path , $ i , $ patternProperties );
203181
204182 $ this ->addErrors ($ validator ->getErrors ());
@@ -214,7 +192,7 @@ protected function checkObject($value, $schema = null, JsonPointer $path = null,
214192 */
215193 protected function checkType ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
216194 {
217- $ validator = $ this ->getFactory () ->createInstanceFor ('type ' );
195+ $ validator = $ this ->factory ->createInstanceFor ('type ' );
218196 $ validator ->check ($ value , $ schema , $ path , $ i );
219197
220198 $ this ->addErrors ($ validator ->getErrors ());
@@ -230,8 +208,8 @@ protected function checkType($value, $schema = null, JsonPointer $path = null, $
230208 */
231209 protected function checkUndefined ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
232210 {
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 );
235213
236214 $ this ->addErrors ($ validator ->getErrors ());
237215 }
@@ -246,7 +224,7 @@ protected function checkUndefined($value, $schema = null, JsonPointer $path = nu
246224 */
247225 protected function checkString ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
248226 {
249- $ validator = $ this ->getFactory () ->createInstanceFor ('string ' );
227+ $ validator = $ this ->factory ->createInstanceFor ('string ' );
250228 $ validator ->check ($ value , $ schema , $ path , $ i );
251229
252230 $ this ->addErrors ($ validator ->getErrors ());
@@ -262,7 +240,7 @@ protected function checkString($value, $schema = null, JsonPointer $path = null,
262240 */
263241 protected function checkNumber ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
264242 {
265- $ validator = $ this ->getFactory () ->createInstanceFor ('number ' );
243+ $ validator = $ this ->factory ->createInstanceFor ('number ' );
266244 $ validator ->check ($ value , $ schema , $ path , $ i );
267245
268246 $ this ->addErrors ($ validator ->getErrors ());
@@ -278,7 +256,7 @@ protected function checkNumber($value, $schema = null, JsonPointer $path = null,
278256 */
279257 protected function checkEnum ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
280258 {
281- $ validator = $ this ->getFactory () ->createInstanceFor ('enum ' );
259+ $ validator = $ this ->factory ->createInstanceFor ('enum ' );
282260 $ validator ->check ($ value , $ schema , $ path , $ i );
283261
284262 $ this ->addErrors ($ validator ->getErrors ());
@@ -294,7 +272,7 @@ protected function checkEnum($value, $schema = null, JsonPointer $path = null, $
294272 */
295273 protected function checkFormat ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
296274 {
297- $ validator = $ this ->getFactory () ->createInstanceFor ('format ' );
275+ $ validator = $ this ->factory ->createInstanceFor ('format ' );
298276 $ validator ->check ($ value , $ schema , $ path , $ i );
299277
300278 $ this ->addErrors ($ validator ->getErrors ());
@@ -307,7 +285,7 @@ protected function checkFormat($value, $schema = null, JsonPointer $path = null,
307285 */
308286 protected function getTypeCheck ()
309287 {
310- return $ this ->getFactory () ->getTypeCheck ();
288+ return $ this ->factory ->getTypeCheck ();
311289 }
312290
313291 /**
0 commit comments