Skip to content

Commit ae08d20

Browse files
committed
Fix RefResolver and make it compatible with draft-04 jsonrainbow#240
1 parent 94d8d29 commit ae08d20

File tree

11 files changed

+488
-586
lines changed

11 files changed

+488
-586
lines changed

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,12 @@ See [json-schema](http://json-schema.org/) for more details.
2727
<?php
2828

2929
// Get the schema and data as objects
30-
$retriever = new JsonSchema\Uri\UriRetriever;
31-
$schema = $retriever->retrieve('file://' . realpath('schema.json'));
32-
$data = json_decode(file_get_contents('data.json'));
33-
3430
// If you use $ref or if you are unsure, resolve those references here
3531
// This modifies the $schema object
36-
$refResolver = new JsonSchema\RefResolver($retriever);
37-
$refResolver->resolve($schema, 'file://' . __DIR__);
32+
$refResolver = new JsonSchema\RefResolver(new JsonSchema\Uri\UriRetriever(), new JsonSchema\Uri\UriResolver());
33+
$schema = $refResolver->resolve('file://' . realpath('schema.json'));
34+
35+
$data = json_decode(file_get_contents('data.json'));
3836

3937
// Validate
4038
$validator = new JsonSchema\Validator();

bin/validate-json

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -200,22 +200,14 @@ try {
200200
echo $urlSchema . "\n";
201201
exit();
202202
}
203-
204-
$schema = $retriever->retrieve($urlSchema);
205-
if ($schema === null) {
206-
echo "Error loading JSON schema file\n";
207-
echo $urlSchema . "\n";
208-
showJsonError();
209-
exit(2);
210-
}
211203
} catch (Exception $e) {
212204
echo "Error loading JSON schema file\n";
213205
echo $urlSchema . "\n";
214206
echo $e->getMessage() . "\n";
215207
exit(2);
216208
}
217-
$refResolver = new JsonSchema\RefResolver($retriever);
218-
$refResolver->resolve($schema, $urlSchema);
209+
$refResolver = new JsonSchema\RefResolver($retriever, $resolver);
210+
$schema = $refResolver->resolve($urlSchema);
219211

220212
if (isset($arOptions['--dump-schema'])) {
221213
$options = defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0;
@@ -242,4 +234,3 @@ try {
242234
echo "Error code: " . $e->getCode() . "\n";
243235
exit(24);
244236
}
245-
?>

src/JsonSchema/Entity/JsonPointer.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the JsonSchema package.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace JsonSchema\Entity;
11+
12+
/**
13+
* @package JsonSchema\Entity
14+
* @author Joost Nijhuis <[email protected]>
15+
*/
16+
class JsonPointer
17+
{
18+
/** @var string */
19+
private $filename;
20+
21+
/** @var string[] */
22+
private $propertyPaths = array();
23+
24+
/**
25+
* @param string $value
26+
* @throws \InvalidArgumentException when $value is not a string
27+
*/
28+
public function __construct($value)
29+
{
30+
if (!is_string($value)) {
31+
throw new \InvalidArgumentException('Ref value must be a string');
32+
}
33+
34+
$splitRef = explode('#', $value, 2);
35+
$this->filename = $splitRef[0];
36+
if (array_key_exists(1, $splitRef)) {
37+
$this->propertyPaths = $this->decodePropertyPaths($splitRef[1]);
38+
}
39+
}
40+
41+
/**
42+
* @param string $propertyPathString
43+
* @return string[]
44+
*/
45+
private function decodePropertyPaths($propertyPathString)
46+
{
47+
$paths = array();
48+
foreach (explode('/', trim($propertyPathString, '/')) as $path) {
49+
$path = $this->decodePath($path);
50+
if ($path) {
51+
$paths[] = $path;
52+
}
53+
}
54+
55+
return $paths;
56+
}
57+
58+
/**
59+
* @return array
60+
*/
61+
private function encodePropertyPaths()
62+
{
63+
return array_map(
64+
array($this, 'encodePath'),
65+
$this->getPropertyPaths()
66+
);
67+
}
68+
69+
/**
70+
* @param string $path
71+
* @return string
72+
*/
73+
private function decodePath($path)
74+
{
75+
return strtr($path, array('~1' => '/', '~0' => '~', '%25' => '%'));
76+
}
77+
78+
/**
79+
* @param string $path
80+
* @return string
81+
*/
82+
private function encodePath($path)
83+
{
84+
return strtr($path, array('/' => '~1', '~' => '~0', '%' => '%25'));
85+
}
86+
87+
/**
88+
* @return string
89+
*/
90+
public function getFilename()
91+
{
92+
return $this->filename;
93+
}
94+
95+
/**
96+
* @return string[]
97+
*/
98+
public function getPropertyPaths()
99+
{
100+
return $this->propertyPaths;
101+
}
102+
103+
/**
104+
* @return string
105+
*/
106+
public function getPropertyPathAsString()
107+
{
108+
return rtrim('#/' . implode('/', $this->encodePropertyPaths()), '/');
109+
}
110+
111+
/**
112+
* @return string
113+
*/
114+
public function __toString()
115+
{
116+
return $this->getFilename() . $this->getPropertyPathAsString();
117+
}
118+
}

0 commit comments

Comments
 (0)