@@ -27,14 +27,21 @@ abstract class Constraint implements ConstraintInterface
2727 const CHECK_MODE_NORMAL = 1 ;
2828 const CHECK_MODE_TYPE_CAST = 2 ;
2929
30+ /**
31+ * @var null|Factory
32+ */
33+ private $ factory ;
34+
3035 /**
3136 * @param int $checkMode
3237 * @param UriRetriever $uriRetriever
38+ * @param Factory $factory
3339 */
34- public function __construct ($ checkMode = self ::CHECK_MODE_NORMAL , UriRetriever $ uriRetriever = null )
40+ public function __construct ($ checkMode = self ::CHECK_MODE_NORMAL , UriRetriever $ uriRetriever = null , Factory $ factory = null )
3541 {
3642 $ this ->checkMode = $ checkMode ;
3743 $ this ->uriRetriever = $ uriRetriever ;
44+ $ this ->factory = $ factory ;
3845 }
3946
4047 /**
@@ -50,6 +57,18 @@ public function getUriRetriever()
5057 return $ this ->uriRetriever ;
5158 }
5259
60+ /**
61+ * @return Factory
62+ */
63+ public function getFactory ()
64+ {
65+ if (!$ this ->factory ) {
66+ $ this ->factory = new Factory ($ this ->getUriRetriever ());
67+ }
68+
69+ return $ this ->factory ;
70+ }
71+
5372 /**
5473 * @param UriRetriever $uriRetriever
5574 */
@@ -145,7 +164,7 @@ protected function incrementPath($path, $i)
145164 */
146165 protected function checkArray ($ value , $ schema = null , $ path = null , $ i = null )
147166 {
148- $ validator = new CollectionConstraint ( $ this ->checkMode , $ this -> uriRetriever );
167+ $ validator = $ this ->getFactory ()-> createInstanceFor ( ' collection ' );
149168 $ validator ->check ($ value , $ schema , $ path , $ i );
150169
151170 $ this ->addErrors ($ validator ->getErrors ());
@@ -162,7 +181,7 @@ protected function checkArray($value, $schema = null, $path = null, $i = null)
162181 */
163182 protected function checkObject ($ value , $ schema = null , $ path = null , $ i = null , $ patternProperties = null )
164183 {
165- $ validator = new ObjectConstraint ( $ this ->checkMode , $ this -> uriRetriever );
184+ $ validator = $ this ->getFactory ()-> createInstanceFor ( ' object ' );
166185 $ validator ->check ($ value , $ schema , $ path , $ i , $ patternProperties );
167186
168187 $ this ->addErrors ($ validator ->getErrors ());
@@ -178,7 +197,7 @@ protected function checkObject($value, $schema = null, $path = null, $i = null,
178197 */
179198 protected function checkType ($ value , $ schema = null , $ path = null , $ i = null )
180199 {
181- $ validator = new TypeConstraint ( $ this ->checkMode , $ this -> uriRetriever );
200+ $ validator = $ this ->getFactory ()-> createInstanceFor ( ' type ' );
182201 $ validator ->check ($ value , $ schema , $ path , $ i );
183202
184203 $ this ->addErrors ($ validator ->getErrors ());
@@ -194,7 +213,7 @@ protected function checkType($value, $schema = null, $path = null, $i = null)
194213 */
195214 protected function checkUndefined ($ value , $ schema = null , $ path = null , $ i = null )
196215 {
197- $ validator = new UndefinedConstraint ( $ this ->checkMode , $ this -> uriRetriever );
216+ $ validator = $ this ->getFactory ()-> createInstanceFor ( ' undefined ' );
198217 $ validator ->check ($ value , $ schema , $ path , $ i );
199218
200219 $ this ->addErrors ($ validator ->getErrors ());
@@ -210,7 +229,7 @@ protected function checkUndefined($value, $schema = null, $path = null, $i = nul
210229 */
211230 protected function checkString ($ value , $ schema = null , $ path = null , $ i = null )
212231 {
213- $ validator = new StringConstraint ( $ this ->checkMode , $ this -> uriRetriever );
232+ $ validator = $ this ->getFactory ()-> createInstanceFor ( ' string ' );
214233 $ validator ->check ($ value , $ schema , $ path , $ i );
215234
216235 $ this ->addErrors ($ validator ->getErrors ());
@@ -226,7 +245,7 @@ protected function checkString($value, $schema = null, $path = null, $i = null)
226245 */
227246 protected function checkNumber ($ value , $ schema = null , $ path = null , $ i = null )
228247 {
229- $ validator = new NumberConstraint ( $ this ->checkMode , $ this -> uriRetriever );
248+ $ validator = $ this ->getFactory ()-> createInstanceFor ( ' number ' );
230249 $ validator ->check ($ value , $ schema , $ path , $ i );
231250
232251 $ this ->addErrors ($ validator ->getErrors ());
@@ -242,15 +261,15 @@ protected function checkNumber($value, $schema = null, $path = null, $i = null)
242261 */
243262 protected function checkEnum ($ value , $ schema = null , $ path = null , $ i = null )
244263 {
245- $ validator = new EnumConstraint ( $ this ->checkMode , $ this -> uriRetriever );
264+ $ validator = $ this ->getFactory ()-> createInstanceFor ( ' enum ' );
246265 $ validator ->check ($ value , $ schema , $ path , $ i );
247266
248267 $ this ->addErrors ($ validator ->getErrors ());
249268 }
250269
251270 protected function checkFormat ($ value , $ schema = null , $ path = null , $ i = null )
252271 {
253- $ validator = new FormatConstraint ( $ this ->checkMode , $ this -> uriRetriever );
272+ $ validator = $ this ->getFactory ()-> createInstanceFor ( ' format ' );
254273 $ validator ->check ($ value , $ schema , $ path , $ i );
255274
256275 $ this ->addErrors ($ validator ->getErrors ());
0 commit comments