Skip to content

Commit 1296583

Browse files
Patrick Safarovbighappyface
Patrick Safarov
authored andcommitted
New way of handling references (#277)
1 parent ef13b9b commit 1296583

File tree

9 files changed

+252
-348
lines changed

9 files changed

+252
-348
lines changed

README.md

+2-8
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,11 @@ See [json-schema](http://json-schema.org/) for more details.
2626
```php
2727
<?php
2828

29-
// Get the schema and data as objects
30-
// If you use $ref or if you are unsure, resolve those references here
31-
// This modifies the $schema object
32-
$refResolver = new JsonSchema\RefResolver(new JsonSchema\Uri\UriRetriever(), new JsonSchema\Uri\UriResolver());
33-
$schema = $refResolver->resolve('file://' . realpath('schema.json'));
34-
3529
$data = json_decode(file_get_contents('data.json'));
3630

3731
// Validate
38-
$validator = new JsonSchema\Validator();
39-
$validator->check($data, $schema);
32+
$validator = new JsonSchema\Validator;
33+
$validator->check($data, (object)['$ref' => 'file://' . realpath('schema.json')]);
4034

4135
if ($validator->isValid()) {
4236
echo "The supplied JSON validates against the schema.\n";

src/JsonSchema/Constraints/Constraint.php

+34-28
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99

1010
namespace JsonSchema\Constraints;
1111

12+
use JsonSchema\SchemaStorage;
1213
use JsonSchema\Uri\UriRetriever;
13-
use JsonSchema\Validator;
14+
use JsonSchema\UriRetrieverInterface;
1415
use JsonSchema\Entity\JsonPointer;
1516

1617
/**
@@ -21,6 +22,7 @@
2122
*/
2223
abstract 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
*

src/JsonSchema/Constraints/Factory.php

+32-15
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,20 @@
1010
namespace JsonSchema\Constraints;
1111

1212
use JsonSchema\Exception\InvalidArgumentException;
13+
use JsonSchema\SchemaStorage;
1314
use JsonSchema\Uri\UriRetriever;
15+
use JsonSchema\UriRetrieverInterface;
1416

1517
/**
1618
* Factory for centralize constraint initialization.
1719
*/
1820
class Factory
1921
{
22+
/**
23+
* @var SchemaStorage
24+
*/
25+
protected $schemaStorage;
26+
2027
/**
2128
* @var UriRetriever $uriRetriever
2229
*/
@@ -50,34 +57,39 @@ class Factory
5057
);
5158

5259
/**
53-
* @param UriRetriever $uriRetriever
60+
* @param SchemaStorage $schemaStorage
61+
* @param UriRetrieverInterface $uriRetriever
62+
* @param int $checkMode
5463
*/
55-
public function __construct(UriRetriever $uriRetriever = null, $checkMode = Constraint::CHECK_MODE_NORMAL)
56-
{
57-
if (!$uriRetriever) {
58-
$uriRetriever = new UriRetriever();
59-
}
60-
61-
$this->uriRetriever = $uriRetriever;
64+
public function __construct(
65+
SchemaStorage $schemaStorage = null,
66+
UriRetrieverInterface $uriRetriever = null,
67+
$checkMode = Constraint::CHECK_MODE_NORMAL
68+
) {
69+
$this->uriRetriever = $uriRetriever ?: new UriRetriever;
70+
$this->schemaStorage = $schemaStorage ?: new SchemaStorage($this->uriRetriever);
6271
$this->checkMode = $checkMode;
6372
}
6473

6574
/**
66-
* @return UriRetriever
75+
* @return UriRetrieverInterface
6776
*/
6877
public function getUriRetriever()
6978
{
7079
return $this->uriRetriever;
7180
}
81+
82+
public function getSchemaStorage()
83+
{
84+
return $this->schemaStorage;
85+
}
7286

7387
public function getTypeCheck()
7488
{
7589
if (!isset($this->typeCheck[$this->checkMode])) {
76-
if ($this->checkMode === Constraint::CHECK_MODE_TYPE_CAST) {
77-
$this->typeCheck[Constraint::CHECK_MODE_TYPE_CAST] = new TypeCheck\LooseTypeCheck();
78-
} else {
79-
$this->typeCheck[$this->checkMode] = new TypeCheck\StrictTypeCheck();
80-
}
90+
$this->typeCheck[$this->checkMode] = $this->checkMode === Constraint::CHECK_MODE_TYPE_CAST
91+
? new TypeCheck\LooseTypeCheck
92+
: new TypeCheck\StrictTypeCheck;
8193
}
8294

8395
return $this->typeCheck[$this->checkMode];
@@ -112,7 +124,12 @@ public function setConstraintClass($name, $class)
112124
public function createInstanceFor($constraintName)
113125
{
114126
if (array_key_exists($constraintName, $this->constraintMap)) {
115-
return new $this->constraintMap[$constraintName]($this->checkMode, $this->uriRetriever, $this);
127+
return new $this->constraintMap[$constraintName](
128+
$this->checkMode,
129+
$this->schemaStorage,
130+
$this->uriRetriever,
131+
$this
132+
);
116133
}
117134
throw new InvalidArgumentException('Unknown constraint ' . $constraintName);
118135
}

src/JsonSchema/Constraints/ObjectConstraint.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function validateElement($element, $matches, $objectDefinition = null, Js
127127
public function validateDefinition($element, $objectDefinition = null, JsonPointer $path = null)
128128
{
129129
foreach ($objectDefinition as $i => $value) {
130-
$property = $this->getProperty($element, $i, new UndefinedConstraint());
130+
$property = $this->getProperty($element, $i, $this->getFactory()->createInstanceFor('undefined'));
131131
$definition = $this->getProperty($objectDefinition, $i);
132132
$this->checkUndefined($property, $definition, $path, $i);
133133
}

src/JsonSchema/Constraints/TypeConstraint.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ protected function validateTypesArray($value, array $type, &$validTypesWording,
8181
// with a new type constraint
8282
if (is_object($tp)) {
8383
if (!$isValid) {
84-
$validator = new static($this->checkMode);
84+
$validator = $this->getFactory()->createInstanceFor('type');
8585
$subSchema = new \stdClass();
8686
$subSchema->type = $tp;
8787
$validator->check($value, $subSchema, $path, null);

0 commit comments

Comments
 (0)