1 | <?php |
||
35 | class JsonValidator |
||
36 | { |
||
37 | /** |
||
38 | * The schema used for validating schemas. |
||
39 | * |
||
40 | * @var object|null |
||
41 | */ |
||
42 | private $metaSchema; |
||
43 | |||
44 | /** |
||
45 | * Validator instance used for validation. |
||
46 | * |
||
47 | * @var Validator |
||
48 | */ |
||
49 | private $validator; |
||
50 | |||
51 | /** |
||
52 | * URI retriever for fetching JSON schemas. |
||
53 | * |
||
54 | * @var UriRetrieverInterface |
||
55 | */ |
||
56 | private $uriRetriever; |
||
57 | |||
58 | /** |
||
59 | * URI resolver for JSON schemas. |
||
60 | * |
||
61 | * @var UriResolverInterface |
||
62 | */ |
||
63 | private $uriResolver; |
||
64 | |||
65 | /** |
||
66 | * JsonValidator constructor. |
||
67 | * |
||
68 | * @param Validator|null $validator JsonSchema\Validator |
||
69 | * instance to use |
||
70 | * @param UriRetriever|null $uriRetriever The retriever for fetching |
||
71 | * JSON schemas |
||
72 | * @param UriResolverInterface|null $uriResolver The resolver for URIs |
||
73 | */ |
||
74 | 111 | public function __construct(Validator $validator = null, UriRetriever $uriRetriever = null, UriResolverInterface $uriResolver = null) |
|
75 | { |
||
76 | 111 | $this->validator = $validator ?: new Validator(); |
|
77 | 111 | $this->uriRetriever = $uriRetriever ?: new UriRetriever(); |
|
|
|||
78 | 111 | $this->uriResolver = $uriResolver ?: new UriResolver(); |
|
79 | 111 | } |
|
80 | |||
81 | /** |
||
82 | * Validates JSON data against a schema. |
||
83 | * |
||
84 | * The schema may be passed as file path or as object returned from |
||
85 | * `json_decode($schemaFile)`. |
||
86 | * |
||
87 | * @param mixed $data The decoded JSON data |
||
88 | * @param string|object|null $schema The schema file or object. If `null`, |
||
89 | * the validator will look for a `$schema` |
||
90 | * property |
||
91 | * |
||
92 | * @return string[] The errors found during validation. Returns an empty |
||
93 | * array if no errors were found |
||
94 | * |
||
95 | * @throws InvalidSchemaException If the schema is invalid |
||
96 | */ |
||
97 | 31 | public function validate($data, $schema = null) |
|
98 | { |
||
99 | 31 | if (null === $schema && isset($data->{'$schema'})) { |
|
100 | 1 | $schema = $data->{'$schema'}; |
|
101 | } |
||
102 | |||
103 | 31 | if (is_string($schema)) { |
|
104 | 18 | $schema = $this->loadSchema($schema); |
|
105 | 29 | } elseif (is_object($schema)) { |
|
106 | 26 | $this->assertSchemaValid($schema); |
|
107 | } else { |
||
108 | 3 | throw new InvalidSchemaException(sprintf( |
|
109 | 'The schema must be given as string, object or in the "$schema" '. |
||
110 | 3 | 'property of the JSON data. Got: %s', |
|
111 | 3 | is_object($schema) ? get_class($schema) : gettype($schema) |
|
112 | )); |
||
113 | } |
||
114 | |||
115 | 26 | $this->validator->reset(); |
|
116 | |||
117 | try { |
||
118 | 26 | $this->validator->check($data, $schema); |
|
119 | 1 | } catch (InvalidArgumentException $e) { |
|
120 | 1 | throw new InvalidSchemaException(sprintf( |
|
121 | 1 | 'The schema is invalid: %s', |
|
122 | 1 | $e->getMessage() |
|
123 | 1 | ), 0, $e); |
|
124 | } |
||
125 | |||
126 | 26 | $errors = array(); |
|
127 | |||
128 | 26 | if (!$this->validator->isValid()) { |
|
129 | 15 | $errors = (array) $this->validator->getErrors(); |
|
130 | |||
131 | 15 | foreach ($errors as $key => $error) { |
|
132 | 15 | $prefix = $error['property'] ? $error['property'].': ' : ''; |
|
133 | 15 | $errors[$key] = $prefix.$error['message']; |
|
134 | } |
||
135 | } |
||
136 | |||
137 | 26 | return $errors; |
|
138 | } |
||
139 | |||
140 | 26 | private function assertSchemaValid($schema) |
|
161 | |||
162 | 18 | private function loadSchema($file) |
|
163 | { |
||
196 | } |
||
197 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..