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 */
37- private $ factory ;
34+ protected $ 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 ()
61- {
62- if (is_null ($ this ->uriRetriever )) {
63- $ this ->setUriRetriever (new UriRetriever );
64- }
65-
66- return $ this ->uriRetriever ;
67- }
68-
69- /**
70- * @return Factory
71- */
72- public function getFactory ()
39+ public function __construct (Factory $ factory = null )
7340 {
74- if (!$ this ->factory ) {
75- $ this ->factory = new Factory ($ this ->getSchemaStorage (), $ this ->getUriRetriever (), $ this ->checkMode );
76- }
77-
78- return $ this ->factory ;
79- }
80-
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 ;
41+ $ this ->factory = $ factory ? : new Factory ();
9942 }
10043
10144 /**
@@ -123,7 +66,9 @@ public function addError(JsonPointer $path = null, $message, $constraint='', arr
12366 */
12467 public function addErrors (array $ errors )
12568 {
126- $ this ->errors = array_merge ($ this ->errors , $ errors );
69+ if ($ errors ) {
70+ $ this ->errors = array_merge ($ this ->errors , $ errors );
71+ }
12772 }
12873
12974 /**
@@ -181,7 +126,7 @@ protected function incrementPath(JsonPointer $path = null, $i)
181126 */
182127 protected function checkArray ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
183128 {
184- $ validator = $ this ->getFactory () ->createInstanceFor ('collection ' );
129+ $ validator = $ this ->factory ->createInstanceFor ('collection ' );
185130 $ validator ->check ($ value , $ schema , $ path , $ i );
186131
187132 $ this ->addErrors ($ validator ->getErrors ());
@@ -198,7 +143,7 @@ protected function checkArray($value, $schema = null, JsonPointer $path = null,
198143 */
199144 protected function checkObject ($ value , $ schema = null , JsonPointer $ path = null , $ i = null , $ patternProperties = null )
200145 {
201- $ validator = $ this ->getFactory () ->createInstanceFor ('object ' );
146+ $ validator = $ this ->factory ->createInstanceFor ('object ' );
202147 $ validator ->check ($ value , $ schema , $ path , $ i , $ patternProperties );
203148
204149 $ this ->addErrors ($ validator ->getErrors ());
@@ -214,7 +159,7 @@ protected function checkObject($value, $schema = null, JsonPointer $path = null,
214159 */
215160 protected function checkType ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
216161 {
217- $ validator = $ this ->getFactory () ->createInstanceFor ('type ' );
162+ $ validator = $ this ->factory ->createInstanceFor ('type ' );
218163 $ validator ->check ($ value , $ schema , $ path , $ i );
219164
220165 $ this ->addErrors ($ validator ->getErrors ());
@@ -230,8 +175,9 @@ protected function checkType($value, $schema = null, JsonPointer $path = null, $
230175 */
231176 protected function checkUndefined ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
232177 {
233- $ validator = $ this ->getFactory ()->createInstanceFor ('undefined ' );
234- $ validator ->check ($ value , $ this ->schemaStorage ->resolveRefSchema ($ schema ), $ path , $ i );
178+ $ validator = $ this ->factory ->createInstanceFor ('undefined ' );
179+
180+ $ validator ->check ($ value , $ this ->factory ->getSchemaStorage ()->resolveRefSchema ($ schema ), $ path , $ i );
235181
236182 $ this ->addErrors ($ validator ->getErrors ());
237183 }
@@ -246,7 +192,7 @@ protected function checkUndefined($value, $schema = null, JsonPointer $path = nu
246192 */
247193 protected function checkString ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
248194 {
249- $ validator = $ this ->getFactory () ->createInstanceFor ('string ' );
195+ $ validator = $ this ->factory ->createInstanceFor ('string ' );
250196 $ validator ->check ($ value , $ schema , $ path , $ i );
251197
252198 $ this ->addErrors ($ validator ->getErrors ());
@@ -262,7 +208,7 @@ protected function checkString($value, $schema = null, JsonPointer $path = null,
262208 */
263209 protected function checkNumber ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
264210 {
265- $ validator = $ this ->getFactory () ->createInstanceFor ('number ' );
211+ $ validator = $ this ->factory ->createInstanceFor ('number ' );
266212 $ validator ->check ($ value , $ schema , $ path , $ i );
267213
268214 $ this ->addErrors ($ validator ->getErrors ());
@@ -278,7 +224,7 @@ protected function checkNumber($value, $schema = null, JsonPointer $path = null,
278224 */
279225 protected function checkEnum ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
280226 {
281- $ validator = $ this ->getFactory () ->createInstanceFor ('enum ' );
227+ $ validator = $ this ->factory ->createInstanceFor ('enum ' );
282228 $ validator ->check ($ value , $ schema , $ path , $ i );
283229
284230 $ this ->addErrors ($ validator ->getErrors ());
@@ -294,7 +240,7 @@ protected function checkEnum($value, $schema = null, JsonPointer $path = null, $
294240 */
295241 protected function checkFormat ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
296242 {
297- $ validator = $ this ->getFactory () ->createInstanceFor ('format ' );
243+ $ validator = $ this ->factory ->createInstanceFor ('format ' );
298244 $ validator ->check ($ value , $ schema , $ path , $ i );
299245
300246 $ this ->addErrors ($ validator ->getErrors ());
@@ -307,7 +253,7 @@ protected function checkFormat($value, $schema = null, JsonPointer $path = null,
307253 */
308254 protected function getTypeCheck ()
309255 {
310- return $ this ->getFactory () ->getTypeCheck ();
256+ return $ this ->factory ->getTypeCheck ();
311257 }
312258
313259 /**
0 commit comments