@@ -27,14 +27,21 @@ abstract class Constraint implements ConstraintInterface
27
27
const CHECK_MODE_NORMAL = 1 ;
28
28
const CHECK_MODE_TYPE_CAST = 2 ;
29
29
30
+ /**
31
+ * @var null|Factory
32
+ */
33
+ private $ factory ;
34
+
30
35
/**
31
36
* @param int $checkMode
32
37
* @param UriRetriever $uriRetriever
38
+ * @param Factory $factory
33
39
*/
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 )
35
41
{
36
42
$ this ->checkMode = $ checkMode ;
37
43
$ this ->uriRetriever = $ uriRetriever ;
44
+ $ this ->factory = $ factory ;
38
45
}
39
46
40
47
/**
@@ -50,6 +57,18 @@ public function getUriRetriever()
50
57
return $ this ->uriRetriever ;
51
58
}
52
59
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
+
53
72
/**
54
73
* @param UriRetriever $uriRetriever
55
74
*/
@@ -145,7 +164,7 @@ protected function incrementPath($path, $i)
145
164
*/
146
165
protected function checkArray ($ value , $ schema = null , $ path = null , $ i = null )
147
166
{
148
- $ validator = new CollectionConstraint ( $ this ->checkMode , $ this -> uriRetriever );
167
+ $ validator = $ this ->getFactory ()-> createInstanceFor ( ' collection ' );
149
168
$ validator ->check ($ value , $ schema , $ path , $ i );
150
169
151
170
$ this ->addErrors ($ validator ->getErrors ());
@@ -162,7 +181,7 @@ protected function checkArray($value, $schema = null, $path = null, $i = null)
162
181
*/
163
182
protected function checkObject ($ value , $ schema = null , $ path = null , $ i = null , $ patternProperties = null )
164
183
{
165
- $ validator = new ObjectConstraint ( $ this ->checkMode , $ this -> uriRetriever );
184
+ $ validator = $ this ->getFactory ()-> createInstanceFor ( ' object ' );
166
185
$ validator ->check ($ value , $ schema , $ path , $ i , $ patternProperties );
167
186
168
187
$ this ->addErrors ($ validator ->getErrors ());
@@ -178,7 +197,7 @@ protected function checkObject($value, $schema = null, $path = null, $i = null,
178
197
*/
179
198
protected function checkType ($ value , $ schema = null , $ path = null , $ i = null )
180
199
{
181
- $ validator = new TypeConstraint ( $ this ->checkMode , $ this -> uriRetriever );
200
+ $ validator = $ this ->getFactory ()-> createInstanceFor ( ' type ' );
182
201
$ validator ->check ($ value , $ schema , $ path , $ i );
183
202
184
203
$ this ->addErrors ($ validator ->getErrors ());
@@ -194,7 +213,7 @@ protected function checkType($value, $schema = null, $path = null, $i = null)
194
213
*/
195
214
protected function checkUndefined ($ value , $ schema = null , $ path = null , $ i = null )
196
215
{
197
- $ validator = new UndefinedConstraint ( $ this ->checkMode , $ this -> uriRetriever );
216
+ $ validator = $ this ->getFactory ()-> createInstanceFor ( ' undefined ' );
198
217
$ validator ->check ($ value , $ schema , $ path , $ i );
199
218
200
219
$ this ->addErrors ($ validator ->getErrors ());
@@ -210,7 +229,7 @@ protected function checkUndefined($value, $schema = null, $path = null, $i = nul
210
229
*/
211
230
protected function checkString ($ value , $ schema = null , $ path = null , $ i = null )
212
231
{
213
- $ validator = new StringConstraint ( $ this ->checkMode , $ this -> uriRetriever );
232
+ $ validator = $ this ->getFactory ()-> createInstanceFor ( ' string ' );
214
233
$ validator ->check ($ value , $ schema , $ path , $ i );
215
234
216
235
$ this ->addErrors ($ validator ->getErrors ());
@@ -226,7 +245,7 @@ protected function checkString($value, $schema = null, $path = null, $i = null)
226
245
*/
227
246
protected function checkNumber ($ value , $ schema = null , $ path = null , $ i = null )
228
247
{
229
- $ validator = new NumberConstraint ( $ this ->checkMode , $ this -> uriRetriever );
248
+ $ validator = $ this ->getFactory ()-> createInstanceFor ( ' number ' );
230
249
$ validator ->check ($ value , $ schema , $ path , $ i );
231
250
232
251
$ this ->addErrors ($ validator ->getErrors ());
@@ -242,15 +261,15 @@ protected function checkNumber($value, $schema = null, $path = null, $i = null)
242
261
*/
243
262
protected function checkEnum ($ value , $ schema = null , $ path = null , $ i = null )
244
263
{
245
- $ validator = new EnumConstraint ( $ this ->checkMode , $ this -> uriRetriever );
264
+ $ validator = $ this ->getFactory ()-> createInstanceFor ( ' enum ' );
246
265
$ validator ->check ($ value , $ schema , $ path , $ i );
247
266
248
267
$ this ->addErrors ($ validator ->getErrors ());
249
268
}
250
269
251
270
protected function checkFormat ($ value , $ schema = null , $ path = null , $ i = null )
252
271
{
253
- $ validator = new FormatConstraint ( $ this ->checkMode , $ this -> uriRetriever );
272
+ $ validator = $ this ->getFactory ()-> createInstanceFor ( ' format ' );
254
273
$ validator ->check ($ value , $ schema , $ path , $ i );
255
274
256
275
$ this ->addErrors ($ validator ->getErrors ());
0 commit comments