|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Validator class file |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Graviton\JsonSchemaBundle\Validator; |
|
7
|
|
|
|
|
8
|
|
|
use HadesArchitect\JsonSchemaBundle\Error\Error; |
|
9
|
|
|
use HadesArchitect\JsonSchemaBundle\Validator\ValidatorServiceInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* JSON definition validation |
|
13
|
|
|
* |
|
14
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
|
15
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
16
|
|
|
* @link http://swisscom.ch |
|
17
|
|
|
*/ |
|
18
|
|
|
class Validator implements ValidatorInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var \stdClass JSON schema |
|
22
|
|
|
*/ |
|
23
|
|
|
private $schema; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var ValidatorServiceInterface Validator |
|
26
|
|
|
*/ |
|
27
|
|
|
private $validator; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Constructor |
|
31
|
|
|
* |
|
32
|
|
|
* @param ValidatorServiceInterface $validator Validator |
|
33
|
|
|
* @param \stdClass $schema JSON schema |
|
34
|
|
|
*/ |
|
35
|
4 |
|
public function __construct(ValidatorServiceInterface $validator, \stdClass $schema) |
|
36
|
|
|
{ |
|
37
|
4 |
|
$this->validator = $validator; |
|
38
|
4 |
|
$this->schema = $schema; |
|
39
|
4 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Validate raw JSON definition |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $json JSON definition |
|
45
|
|
|
* @return Error[] |
|
46
|
|
|
* @throws InvalidJsonException If JSON is not valid |
|
47
|
|
|
*/ |
|
48
|
4 |
|
public function validateJsonDefinition($json) |
|
49
|
|
|
{ |
|
50
|
4 |
|
$json = json_decode($json); |
|
51
|
4 |
|
if (json_last_error() !== JSON_ERROR_NONE) { |
|
52
|
1 |
|
throw new InvalidJsonException(sprintf('Malformed JSON: %s', $this->getJsonLastErrorMessage())); |
|
53
|
|
|
} |
|
54
|
3 |
|
if (!is_object($json)) { |
|
55
|
1 |
|
throw new InvalidJsonException('JSON value must be an object'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
2 |
|
if ($this->validator->isValid($json, $this->schema)) { |
|
59
|
1 |
|
return []; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
return $this->validator->getErrors(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get JSON decode last error message |
|
67
|
|
|
* |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
1 |
|
private function getJsonLastErrorMessage() |
|
71
|
|
|
{ |
|
72
|
1 |
|
if (function_exists('json_last_error_msg')) { |
|
73
|
1 |
|
return json_last_error_msg(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$errorNo = json_last_error(); |
|
77
|
|
|
$errorMap = [ |
|
78
|
|
|
JSON_ERROR_DEPTH => 'Maximum stack depth exceeded', |
|
79
|
|
|
JSON_ERROR_STATE_MISMATCH => 'Underflow or modes mismatch', |
|
80
|
|
|
JSON_ERROR_CTRL_CHAR => 'Unexpected control character found', |
|
81
|
|
|
JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON', |
|
82
|
|
|
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded', |
|
83
|
|
|
]; |
|
84
|
|
|
return isset($errorMap[$errorNo]) ? $errorMap[$errorNo]: 'Unknown error'; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|