1 | <?php |
||
12 | class JsonValidator |
||
13 | { |
||
14 | const KEY = 'JSON_VALIDATION_ERRORS'; |
||
15 | |||
16 | use AttributeTrait; |
||
17 | use CallableTrait; |
||
18 | |||
19 | /** @var \stdClass */ |
||
20 | private $schema; |
||
21 | |||
22 | /** @var callable */ |
||
23 | private $errorHandler; |
||
24 | |||
25 | /** |
||
26 | * JsonSchema constructor. |
||
27 | * Consider using one of the following factories instead of invoking the controller directly: |
||
28 | * - JsonValidator::fromFile() |
||
29 | * - JsonValidator::fromEncodedString() |
||
30 | * - JsonValidator::fromDecodedObject() |
||
31 | * - JsonValidator::fromArray() |
||
32 | * |
||
33 | * @param \stdClass $schema A JSON-decoded object-representation of the schema. |
||
34 | */ |
||
35 | public function __construct(\stdClass $schema) |
||
40 | |||
41 | /** |
||
42 | * @param \stdClass $schema |
||
43 | * @return static|callable |
||
44 | */ |
||
45 | public static function fromDecodedObject(\stdClass $schema) |
||
49 | |||
50 | /** |
||
51 | * @param \SplFileObject $file |
||
52 | * @return static|callable |
||
53 | */ |
||
54 | public static function fromFile(\SplFileObject $file) |
||
62 | |||
63 | /** |
||
64 | * @param string $json |
||
65 | * @return static|callable |
||
66 | */ |
||
67 | public static function fromEncodedString($json) |
||
71 | |||
72 | /** |
||
73 | * @param array $json |
||
74 | * @return static|callable |
||
75 | */ |
||
76 | public static function fromArray(array $json) |
||
80 | |||
81 | /** |
||
82 | * Returns the request's JSON validation errors. |
||
83 | * |
||
84 | * @param ServerRequestInterface $request |
||
85 | * |
||
86 | * @return array|null |
||
87 | */ |
||
88 | public static function getErrors(ServerRequestInterface $request) |
||
92 | |||
93 | /** |
||
94 | * Execute the middleware. |
||
95 | * |
||
96 | * @param ServerRequestInterface $request |
||
97 | * @param ResponseInterface $response |
||
98 | * @param callable $next |
||
99 | * |
||
100 | * @return ResponseInterface |
||
101 | * @throws \RuntimeException |
||
102 | * @throws \InvalidArgumentException |
||
103 | * @throws \JsonSchema\Exception\ExceptionInterface |
||
104 | */ |
||
105 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
||
127 | |||
128 | /** |
||
129 | * @param ServerRequestInterface $request |
||
130 | * @param ResponseInterface $response |
||
131 | * @return ResponseInterface |
||
132 | * @throws \RuntimeException |
||
133 | * @throws \InvalidArgumentException |
||
134 | */ |
||
135 | private function defaultErrorHandler( |
||
152 | |||
153 | /** |
||
154 | * Has the following method signature: |
||
155 | * function (ServerRequestInterface $request, ResponseInterface $response): ResponseInterface {} |
||
156 | * |
||
157 | * Validation errors are stored in a middleware attribute: |
||
158 | * $request->getAttribute(Middleware::KEY, [])[JsonValidator::KEY]; |
||
159 | * |
||
160 | * @param callable $errorHandler |
||
161 | * @return void |
||
162 | */ |
||
163 | public function errorHandler(callable $errorHandler) |
||
167 | } |
||
168 |