@@ -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 */
@@ -137,7 +156,7 @@ protected function incrementPath($path, $i)
137156 */
138157 protected function checkArray ($ value , $ schema = null , $ path = null , $ i = null )
139158 {
140- $ validator = new CollectionConstraint ( $ this ->checkMode , $ this -> uriRetriever );
159+ $ validator = $ this ->getFactory ()-> createInstanceFor ( ' collection ' );
141160 $ validator ->check ($ value , $ schema , $ path , $ i );
142161
143162 $ this ->addErrors ($ validator ->getErrors ());
@@ -154,7 +173,7 @@ protected function checkArray($value, $schema = null, $path = null, $i = null)
154173 */
155174 protected function checkObject ($ value , $ schema = null , $ path = null , $ i = null , $ patternProperties = null )
156175 {
157- $ validator = new ObjectConstraint ( $ this ->checkMode , $ this -> uriRetriever );
176+ $ validator = $ this ->getFactory ()-> createInstanceFor ( ' object ' );
158177 $ validator ->check ($ value , $ schema , $ path , $ i , $ patternProperties );
159178
160179 $ this ->addErrors ($ validator ->getErrors ());
@@ -170,7 +189,7 @@ protected function checkObject($value, $schema = null, $path = null, $i = null,
170189 */
171190 protected function checkType ($ value , $ schema = null , $ path = null , $ i = null )
172191 {
173- $ validator = new TypeConstraint ( $ this ->checkMode , $ this -> uriRetriever );
192+ $ validator = $ this ->getFactory ()-> createInstanceFor ( ' type ' );
174193 $ validator ->check ($ value , $ schema , $ path , $ i );
175194
176195 $ this ->addErrors ($ validator ->getErrors ());
@@ -186,7 +205,7 @@ protected function checkType($value, $schema = null, $path = null, $i = null)
186205 */
187206 protected function checkUndefined ($ value , $ schema = null , $ path = null , $ i = null )
188207 {
189- $ validator = new UndefinedConstraint ( $ this ->checkMode , $ this -> uriRetriever );
208+ $ validator = $ this ->getFactory ()-> createInstanceFor ( ' undefined ' );
190209 $ validator ->check ($ value , $ schema , $ path , $ i );
191210
192211 $ this ->addErrors ($ validator ->getErrors ());
@@ -202,7 +221,7 @@ protected function checkUndefined($value, $schema = null, $path = null, $i = nul
202221 */
203222 protected function checkString ($ value , $ schema = null , $ path = null , $ i = null )
204223 {
205- $ validator = new StringConstraint ( $ this ->checkMode , $ this -> uriRetriever );
224+ $ validator = $ this ->getFactory ()-> createInstanceFor ( ' string ' );
206225 $ validator ->check ($ value , $ schema , $ path , $ i );
207226
208227 $ this ->addErrors ($ validator ->getErrors ());
@@ -218,7 +237,7 @@ protected function checkString($value, $schema = null, $path = null, $i = null)
218237 */
219238 protected function checkNumber ($ value , $ schema = null , $ path = null , $ i = null )
220239 {
221- $ validator = new NumberConstraint ( $ this ->checkMode , $ this -> uriRetriever );
240+ $ validator = $ this ->getFactory ()-> createInstanceFor ( ' number ' );
222241 $ validator ->check ($ value , $ schema , $ path , $ i );
223242
224243 $ this ->addErrors ($ validator ->getErrors ());
@@ -234,15 +253,15 @@ protected function checkNumber($value, $schema = null, $path = null, $i = null)
234253 */
235254 protected function checkEnum ($ value , $ schema = null , $ path = null , $ i = null )
236255 {
237- $ validator = new EnumConstraint ( $ this ->checkMode , $ this -> uriRetriever );
256+ $ validator = $ this ->getFactory ()-> createInstanceFor ( ' enum ' );
238257 $ validator ->check ($ value , $ schema , $ path , $ i );
239258
240259 $ this ->addErrors ($ validator ->getErrors ());
241260 }
242261
243262 protected function checkFormat ($ value , $ schema = null , $ path = null , $ i = null )
244263 {
245- $ validator = new FormatConstraint ( $ this ->checkMode , $ this -> uriRetriever );
264+ $ validator = $ this ->getFactory ()-> createInstanceFor ( ' format ' );
246265 $ validator ->check ($ value , $ schema , $ path , $ i );
247266
248267 $ this ->addErrors ($ validator ->getErrors ());
0 commit comments