Web Interfaces of PROSUME

SchemaStorage.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace JsonSchema;
  3. use JsonSchema\Constraints\BaseConstraint;
  4. use JsonSchema\Entity\JsonPointer;
  5. use JsonSchema\Exception\UnresolvableJsonPointerException;
  6. use JsonSchema\Uri\UriResolver;
  7. use JsonSchema\Uri\UriRetriever;
  8. class SchemaStorage implements SchemaStorageInterface
  9. {
  10. const INTERNAL_PROVIDED_SCHEMA_URI = 'internal://provided-schema/';
  11. protected $uriRetriever;
  12. protected $uriResolver;
  13. protected $schemas = array();
  14. public function __construct(
  15. UriRetrieverInterface $uriRetriever = null,
  16. UriResolverInterface $uriResolver = null
  17. ) {
  18. $this->uriRetriever = $uriRetriever ?: new UriRetriever();
  19. $this->uriResolver = $uriResolver ?: new UriResolver();
  20. }
  21. /**
  22. * @return UriRetrieverInterface
  23. */
  24. public function getUriRetriever()
  25. {
  26. return $this->uriRetriever;
  27. }
  28. /**
  29. * @return UriResolverInterface
  30. */
  31. public function getUriResolver()
  32. {
  33. return $this->uriResolver;
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function addSchema($id, $schema = null)
  39. {
  40. if (is_null($schema) && $id !== self::INTERNAL_PROVIDED_SCHEMA_URI) {
  41. // if the schema was user-provided to Validator and is still null, then assume this is
  42. // what the user intended, as there's no way for us to retrieve anything else. User-supplied
  43. // schemas do not have an associated URI when passed via Validator::validate().
  44. $schema = $this->uriRetriever->retrieve($id);
  45. }
  46. // cast array schemas to object
  47. if (is_array($schema)) {
  48. $schema = BaseConstraint::arrayToObjectRecursive($schema);
  49. }
  50. // workaround for bug in draft-03 & draft-04 meta-schemas (id & $ref defined with incorrect format)
  51. // see https://github.com/json-schema-org/JSON-Schema-Test-Suite/issues/177#issuecomment-293051367
  52. if (is_object($schema) && property_exists($schema, 'id')) {
  53. if ($schema->id == 'http://json-schema.org/draft-04/schema#') {
  54. $schema->properties->id->format = 'uri-reference';
  55. } elseif ($schema->id == 'http://json-schema.org/draft-03/schema#') {
  56. $schema->properties->id->format = 'uri-reference';
  57. $schema->properties->{'$ref'}->format = 'uri-reference';
  58. }
  59. }
  60. // resolve references
  61. $this->expandRefs($schema, $id);
  62. $this->schemas[$id] = $schema;
  63. }
  64. /**
  65. * Recursively resolve all references against the provided base
  66. *
  67. * @param mixed $schema
  68. * @param string $base
  69. */
  70. private function expandRefs(&$schema, $base = null)
  71. {
  72. if (!is_object($schema)) {
  73. if (is_array($schema)) {
  74. foreach ($schema as &$member) {
  75. $this->expandRefs($member, $base);
  76. }
  77. }
  78. return;
  79. }
  80. if (property_exists($schema, 'id') && is_string($schema->id) && $base != $schema->id) {
  81. $base = $this->uriResolver->resolve($schema->id, $base);
  82. }
  83. if (property_exists($schema, '$ref') && is_string($schema->{'$ref'})) {
  84. $refPointer = new JsonPointer($this->uriResolver->resolve($schema->{'$ref'}, $base));
  85. $schema->{'$ref'} = (string) $refPointer;
  86. }
  87. foreach ($schema as &$member) {
  88. $this->expandRefs($member, $base);
  89. }
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function getSchema($id)
  95. {
  96. if (!array_key_exists($id, $this->schemas)) {
  97. $this->addSchema($id);
  98. }
  99. return $this->schemas[$id];
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function resolveRef($ref)
  105. {
  106. $jsonPointer = new JsonPointer($ref);
  107. // resolve filename for pointer
  108. $fileName = $jsonPointer->getFilename();
  109. if (!strlen($fileName)) {
  110. throw new UnresolvableJsonPointerException(sprintf(
  111. "Could not resolve fragment '%s': no file is defined",
  112. $jsonPointer->getPropertyPathAsString()
  113. ));
  114. }
  115. // get & process the schema
  116. $refSchema = $this->getSchema($fileName);
  117. foreach ($jsonPointer->getPropertyPaths() as $path) {
  118. if (is_object($refSchema) && property_exists($refSchema, $path)) {
  119. $refSchema = $this->resolveRefSchema($refSchema->{$path});
  120. } elseif (is_array($refSchema) && array_key_exists($path, $refSchema)) {
  121. $refSchema = $this->resolveRefSchema($refSchema[$path]);
  122. } else {
  123. throw new UnresolvableJsonPointerException(sprintf(
  124. 'File: %s is found, but could not resolve fragment: %s',
  125. $jsonPointer->getFilename(),
  126. $jsonPointer->getPropertyPathAsString()
  127. ));
  128. }
  129. }
  130. return $refSchema;
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function resolveRefSchema($refSchema)
  136. {
  137. if (is_object($refSchema) && property_exists($refSchema, '$ref') && is_string($refSchema->{'$ref'})) {
  138. $newSchema = $this->resolveRef($refSchema->{'$ref'});
  139. $refSchema = (object) (get_object_vars($refSchema) + get_object_vars($newSchema));
  140. unset($refSchema->{'$ref'});
  141. }
  142. return $refSchema;
  143. }
  144. }