9
9
10
10
namespace JsonSchema \Constraints ;
11
11
12
+ use JsonSchema \SchemaStorage ;
12
13
use JsonSchema \Uri \UriRetriever ;
13
- use JsonSchema \Validator ;
14
+ use JsonSchema \UriRetrieverInterface ;
14
15
use JsonSchema \Entity \JsonPointer ;
15
16
16
17
/**
21
22
*/
22
23
abstract class Constraint implements ConstraintInterface
23
24
{
25
+ protected $ schemaStorage ;
24
26
protected $ checkMode = self ::CHECK_MODE_NORMAL ;
25
27
protected $ uriRetriever ;
26
28
protected $ errors = array ();
@@ -35,19 +37,25 @@ abstract class Constraint implements ConstraintInterface
35
37
private $ factory ;
36
38
37
39
/**
38
- * @param int $checkMode
39
- * @param UriRetriever $uriRetriever
40
- * @param Factory $factory
40
+ * @param int $checkMode
41
+ * @param SchemaStorage $schemaStorage
42
+ * @param UriRetrieverInterface $uriRetriever
43
+ * @param Factory $factory
41
44
*/
42
- public function __construct ($ checkMode = self ::CHECK_MODE_NORMAL , UriRetriever $ uriRetriever = null , Factory $ factory = null )
43
- {
44
- $ this ->checkMode = $ checkMode ;
45
- $ this ->uriRetriever = $ uriRetriever ;
46
- $ this ->factory = $ factory ;
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 ;
47
55
}
48
56
49
57
/**
50
- * @return UriRetriever $uriRetriever
58
+ * @return UriRetrieverInterface $uriRetriever
51
59
*/
52
60
public function getUriRetriever ()
53
61
{
@@ -64,16 +72,28 @@ public function getUriRetriever()
64
72
public function getFactory ()
65
73
{
66
74
if (!$ this ->factory ) {
67
- $ this ->factory = new Factory ($ this ->getUriRetriever (), $ this ->checkMode );
75
+ $ this ->factory = new Factory ($ this ->getSchemaStorage (), $ this -> getUriRetriever (), $ this ->checkMode );
68
76
}
69
77
70
78
return $ this ->factory ;
71
79
}
72
80
73
81
/**
74
- * @param UriRetriever $uriRetriever
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
75
95
*/
76
- public function setUriRetriever (UriRetriever $ uriRetriever )
96
+ public function setUriRetriever (UriRetrieverInterface $ uriRetriever )
77
97
{
78
98
$ this ->uriRetriever = $ uriRetriever ;
79
99
}
@@ -211,7 +231,7 @@ protected function checkType($value, $schema = null, JsonPointer $path = null, $
211
231
protected function checkUndefined ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
212
232
{
213
233
$ validator = $ this ->getFactory ()->createInstanceFor ('undefined ' );
214
- $ validator ->check ($ value , $ schema , $ path , $ i );
234
+ $ validator ->check ($ value , $ this -> schemaStorage -> resolveRefSchema ( $ schema) , $ path , $ i );
215
235
216
236
$ this ->addErrors ($ validator ->getErrors ());
217
237
}
@@ -280,20 +300,6 @@ protected function checkFormat($value, $schema = null, JsonPointer $path = null,
280
300
$ this ->addErrors ($ validator ->getErrors ());
281
301
}
282
302
283
- /**
284
- * @param string $uri JSON Schema URI
285
- * @return string JSON Schema contents
286
- */
287
- protected function retrieveUri ($ uri )
288
- {
289
- if (null === $ this ->uriRetriever ) {
290
- $ this ->setUriRetriever (new UriRetriever );
291
- }
292
- $ jsonSchema = $ this ->uriRetriever ->retrieve ($ uri );
293
- // TODO validate using schema
294
- return $ jsonSchema ;
295
- }
296
-
297
303
/**
298
304
* Get the type check based on the set check mode.
299
305
*
0 commit comments