@@ -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
*/
@@ -137,7 +156,7 @@ protected function incrementPath($path, $i)
137
156
*/
138
157
protected function checkArray ($ value , $ schema = null , $ path = null , $ i = null )
139
158
{
140
- $ validator = new CollectionConstraint ( $ this ->checkMode , $ this -> uriRetriever );
159
+ $ validator = $ this ->getFactory ()-> createInstanceFor ( ' collection ' );
141
160
$ validator ->check ($ value , $ schema , $ path , $ i );
142
161
143
162
$ this ->addErrors ($ validator ->getErrors ());
@@ -154,7 +173,7 @@ protected function checkArray($value, $schema = null, $path = null, $i = null)
154
173
*/
155
174
protected function checkObject ($ value , $ schema = null , $ path = null , $ i = null , $ patternProperties = null )
156
175
{
157
- $ validator = new ObjectConstraint ( $ this ->checkMode , $ this -> uriRetriever );
176
+ $ validator = $ this ->getFactory ()-> createInstanceFor ( ' object ' );
158
177
$ validator ->check ($ value , $ schema , $ path , $ i , $ patternProperties );
159
178
160
179
$ this ->addErrors ($ validator ->getErrors ());
@@ -170,7 +189,7 @@ protected function checkObject($value, $schema = null, $path = null, $i = null,
170
189
*/
171
190
protected function checkType ($ value , $ schema = null , $ path = null , $ i = null )
172
191
{
173
- $ validator = new TypeConstraint ( $ this ->checkMode , $ this -> uriRetriever );
192
+ $ validator = $ this ->getFactory ()-> createInstanceFor ( ' type ' );
174
193
$ validator ->check ($ value , $ schema , $ path , $ i );
175
194
176
195
$ this ->addErrors ($ validator ->getErrors ());
@@ -186,7 +205,7 @@ protected function checkType($value, $schema = null, $path = null, $i = null)
186
205
*/
187
206
protected function checkUndefined ($ value , $ schema = null , $ path = null , $ i = null )
188
207
{
189
- $ validator = new UndefinedConstraint ( $ this ->checkMode , $ this -> uriRetriever );
208
+ $ validator = $ this ->getFactory ()-> createInstanceFor ( ' undefined ' );
190
209
$ validator ->check ($ value , $ schema , $ path , $ i );
191
210
192
211
$ this ->addErrors ($ validator ->getErrors ());
@@ -202,7 +221,7 @@ protected function checkUndefined($value, $schema = null, $path = null, $i = nul
202
221
*/
203
222
protected function checkString ($ value , $ schema = null , $ path = null , $ i = null )
204
223
{
205
- $ validator = new StringConstraint ( $ this ->checkMode , $ this -> uriRetriever );
224
+ $ validator = $ this ->getFactory ()-> createInstanceFor ( ' string ' );
206
225
$ validator ->check ($ value , $ schema , $ path , $ i );
207
226
208
227
$ this ->addErrors ($ validator ->getErrors ());
@@ -218,7 +237,7 @@ protected function checkString($value, $schema = null, $path = null, $i = null)
218
237
*/
219
238
protected function checkNumber ($ value , $ schema = null , $ path = null , $ i = null )
220
239
{
221
- $ validator = new NumberConstraint ( $ this ->checkMode , $ this -> uriRetriever );
240
+ $ validator = $ this ->getFactory ()-> createInstanceFor ( ' number ' );
222
241
$ validator ->check ($ value , $ schema , $ path , $ i );
223
242
224
243
$ this ->addErrors ($ validator ->getErrors ());
@@ -234,15 +253,15 @@ protected function checkNumber($value, $schema = null, $path = null, $i = null)
234
253
*/
235
254
protected function checkEnum ($ value , $ schema = null , $ path = null , $ i = null )
236
255
{
237
- $ validator = new EnumConstraint ( $ this ->checkMode , $ this -> uriRetriever );
256
+ $ validator = $ this ->getFactory ()-> createInstanceFor ( ' enum ' );
238
257
$ validator ->check ($ value , $ schema , $ path , $ i );
239
258
240
259
$ this ->addErrors ($ validator ->getErrors ());
241
260
}
242
261
243
262
protected function checkFormat ($ value , $ schema = null , $ path = null , $ i = null )
244
263
{
245
- $ validator = new FormatConstraint ( $ this ->checkMode , $ this -> uriRetriever );
264
+ $ validator = $ this ->getFactory ()-> createInstanceFor ( ' format ' );
246
265
$ validator ->check ($ value , $ schema , $ path , $ i );
247
266
248
267
$ this ->addErrors ($ validator ->getErrors ());
0 commit comments