|
5 | 5 | use JsonSchema\Constraints\BaseConstraint; |
6 | 6 | use JsonSchema\Entity\JsonPointer; |
7 | 7 | use JsonSchema\Exception\UnresolvableJsonPointerException; |
8 | | -use JsonSchema\Iterator\ObjectIterator; |
9 | 8 | use JsonSchema\Uri\UriResolver; |
10 | 9 | use JsonSchema\Uri\UriRetriever; |
11 | 10 |
|
@@ -69,14 +68,42 @@ public function addSchema($id, $schema = null) |
69 | 68 | } |
70 | 69 | } |
71 | 70 |
|
72 | | - $objectIterator = new ObjectIterator($schema); |
73 | | - foreach ($objectIterator as $toResolveSchema) { |
74 | | - if (property_exists($toResolveSchema, '$ref') && is_string($toResolveSchema->{'$ref'})) { |
75 | | - $jsonPointer = new JsonPointer($this->uriResolver->resolve($toResolveSchema->{'$ref'}, $id)); |
76 | | - $toResolveSchema->{'$ref'} = (string) $jsonPointer; |
| 71 | + // resolve references |
| 72 | + $this->expandRefs($schema, $id); |
| 73 | + |
| 74 | + $this->schemas[$id] = $schema; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Recursively resolve all references against the provided base |
| 79 | + * |
| 80 | + * @param mixed $schema |
| 81 | + * @param string $base |
| 82 | + */ |
| 83 | + private function expandRefs(&$schema, $base = null) |
| 84 | + { |
| 85 | + if (!is_object($schema)) { |
| 86 | + if (is_array($schema)) { |
| 87 | + foreach ($schema as &$member) { |
| 88 | + $this->expandRefs($member, $base); |
| 89 | + } |
77 | 90 | } |
| 91 | + |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + if (property_exists($schema, 'id') && is_string($schema->id)) { |
| 96 | + $base = $this->uriResolver->resolve($schema->id, $base); |
| 97 | + } |
| 98 | + |
| 99 | + if (property_exists($schema, '$ref') && is_string($schema->{'$ref'})) { |
| 100 | + $refPointer = new JsonPointer($this->uriResolver->resolve($schema->{'$ref'}, $base)); |
| 101 | + $schema->{'$ref'} = (string) $refPointer; |
| 102 | + } |
| 103 | + |
| 104 | + foreach ($schema as &$member) { |
| 105 | + $this->expandRefs($member, $base); |
78 | 106 | } |
79 | | - $this->schemas[$id] = $schema; |
80 | 107 | } |
81 | 108 |
|
82 | 109 | /** |
|
0 commit comments