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