|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the OpenapiBundle package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Niels Nijens <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Nijens\OpenapiBundle\Deserialization\ArgumentResolver; |
|
15
|
|
|
|
|
16
|
|
|
use Nijens\OpenapiBundle\Deserialization\Attribute\DeserializedObject; |
|
17
|
|
|
use Nijens\OpenapiBundle\Deserialization\DeserializationContext; |
|
18
|
|
|
use Nijens\OpenapiBundle\Routing\RouteContext; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
20
|
|
|
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface; |
|
21
|
|
|
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; |
|
22
|
|
|
|
|
23
|
|
|
class DeserializedObjectArgumentResolver implements ArgumentValueResolverInterface |
|
24
|
|
|
{ |
|
25
|
|
|
public function supports(Request $request, ArgumentMetadata $argument): bool |
|
26
|
|
|
{ |
|
27
|
|
|
if ($request->attributes->has(DeserializationContext::REQUEST_ATTRIBUTE) === false) { |
|
28
|
|
|
return false; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$routeContext = $request->attributes->get(RouteContext::REQUEST_ATTRIBUTE); |
|
32
|
|
|
if ($this->isDeserializationObjectType($argument, $routeContext)) { |
|
33
|
|
|
return true; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
if ($this->hasDeserializedObjectAttribute($argument)) { |
|
37
|
|
|
return true; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if ($this->isDeserializationObjectArgumentName($argument, $routeContext)) { |
|
41
|
|
|
return true; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return false; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function resolve(Request $request, ArgumentMetadata $argument): iterable |
|
48
|
|
|
{ |
|
49
|
|
|
yield $request->attributes->get(DeserializationContext::REQUEST_ATTRIBUTE); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
private function isDeserializationObjectType(ArgumentMetadata $argument, ?array $routeContext): bool |
|
53
|
|
|
{ |
|
54
|
|
|
if (isset($routeContext[RouteContext::DESERIALIZATION_OBJECT]) === false) { |
|
55
|
|
|
return false; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $routeContext[RouteContext::DESERIALIZATION_OBJECT] === $argument->getType(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
private function hasDeserializedObjectAttribute(ArgumentMetadata $argument): bool |
|
62
|
|
|
{ |
|
63
|
|
|
if (method_exists($argument, 'getAttributes')) { |
|
64
|
|
|
return count($argument->getAttributes(DeserializedObject::class, ArgumentMetadata::IS_INSTANCEOF)) > 0; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return false; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
private function isDeserializationObjectArgumentName(ArgumentMetadata $argument, ?array $routeContext): bool |
|
71
|
|
|
{ |
|
72
|
|
|
if (isset($routeContext[RouteContext::DESERIALIZATION_OBJECT_ARGUMENT_NAME]) === false) { |
|
73
|
|
|
return false; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $routeContext[RouteContext::DESERIALIZATION_OBJECT_ARGUMENT_NAME] === $argument->getName(); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|