99
1010namespace JsonSchema \Constraints ;
1111
12+ use JsonSchema \SchemaStorage ;
1213use JsonSchema \Uri \UriRetriever ;
13- use JsonSchema \Validator ;
14+ use JsonSchema \UriRetrieverInterface ;
1415use JsonSchema \Entity \JsonPointer ;
1516
1617/**
2122 */
2223abstract class Constraint implements ConstraintInterface
2324{
25+ protected $ schemaStorage ;
2426 protected $ checkMode = self ::CHECK_MODE_NORMAL ;
2527 protected $ uriRetriever ;
2628 protected $ errors = array ();
@@ -35,19 +37,25 @@ abstract class Constraint implements ConstraintInterface
3537 private $ factory ;
3638
3739 /**
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
4144 */
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 ;
4755 }
4856
4957 /**
50- * @return UriRetriever $uriRetriever
58+ * @return UriRetrieverInterface $uriRetriever
5159 */
5260 public function getUriRetriever ()
5361 {
@@ -64,16 +72,28 @@ public function getUriRetriever()
6472 public function getFactory ()
6573 {
6674 if (!$ this ->factory ) {
67- $ this ->factory = new Factory ($ this ->getUriRetriever (), $ this ->checkMode );
75+ $ this ->factory = new Factory ($ this ->getSchemaStorage (), $ this -> getUriRetriever (), $ this ->checkMode );
6876 }
6977
7078 return $ this ->factory ;
7179 }
7280
7381 /**
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
7595 */
76- public function setUriRetriever (UriRetriever $ uriRetriever )
96+ public function setUriRetriever (UriRetrieverInterface $ uriRetriever )
7797 {
7898 $ this ->uriRetriever = $ uriRetriever ;
7999 }
@@ -211,7 +231,7 @@ protected function checkType($value, $schema = null, JsonPointer $path = null, $
211231 protected function checkUndefined ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
212232 {
213233 $ validator = $ this ->getFactory ()->createInstanceFor ('undefined ' );
214- $ validator ->check ($ value , $ schema , $ path , $ i );
234+ $ validator ->check ($ value , $ this -> schemaStorage -> resolveRefSchema ( $ schema) , $ path , $ i );
215235
216236 $ this ->addErrors ($ validator ->getErrors ());
217237 }
@@ -280,20 +300,6 @@ protected function checkFormat($value, $schema = null, JsonPointer $path = null,
280300 $ this ->addErrors ($ validator ->getErrors ());
281301 }
282302
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-
297303 /**
298304 * Get the type check based on the set check mode.
299305 *
0 commit comments