/
NegotiationValidation.php
69 lines (59 loc) · 2.01 KB
/
NegotiationValidation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php declare(strict_types = 1);
namespace Apitte\Core\Schema\Validation;
use Apitte\Core\Exception\Logical\InvalidSchemaException;
use Apitte\Core\Schema\SchemaBuilder;
use ReflectionClass;
class NegotiationValidation implements IValidation
{
public function validate(SchemaBuilder $builder): void
{
foreach ($builder->getControllers() as $controller) {
foreach ($controller->getMethods() as $method) {
$haveDefault = null;
$takenSuffixes = [];
foreach ($method->getNegotiations() as $negotiation) {
if ($negotiation->isDefault() === true) {
if ($haveDefault !== null) {
throw new InvalidSchemaException(sprintf(
'Multiple negotiations with "default=true" given in "%s::%s()". Only one negotiation could be default.',
$controller->getClass(),
$method->getName()
));
}
$haveDefault = $negotiation;
}
if (!isset($takenSuffixes[$negotiation->getSuffix()])) {
$takenSuffixes[$negotiation->getSuffix()] = $negotiation;
} else {
throw new InvalidSchemaException(sprintf(
'Multiple negotiations with "suffix=%s" given in "%s::%s()". Each negotiation must have unique suffix',
$negotiation->getSuffix(),
$controller->getClass(),
$method->getName()
));
}
$renderer = $negotiation->getRenderer();
if ($renderer !== null) {
if (!class_exists($renderer)) {
throw new InvalidSchemaException(sprintf(
'Negotiation renderer "%s" in "%s::%s()" does not exists',
$renderer,
$controller->getClass(),
$method->getName()
));
}
$reflection = new ReflectionClass($renderer);
if (!$reflection->hasMethod('__invoke')) {
throw new InvalidSchemaException(sprintf(
'Negotiation renderer "%s" in "%s::%s()" does not implement __invoke(ApiRequest $request, ApiResponse $response, array $context): ApiResponse',
$renderer,
$controller->getClass(),
$method->getName()
));
}
}
}
}
}
}
}