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 | * |
||
44 | * @return static|callable |
||
45 | */ |
||
46 | public static function fromDecodedObject(\stdClass $schema) |
||
50 | |||
51 | /** |
||
52 | * @param \SplFileObject $file |
||
53 | * |
||
54 | * @return static|callable |
||
55 | */ |
||
56 | public static function fromFile(\SplFileObject $file) |
||
64 | |||
65 | /** |
||
66 | * @param string $json |
||
67 | * |
||
68 | * @return static|callable |
||
69 | */ |
||
70 | public static function fromEncodedString($json) |
||
74 | |||
75 | /** |
||
76 | * @param array $json |
||
77 | * |
||
78 | * @return static|callable |
||
79 | */ |
||
80 | public static function fromArray(array $json) |
||
84 | |||
85 | /** |
||
86 | * Returns the request's JSON validation errors. |
||
87 | * |
||
88 | * @param ServerRequestInterface $request |
||
89 | * |
||
90 | * @return array|null |
||
91 | */ |
||
92 | public static function getErrors(ServerRequestInterface $request) |
||
96 | |||
97 | /** |
||
98 | * Execute the middleware. |
||
99 | * |
||
100 | * @param ServerRequestInterface $request |
||
101 | * @param ResponseInterface $response |
||
102 | * @param callable $next |
||
103 | * |
||
104 | * @return ResponseInterface |
||
105 | * |
||
106 | * @throws \RuntimeException |
||
107 | * @throws \InvalidArgumentException |
||
108 | * @throws \JsonSchema\Exception\ExceptionInterface |
||
109 | */ |
||
110 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
||
132 | |||
133 | /** |
||
134 | * @param ServerRequestInterface $request |
||
135 | * @param ResponseInterface $response |
||
136 | * |
||
137 | * @return ResponseInterface |
||
138 | * |
||
139 | * @throws \RuntimeException |
||
140 | * @throws \InvalidArgumentException |
||
141 | */ |
||
142 | private function defaultErrorHandler( |
||
159 | |||
160 | /** |
||
161 | * Has the following method signature: |
||
162 | * function (ServerRequestInterface $request, ResponseInterface $response): ResponseInterface {}. |
||
163 | * |
||
164 | * Validation errors are stored in a middleware attribute: |
||
165 | * $request->getAttribute(Middleware::KEY, [])[JsonValidator::KEY]; |
||
166 | * |
||
167 | * @param callable $errorHandler |
||
168 | */ |
||
169 | public function errorHandler(callable $errorHandler) |
||
173 | } |
||
174 |