/usr/share/php/JsonSchema/RefResolver.php is in php-json-schema 1.6.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | <?php
/*
* This file is part of the JsonSchema package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace JsonSchema;
use JsonSchema\Exception\JsonDecodingException;
use JsonSchema\Uri\Retrievers\UriRetrieverInterface;
use JsonSchema\Uri\UriRetriever;
/**
* Take in an object that's a JSON schema and take care of all $ref references
*
* @author Tyler Akins <fidian@rumkin.com>
* @see README.md
*/
class RefResolver
{
/**
* HACK to prevent too many recursive expansions.
* Happens e.g. when you want to validate a schema against the schema
* definition.
*
* @var integer
*/
protected static $depth = 0;
/**
* maximum references depth
* @var integer
*/
public static $maxDepth = 7;
/**
* @var UriRetrieverInterface
*/
protected $uriRetriever = null;
/**
* @var object
*/
protected $rootSchema = null;
/**
* @param UriRetriever $retriever
*/
public function __construct($retriever = null)
{
$this->uriRetriever = $retriever;
}
/**
* Retrieves a given schema given a ref and a source URI
*
* @param string $ref Reference from schema
* @param string $sourceUri URI where original schema was located
* @return object Schema
*/
public function fetchRef($ref, $sourceUri)
{
$retriever = $this->getUriRetriever();
$jsonSchema = $retriever->retrieve($ref, $sourceUri);
$this->resolve($jsonSchema);
return $jsonSchema;
}
/**
* Return the URI Retriever, defaulting to making a new one if one
* was not yet set.
*
* @return UriRetriever
*/
public function getUriRetriever()
{
if (is_null($this->uriRetriever)) {
$this->setUriRetriever(new UriRetriever);
}
return $this->uriRetriever;
}
/**
* Resolves all $ref references for a given schema. Recurses through
* the object to resolve references of any child schemas.
*
* The 'format' property is omitted because it isn't required for
* validation. Theoretically, this class could be extended to look
* for URIs in formats: "These custom formats MAY be expressed as
* an URI, and this URI MAY reference a schema of that format."
*
* The 'id' property is not filled in, but that could be made to happen.
*
* @param object $schema JSON Schema to flesh out
* @param string $sourceUri URI where this schema was located
*/
public function resolve($schema, $sourceUri = null)
{
if (self::$depth > self::$maxDepth) {
self::$depth = 0;
throw new JsonDecodingException(JSON_ERROR_DEPTH);
}
++self::$depth;
if (! is_object($schema)) {
--self::$depth;
return;
}
if (null === $sourceUri && ! empty($schema->id)) {
$sourceUri = $schema->id;
}
if (null === $this->rootSchema) {
$this->rootSchema = $schema;
}
// Resolve $ref first
$this->resolveRef($schema, $sourceUri);
// These properties are just schemas
// eg. items can be a schema or an array of schemas
foreach (array('additionalItems', 'additionalProperties', 'extends', 'items') as $propertyName) {
$this->resolveProperty($schema, $propertyName, $sourceUri);
}
// These are all potentially arrays that contain schema objects
// eg. type can be a value or an array of values/schemas
// eg. items can be a schema or an array of schemas
foreach (array('disallow', 'extends', 'items', 'type', 'allOf', 'anyOf', 'oneOf') as $propertyName) {
$this->resolveArrayOfSchemas($schema, $propertyName, $sourceUri);
}
// These are all objects containing properties whose values are schemas
foreach (array('dependencies', 'patternProperties', 'properties') as $propertyName) {
$this->resolveObjectOfSchemas($schema, $propertyName, $sourceUri);
}
--self::$depth;
}
/**
* Given an object and a property name, that property should be an
* array whose values can be schemas.
*
* @param object $schema JSON Schema to flesh out
* @param string $propertyName Property to work on
* @param string $sourceUri URI where this schema was located
*/
public function resolveArrayOfSchemas($schema, $propertyName, $sourceUri)
{
if (! isset($schema->$propertyName) || ! is_array($schema->$propertyName)) {
return;
}
foreach ($schema->$propertyName as $possiblySchema) {
$this->resolve($possiblySchema, $sourceUri);
}
}
/**
* Given an object and a property name, that property should be an
* object whose properties are schema objects.
*
* @param object $schema JSON Schema to flesh out
* @param string $propertyName Property to work on
* @param string $sourceUri URI where this schema was located
*/
public function resolveObjectOfSchemas($schema, $propertyName, $sourceUri)
{
if (! isset($schema->$propertyName) || ! is_object($schema->$propertyName)) {
return;
}
foreach (get_object_vars($schema->$propertyName) as $possiblySchema) {
$this->resolve($possiblySchema, $sourceUri);
}
}
/**
* Given an object and a property name, that property should be a
* schema object.
*
* @param object $schema JSON Schema to flesh out
* @param string $propertyName Property to work on
* @param string $sourceUri URI where this schema was located
*/
public function resolveProperty($schema, $propertyName, $sourceUri)
{
if (! isset($schema->$propertyName)) {
return;
}
$this->resolve($schema->$propertyName, $sourceUri);
}
/**
* Look for the $ref property in the object. If found, remove the
* reference and augment this object with the contents of another
* schema.
*
* @param object $schema JSON Schema to flesh out
* @param string $sourceUri URI where this schema was located
*/
public function resolveRef($schema, $sourceUri)
{
$ref = '$ref';
if (empty($schema->$ref)) {
return;
}
$splitRef = explode('#', $schema->$ref, 2);
$refDoc = $splitRef[0];
$refPath = null;
if (count($splitRef) === 2) {
$refPath = explode('/', $splitRef[1]);
array_shift($refPath);
}
if (empty($refDoc) && empty($refPath)) {
// TODO: Not yet implemented - root pointer ref, causes recursion issues
return;
}
if (!empty($refDoc)) {
$refSchema = $this->fetchRef($refDoc, $sourceUri);
} else {
$refSchema = $this->rootSchema;
}
if (null !== $refPath) {
$refSchema = $this->resolveRefSegment($refSchema, $refPath);
}
unset($schema->$ref);
// Augment the current $schema object with properties fetched
foreach (get_object_vars($refSchema) as $prop => $value) {
$schema->$prop = $value;
}
}
/**
* Set URI Retriever for use with the Ref Resolver
*
* @param UriRetriever $retriever
* @return $this for chaining
*/
public function setUriRetriever(UriRetriever $retriever)
{
$this->uriRetriever = $retriever;
return $this;
}
protected function resolveRefSegment($data, $pathParts)
{
foreach ($pathParts as $path) {
$path = strtr($path, array('~1' => '/', '~0' => '~', '%25' => '%'));
if (is_array($data)) {
$data = $data[$path];
} else {
$data = $data->{$path};
}
}
return $data;
}
}
|