| 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; |
| 11 |
|
| 12 |
use JsonSchema\Constraints\BaseConstraint; |
| 13 |
use JsonSchema\Constraints\Constraint; |
| 14 |
|
| 15 |
/** |
| 16 |
* A JsonSchema Constraint |
| 17 |
* |
| 18 |
* @author Robert Schönthal <[email protected]> |
| 19 |
* @author Bruno Prieto Reis <[email protected]> |
| 20 |
* |
| 21 |
* @see README.md |
| 22 |
*/ |
| 23 |
class Validator extends BaseConstraint |
| 24 |
{ |
| 25 |
const SCHEMA_MEDIA_TYPE = 'application/schema+json'; |
| 26 |
|
| 27 |
const ERROR_NONE = 0x00000000; |
| 28 |
const ERROR_ALL = 0xFFFFFFFF; |
| 29 |
const ERROR_DOCUMENT_VALIDATION = 0x00000001; |
| 30 |
const ERROR_SCHEMA_VALIDATION = 0x00000002; |
| 31 |
|
| 32 |
/** |
| 33 |
* Validates the given data against the schema and returns an object containing the results |
| 34 |
* Both the php object and the schema are supposed to be a result of a json_decode call. |
| 35 |
* The validation works as defined by the schema proposal in http://json-schema.org. |
| 36 |
* |
| 37 |
* Note that the first argument is passed by reference, so you must pass in a variable. |
| 38 |
*/ |
| 39 |
public function validate(&$value, $schema = null, $checkMode = null) |
| 40 |
{ |
| 41 |
// make sure $schema is an object |
| 42 |
if (is_array($schema)) { |
| 43 |
$schema = self::arrayToObjectRecursive($schema); |
| 44 |
} |
| 45 |
|
| 46 |
// set checkMode |
| 47 |
$initialCheckMode = $this->factory->getConfig(); |
| 48 |
if ($checkMode !== null) { |
| 49 |
$this->factory->setConfig($checkMode); |
| 50 |
} |
| 51 |
|
| 52 |
// add provided schema to SchemaStorage with internal URI to allow internal $ref resolution |
| 53 |
if (is_object($schema) && property_exists($schema, 'id')) { |
| 54 |
$schemaURI = $schema->id; |
| 55 |
} else { |
| 56 |
$schemaURI = SchemaStorage::INTERNAL_PROVIDED_SCHEMA_URI; |
| 57 |
} |
| 58 |
$this->factory->getSchemaStorage()->addSchema($schemaURI, $schema); |
| 59 |
|
| 60 |
$validator = $this->factory->createInstanceFor('schema'); |
| 61 |
$validator->check( |
| 62 |
$value, |
| 63 |
$this->factory->getSchemaStorage()->getSchema($schemaURI) |
| 64 |
); |
| 65 |
|
| 66 |
$this->factory->setConfig($initialCheckMode); |
| 67 |
|
| 68 |
$this->addErrors(array_unique($validator->getErrors(), SORT_REGULAR)); |
| 69 |
|
| 70 |
return $validator->getErrorMask(); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Alias to validate(), to maintain backwards-compatibility with the previous API |
| 75 |
*/ |
| 76 |
public function check($value, $schema) |
| 77 |
{ |
| 78 |
return $this->validate($value, $schema); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Alias to validate(), to maintain backwards-compatibility with the previous API |
| 83 |
*/ |
| 84 |
public function coerce(&$value, $schema) |
| 85 |
{ |
| 86 |
return $this->validate($value, $schema, Constraint::CHECK_MODE_COERCE_TYPES); |
| 87 |
} |
| 88 |
} |