1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BEAR\Resource\Interceptor; |
6
|
|
|
|
7
|
|
|
use BEAR\Resource\Annotation\JsonSchema; |
8
|
|
|
use BEAR\Resource\Code; |
9
|
|
|
use BEAR\Resource\Exception\JsonSchemaException; |
10
|
|
|
use BEAR\Resource\Exception\JsonSchemaKeytNotFoundException; |
11
|
|
|
use BEAR\Resource\Exception\JsonSchemaNotFoundException; |
12
|
|
|
use BEAR\Resource\JsonSchemaExceptionHandlerInterface; |
13
|
|
|
use BEAR\Resource\JsonSchemaRequestExceptionHandlerInterface; |
14
|
|
|
use BEAR\Resource\ResourceObject; |
15
|
|
|
use JsonSchema\Constraints\Constraint; |
16
|
|
|
use JsonSchema\Validator; |
17
|
|
|
use Ray\Aop\MethodInvocation; |
18
|
|
|
use Ray\Di\Di\Named; |
19
|
|
|
use ReflectionClass; |
20
|
|
|
use stdClass; |
21
|
|
|
|
22
|
|
|
use function assert; |
23
|
|
|
use function file_exists; |
24
|
|
|
use function is_array; |
25
|
|
|
use function is_dir; |
26
|
|
|
use function is_object; |
27
|
|
|
use function is_string; |
28
|
|
|
use function json_decode; |
29
|
|
|
use function json_encode; |
30
|
|
|
use function property_exists; |
31
|
|
|
use function sprintf; |
32
|
|
|
use function str_replace; |
33
|
|
|
|
34
|
|
|
use const JSON_THROW_ON_ERROR; |
35
|
|
|
|
36
|
|
|
final class JsonSchemaInterceptor implements JsonSchemaInterceptorInterface |
37
|
|
|
{ |
38
|
|
|
public function __construct( |
39
|
|
|
#[Named('json_schema_dir')] |
40
|
|
|
private readonly string $schemaDir, |
41
|
|
|
#[Named('json_validate_dir')] |
42
|
|
|
private readonly string $validateDir, |
43
|
|
|
private readonly JsonSchemaExceptionHandlerInterface $handler, |
44
|
|
|
private readonly JsonSchemaRequestExceptionHandlerInterface $requestHandler, |
45
|
|
|
#[Named('json_schema_host')] |
46
|
|
|
private readonly string|null $schemaHost = null, |
47
|
|
|
) { |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritDoc} |
52
|
|
|
*/ |
53
|
|
|
public function invoke(MethodInvocation $invocation): ResourceObject |
54
|
|
|
{ |
55
|
|
|
$method = $invocation->getMethod(); |
56
|
|
|
$jsonSchema = $method->getAnnotation(JsonSchema::class); |
57
|
|
|
assert($jsonSchema instanceof JsonSchema); |
58
|
|
|
if ($jsonSchema->params) { |
59
|
|
|
$arguments = $this->getNamedArguments($invocation); |
60
|
|
|
$this->validateRequest($invocation, $jsonSchema, $arguments); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$ro = $invocation->proceed(); |
64
|
|
|
assert($ro instanceof ResourceObject); |
65
|
|
|
if ($ro->code === 200 || $ro->code === 201) { |
66
|
|
|
$this->validateResponse($ro, $jsonSchema); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $ro; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** @param array<string, mixed> $arguments */ |
73
|
|
|
private function validateRequest(MethodInvocation $invocation, JsonSchema $jsonSchema, array $arguments): void // @phpstan-ignore-line |
74
|
|
|
{ |
75
|
|
|
try { |
76
|
|
|
$schemaFile = $this->validateDir . '/' . $jsonSchema->params; |
77
|
|
|
$this->validateFileExists($schemaFile); |
78
|
|
|
$this->validate($arguments, $schemaFile); |
79
|
|
|
} catch (JsonSchemaException $e) { |
80
|
|
|
$ro = $invocation->getThis(); |
81
|
|
|
assert($ro instanceof ResourceObject); |
82
|
|
|
/** @psalm-suppress PossiblyUndefinedVariable */ |
83
|
|
|
$this->requestHandler->handleRequestException($arguments, $ro, $e, $schemaFile); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function validateResponse(ResourceObject $ro, JsonSchema $jsonSchema): void |
88
|
|
|
{ |
89
|
|
|
$schemaFile = $this->getSchemaFile($jsonSchema, $ro); |
90
|
|
|
try { |
91
|
|
|
$this->validateRo($ro, $schemaFile, $jsonSchema); |
92
|
|
|
if (is_string($this->schemaHost)) { |
93
|
|
|
$ro->headers['Link'] = sprintf('<%s%s>; rel="describedby"', $this->schemaHost, $jsonSchema->schema); |
94
|
|
|
} |
95
|
|
|
} catch (JsonSchemaException $e) { |
96
|
|
|
$this->handler->handle($ro, $e, $schemaFile); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private function validateRo(ResourceObject $ro, string $schemaFile, JsonSchema $jsonSchema): void |
101
|
|
|
{ |
102
|
|
|
$viewJson = $jsonSchema->target === 'view' ? (string) $ro : json_encode($ro, JSON_THROW_ON_ERROR); |
103
|
|
|
/** @var array<stdClass>|false|stdClass $json */ |
104
|
|
|
$json = json_decode($viewJson, null, 512, JSON_THROW_ON_ERROR); |
105
|
|
|
/** @var array<stdClass>|stdClass $target */ |
106
|
|
|
$target = is_object($json) ? $this->getTarget($json, $jsonSchema) : $json; |
107
|
|
|
$this->validate($target, $schemaFile); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
private function getTarget(object $json, JsonSchema $jsonSchema): mixed |
111
|
|
|
{ |
112
|
|
|
if ($jsonSchema->key === '') { |
113
|
|
|
return $json; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if (! property_exists($json, $jsonSchema->key)) { |
117
|
|
|
throw new JsonSchemaKeytNotFoundException($jsonSchema->key); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $json->{$jsonSchema->key}; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** @param array<mixed>|stdClass $target */ |
124
|
|
|
private function validate(array|stdClass $target, string $schemaFile): void |
125
|
|
|
{ |
126
|
|
|
$validator = new Validator(); |
127
|
|
|
$schema = (object) ['$ref' => 'file://' . $schemaFile]; |
128
|
|
|
$scanArray = is_array($target) ? $target : $this->deepArray($target); |
|
|
|
|
129
|
|
|
$validator->validate($scanArray, $schema, Constraint::CHECK_MODE_TYPE_CAST); |
130
|
|
|
$isValid = $validator->isValid(); |
131
|
|
|
if ($isValid) { |
132
|
|
|
return; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
throw $this->throwJsonSchemaException($validator, $schemaFile); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** @return array<int|string, mixed> */ |
139
|
|
|
private function deepArray(object $values): array |
140
|
|
|
{ |
141
|
|
|
$result = []; |
142
|
|
|
/** @psalm-suppress MixedAssignment */ |
143
|
|
|
foreach ($values as $key => $value) { // @phpstan-ignore-line |
144
|
|
|
/** @psalm-suppress MixedArrayOffset */ |
145
|
|
|
$result[$key] = is_object($value) ? $this->deepArray($value) : $result[$key] = $value; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $result; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
private function throwJsonSchemaException(Validator $validator, string $schemaFile): JsonSchemaException |
152
|
|
|
{ |
153
|
|
|
/** @var array<array<string, string>> $errors */ |
154
|
|
|
$errors = $validator->getErrors(); |
155
|
|
|
$msg = ''; |
156
|
|
|
foreach ($errors as $error) { |
157
|
|
|
$msg .= sprintf('[%s] %s; ', $error['property'], $error['message']); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$msg .= "by {$schemaFile}"; |
161
|
|
|
|
162
|
|
|
return new JsonSchemaException($msg, Code::ERROR); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
private function getSchemaFile(JsonSchema $jsonSchema, ResourceObject $ro): string |
166
|
|
|
{ |
167
|
|
|
if (! $jsonSchema->schema) { |
168
|
|
|
// for BC only |
169
|
|
|
new ReflectionClass($ro); |
170
|
|
|
$roFileName = $this->getParentClassName($ro); |
171
|
|
|
$bcFile = str_replace('.php', '.json', $roFileName); |
172
|
|
|
if (file_exists($bcFile)) { |
173
|
|
|
return $bcFile; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$schemaFile = $this->schemaDir . '/' . $jsonSchema->schema; |
178
|
|
|
$this->validateFileExists($schemaFile); |
179
|
|
|
|
180
|
|
|
return $schemaFile; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
private function getParentClassName(ResourceObject $ro): string |
184
|
|
|
{ |
185
|
|
|
$parent = (new ReflectionClass($ro))->getParentClass(); |
186
|
|
|
|
187
|
|
|
return $parent instanceof ReflectionClass ? (string) $parent->getFileName() : ''; |
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
private function validateFileExists(string $schemaFile): void |
191
|
|
|
{ |
192
|
|
|
if (! file_exists($schemaFile) || is_dir($schemaFile)) { |
193
|
|
|
throw new JsonSchemaNotFoundException($schemaFile); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** @return array<string, mixed> */ |
198
|
|
|
private function getNamedArguments(MethodInvocation $invocation): array |
199
|
|
|
{ |
200
|
|
|
$parameters = $invocation->getMethod()->getParameters(); |
201
|
|
|
$values = $invocation->getArguments(); |
202
|
|
|
$arguments = []; |
203
|
|
|
foreach ($parameters as $index => $parameter) { |
204
|
|
|
/** @psalm-suppress MixedAssignment */ |
205
|
|
|
$arguments[$parameter->name] = $values[$index] ?? $parameter->getDefaultValue(); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return $arguments; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|