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 ;
55- }
56-
57- /**
58- * @return UriRetrieverInterface $uriRetriever
59- */
60- public function getUriRetriever ()
39+ public function __construct (Factory $ factory = null )
6140 {
62- if (is_null ($ this ->uriRetriever )) {
63- $ this ->setUriRetriever (new UriRetriever );
64- }
65-
66- return $ this ->uriRetriever ;
41+ $ this ->factory = $ factory ? : $ this ->getFactory ();
6742 }
6843
6944 /**
@@ -72,32 +47,12 @@ public function getUriRetriever()
7247 public function getFactory ()
7348 {
7449 if (!$ this ->factory ) {
75- $ this ->factory = new Factory ($ this -> getSchemaStorage (), $ this -> getUriRetriever (), $ this -> checkMode );
50+ $ this ->factory = new Factory ();
7651 }
7752
7853 return $ this ->factory ;
7954 }
8055
81- /**
82- * @return SchemaStorage
83- */
84- public function getSchemaStorage ()
85- {
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 ;
99- }
100-
10156 /**
10257 * {@inheritDoc}
10358 */
@@ -123,7 +78,9 @@ public function addError(JsonPointer $path = null, $message, $constraint='', arr
12378 */
12479 public function addErrors (array $ errors )
12580 {
126- $ this ->errors = array_merge ($ this ->errors , $ errors );
81+ if ($ errors ) {
82+ $ this ->errors = array_merge ($ this ->errors , $ errors );
83+ }
12784 }
12885
12986 /**
@@ -181,7 +138,7 @@ protected function incrementPath(JsonPointer $path = null, $i)
181138 */
182139 protected function checkArray ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
183140 {
184- $ validator = $ this ->getFactory () ->createInstanceFor ('collection ' );
141+ $ validator = $ this ->factory ->createInstanceFor ('collection ' );
185142 $ validator ->check ($ value , $ schema , $ path , $ i );
186143
187144 $ this ->addErrors ($ validator ->getErrors ());
@@ -198,7 +155,7 @@ protected function checkArray($value, $schema = null, JsonPointer $path = null,
198155 */
199156 protected function checkObject ($ value , $ schema = null , JsonPointer $ path = null , $ i = null , $ patternProperties = null )
200157 {
201- $ validator = $ this ->getFactory () ->createInstanceFor ('object ' );
158+ $ validator = $ this ->factory ->createInstanceFor ('object ' );
202159 $ validator ->check ($ value , $ schema , $ path , $ i , $ patternProperties );
203160
204161 $ this ->addErrors ($ validator ->getErrors ());
@@ -214,7 +171,7 @@ protected function checkObject($value, $schema = null, JsonPointer $path = null,
214171 */
215172 protected function checkType ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
216173 {
217- $ validator = $ this ->getFactory () ->createInstanceFor ('type ' );
174+ $ validator = $ this ->factory ->createInstanceFor ('type ' );
218175 $ validator ->check ($ value , $ schema , $ path , $ i );
219176
220177 $ this ->addErrors ($ validator ->getErrors ());
@@ -230,8 +187,8 @@ protected function checkType($value, $schema = null, JsonPointer $path = null, $
230187 */
231188 protected function checkUndefined ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
232189 {
233- $ validator = $ this ->getFactory () ->createInstanceFor ('undefined ' );
234- $ validator ->check ($ value , $ this ->schemaStorage ->resolveRefSchema ($ schema ), $ path , $ i );
190+ $ validator = $ this ->factory ->createInstanceFor ('undefined ' );
191+ $ validator ->check ($ value , $ this ->getFactory ()-> getSchemaStorage () ->resolveRefSchema ($ schema ), $ path , $ i );
235192
236193 $ this ->addErrors ($ validator ->getErrors ());
237194 }
@@ -246,7 +203,7 @@ protected function checkUndefined($value, $schema = null, JsonPointer $path = nu
246203 */
247204 protected function checkString ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
248205 {
249- $ validator = $ this ->getFactory () ->createInstanceFor ('string ' );
206+ $ validator = $ this ->factory ->createInstanceFor ('string ' );
250207 $ validator ->check ($ value , $ schema , $ path , $ i );
251208
252209 $ this ->addErrors ($ validator ->getErrors ());
@@ -262,7 +219,7 @@ protected function checkString($value, $schema = null, JsonPointer $path = null,
262219 */
263220 protected function checkNumber ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
264221 {
265- $ validator = $ this ->getFactory () ->createInstanceFor ('number ' );
222+ $ validator = $ this ->factory ->createInstanceFor ('number ' );
266223 $ validator ->check ($ value , $ schema , $ path , $ i );
267224
268225 $ this ->addErrors ($ validator ->getErrors ());
@@ -278,7 +235,7 @@ protected function checkNumber($value, $schema = null, JsonPointer $path = null,
278235 */
279236 protected function checkEnum ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
280237 {
281- $ validator = $ this ->getFactory () ->createInstanceFor ('enum ' );
238+ $ validator = $ this ->factory ->createInstanceFor ('enum ' );
282239 $ validator ->check ($ value , $ schema , $ path , $ i );
283240
284241 $ this ->addErrors ($ validator ->getErrors ());
@@ -294,7 +251,7 @@ protected function checkEnum($value, $schema = null, JsonPointer $path = null, $
294251 */
295252 protected function checkFormat ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
296253 {
297- $ validator = $ this ->getFactory () ->createInstanceFor ('format ' );
254+ $ validator = $ this ->factory ->createInstanceFor ('format ' );
298255 $ validator ->check ($ value , $ schema , $ path , $ i );
299256
300257 $ this ->addErrors ($ validator ->getErrors ());
@@ -307,7 +264,7 @@ protected function checkFormat($value, $schema = null, JsonPointer $path = null,
307264 */
308265 protected function getTypeCheck ()
309266 {
310- return $ this ->getFactory () ->getTypeCheck ();
267+ return $ this ->factory ->getTypeCheck ();
311268 }
312269
313270 /**
0 commit comments