Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into feature/some-opti…
Browse files Browse the repository at this point in the history
…mization

# Conflicts:
#	src/JsonSchema/Constraints/Factory.php
  • Loading branch information
narcoticfresh committed Aug 19, 2016
2 parents 2f590d5 + 1296583 commit 4a0849c
Show file tree
Hide file tree
Showing 25 changed files with 654 additions and 509 deletions.
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,11 @@ See [json-schema](http://json-schema.org/) for more details.
```php
<?php

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

$data = json_decode(file_get_contents('data.json'));

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

if ($validator->isValid()) {
echo "The supplied JSON validates against the schema.\n";
Expand Down
14 changes: 8 additions & 6 deletions src/JsonSchema/Constraints/CollectionConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace JsonSchema\Constraints;

use JsonSchema\Entity\JsonPointer;

/**
* The CollectionConstraint Constraints, validates an array against a given schema
*
Expand All @@ -20,7 +22,7 @@ class CollectionConstraint extends Constraint
/**
* {@inheritDoc}
*/
public function check($value, $schema = null, $path = null, $i = null)
public function check($value, $schema = null, JsonPointer $path = null, $i = null)
{
// Verify minItems
if (isset($schema->minItems) && count($value) < $schema->minItems) {
Expand Down Expand Up @@ -52,12 +54,12 @@ public function check($value, $schema = null, $path = null, $i = null)
/**
* Validates the items
*
* @param array $value
* @param \stdClass $schema
* @param string $path
* @param string $i
* @param array $value
* @param \stdClass $schema
* @param JsonPointer|null $path
* @param string $i
*/
protected function validateItems($value, $schema = null, $path = null, $i = null)
protected function validateItems($value, $schema = null, JsonPointer $path = null, $i = null)
{
if (is_object($schema->items)) {
// just one type definition for the whole array
Expand Down
192 changes: 109 additions & 83 deletions src/JsonSchema/Constraints/Constraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

namespace JsonSchema\Constraints;

use JsonSchema\SchemaStorage;
use JsonSchema\Uri\UriRetriever;
use JsonSchema\Validator;
use JsonSchema\UriRetrieverInterface;
use JsonSchema\Entity\JsonPointer;

/**
* The Base Constraints, all Validators should extend this class
Expand All @@ -20,6 +22,7 @@
*/
abstract class Constraint implements ConstraintInterface
{
protected $schemaStorage;
protected $checkMode = self::CHECK_MODE_NORMAL;
protected $uriRetriever;
protected $errors = array();
Expand All @@ -34,19 +37,25 @@ abstract class Constraint implements ConstraintInterface
private $factory;

/**
* @param int $checkMode
* @param UriRetriever $uriRetriever
* @param Factory $factory
* @param int $checkMode
* @param SchemaStorage $schemaStorage
* @param UriRetrieverInterface $uriRetriever
* @param Factory $factory
*/
public function __construct($checkMode = self::CHECK_MODE_NORMAL, UriRetriever $uriRetriever = null, Factory $factory = null)
{
$this->checkMode = $checkMode;
$this->uriRetriever = $uriRetriever;
$this->factory = $factory;
public function __construct(
$checkMode = self::CHECK_MODE_NORMAL,
SchemaStorage $schemaStorage = null,
UriRetrieverInterface $uriRetriever = null,
Factory $factory = null
) {
$this->checkMode = $checkMode;
$this->uriRetriever = $uriRetriever;
$this->factory = $factory;
$this->schemaStorage = $schemaStorage;
}

/**
* @return UriRetriever $uriRetriever
* @return UriRetrieverInterface $uriRetriever
*/
public function getUriRetriever()
{
Expand All @@ -63,27 +72,40 @@ public function getUriRetriever()
public function getFactory()
{
if (!$this->factory) {
$this->factory = new Factory($this->getUriRetriever(), $this->checkMode);
$this->factory = new Factory($this->getSchemaStorage(), $this->getUriRetriever(), $this->checkMode);
}

return $this->factory;
}

/**
* @param UriRetriever $uriRetriever
* @return SchemaStorage
*/
public function setUriRetriever(UriRetriever $uriRetriever)
public function getSchemaStorage()
{
if (is_null($this->schemaStorage)) {
$this->schemaStorage = new SchemaStorage($this->getUriRetriever());
}

return $this->schemaStorage;
}

/**
* @param UriRetrieverInterface $uriRetriever
*/
public function setUriRetriever(UriRetrieverInterface $uriRetriever)
{
$this->uriRetriever = $uriRetriever;
}

/**
* {@inheritDoc}
*/
public function addError($path, $message, $constraint='', array $more=null)
public function addError(JsonPointer $path = null, $message, $constraint='', array $more=null)
{
$error = array(
'property' => $path,
'property' => $this->convertJsonPointerIntoPropertyPath($path ?: new JsonPointer('')),
'pointer' => ltrim(strval($path ?: new JsonPointer('')), '#'),
'message' => $message,
'constraint' => $constraint,
);
Expand Down Expand Up @@ -132,37 +154,32 @@ public function reset()
/**
* Bubble down the path
*
* @param string $path Current path
* @param mixed $i What to append to the path
* @param JsonPointer|null $path Current path
* @param mixed $i What to append to the path
*
* @return string
* @return JsonPointer;
*/
protected function incrementPath($path, $i)
protected function incrementPath(JsonPointer $path = null, $i)
{
if ($path !== '') {
if (is_int($i)) {
$path .= '[' . $i . ']';
} elseif ($i == '') {
$path .= '';
} else {
$path .= '.' . $i;
}
} else {
$path = $i;
}

$path = $path ?: new JsonPointer('');
$path = $path->withPropertyPaths(
array_merge(
$path->getPropertyPaths(),
array_filter(array($i), 'strlen')
)
);
return $path;
}

/**
* Validates an array
*
* @param mixed $value
* @param mixed $schema
* @param mixed $path
* @param mixed $i
* @param mixed $value
* @param mixed $schema
* @param JsonPointer|null $path
* @param mixed $i
*/
protected function checkArray($value, $schema = null, $path = null, $i = null)
protected function checkArray($value, $schema = null, JsonPointer $path = null, $i = null)
{
$validator = $this->getFactory()->createInstanceFor('collection');
$validator->check($value, $schema, $path, $i);
Expand All @@ -173,13 +190,13 @@ protected function checkArray($value, $schema = null, $path = null, $i = null)
/**
* Validates an object
*
* @param mixed $value
* @param mixed $schema
* @param mixed $path
* @param mixed $i
* @param mixed $patternProperties
* @param mixed $value
* @param mixed $schema
* @param JsonPointer|null $path
* @param mixed $i
* @param mixed $patternProperties
*/
protected function checkObject($value, $schema = null, $path = null, $i = null, $patternProperties = null)
protected function checkObject($value, $schema = null, JsonPointer $path = null, $i = null, $patternProperties = null)
{
$validator = $this->getFactory()->createInstanceFor('object');
$validator->check($value, $schema, $path, $i, $patternProperties);
Expand All @@ -190,12 +207,12 @@ protected function checkObject($value, $schema = null, $path = null, $i = null,
/**
* Validates the type of a property
*
* @param mixed $value
* @param mixed $schema
* @param mixed $path
* @param mixed $i
* @param mixed $value
* @param mixed $schema
* @param JsonPointer|null $path
* @param mixed $i
*/
protected function checkType($value, $schema = null, $path = null, $i = null)
protected function checkType($value, $schema = null, JsonPointer $path = null, $i = null)
{
$validator = $this->getFactory()->createInstanceFor('type');
$validator->check($value, $schema, $path, $i);
Expand All @@ -206,28 +223,28 @@ protected function checkType($value, $schema = null, $path = null, $i = null)
/**
* Checks a undefined element
*
* @param mixed $value
* @param mixed $schema
* @param mixed $path
* @param mixed $i
* @param mixed $value
* @param mixed $schema
* @param JsonPointer|null $path
* @param mixed $i
*/
protected function checkUndefined($value, $schema = null, $path = null, $i = null)
protected function checkUndefined($value, $schema = null, JsonPointer $path = null, $i = null)
{
$validator = $this->getFactory()->createInstanceFor('undefined');
$validator->check($value, $schema, $path, $i);
$validator->check($value, $this->schemaStorage->resolveRefSchema($schema), $path, $i);

$this->addErrors($validator->getErrors());
}

/**
* Checks a string element
*
* @param mixed $value
* @param mixed $schema
* @param mixed $path
* @param mixed $i
* @param mixed $value
* @param mixed $schema
* @param JsonPointer|null $path
* @param mixed $i
*/
protected function checkString($value, $schema = null, $path = null, $i = null)
protected function checkString($value, $schema = null, JsonPointer $path = null, $i = null)
{
$validator = $this->getFactory()->createInstanceFor('string');
$validator->check($value, $schema, $path, $i);
Expand All @@ -238,12 +255,12 @@ protected function checkString($value, $schema = null, $path = null, $i = null)
/**
* Checks a number element
*
* @param mixed $value
* @param mixed $schema
* @param mixed $path
* @param mixed $i
* @param mixed $value
* @param mixed $schema
* @param JsonPointer $path
* @param mixed $i
*/
protected function checkNumber($value, $schema = null, $path = null, $i = null)
protected function checkNumber($value, $schema = null, JsonPointer $path = null, $i = null)
{
$validator = $this->getFactory()->createInstanceFor('number');
$validator->check($value, $schema, $path, $i);
Expand All @@ -254,41 +271,35 @@ protected function checkNumber($value, $schema = null, $path = null, $i = null)
/**
* Checks a enum element
*
* @param mixed $value
* @param mixed $schema
* @param mixed $path
* @param mixed $i
* @param mixed $value
* @param mixed $schema
* @param JsonPointer|null $path
* @param mixed $i
*/
protected function checkEnum($value, $schema = null, $path = null, $i = null)
protected function checkEnum($value, $schema = null, JsonPointer $path = null, $i = null)
{
$validator = $this->getFactory()->createInstanceFor('enum');
$validator->check($value, $schema, $path, $i);

$this->addErrors($validator->getErrors());
}

protected function checkFormat($value, $schema = null, $path = null, $i = null)
/**
* Checks format of an element
*
* @param mixed $value
* @param mixed $schema
* @param JsonPointer|null $path
* @param mixed $i
*/
protected function checkFormat($value, $schema = null, JsonPointer $path = null, $i = null)
{
$validator = $this->getFactory()->createInstanceFor('format');
$validator->check($value, $schema, $path, $i);

$this->addErrors($validator->getErrors());
}

/**
* @param string $uri JSON Schema URI
* @return string JSON Schema contents
*/
protected function retrieveUri($uri)
{
if (null === $this->uriRetriever) {
$this->setUriRetriever(new UriRetriever);
}
$jsonSchema = $this->uriRetriever->retrieve($uri);
// TODO validate using schema
return $jsonSchema;
}

/**
* Get the type check based on the set check mode.
*
Expand All @@ -298,4 +309,19 @@ protected function getTypeCheck()
{
return $this->getFactory()->getTypeCheck();
}

/**
* @param JsonPointer $pointer
* @return string property path
*/
protected function convertJsonPointerIntoPropertyPath(JsonPointer $pointer)
{
$result = array_map(
function($path) {
return sprintf(is_numeric($path) ? '[%d]' : '.%s', $path);
},
$pointer->getPropertyPaths()
);
return trim(implode('', $result), '.');
}
}
Loading

0 comments on commit 4a0849c

Please sign in to comment.