1 | <?php |
||
2 | |||
3 | /** |
||
4 | * It's free open-source software released under the MIT License. |
||
5 | * |
||
6 | * @author Anatoly Nekhay <[email protected]> |
||
7 | * @copyright Copyright (c) 2018, Anatoly Nekhay |
||
8 | * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE |
||
9 | * @link https://github.com/sunrise-php/http-router |
||
10 | */ |
||
11 | |||
12 | declare(strict_types=1); |
||
13 | |||
14 | namespace Sunrise\Http\Router\OpenApi\OperationEnricher; |
||
15 | |||
16 | use Fig\Http\Message\StatusCodeInterface; |
||
17 | use ReflectionClass; |
||
18 | use ReflectionMethod; |
||
19 | use Sunrise\Http\Router\Annotation\EncodableResponse; |
||
20 | use Sunrise\Http\Router\OpenApi\OpenApiConfiguration; |
||
21 | use Sunrise\Http\Router\OpenApi\OpenApiConfigurationAwareInterface; |
||
22 | use Sunrise\Http\Router\OpenApi\OpenApiOperationEnricherInterface; |
||
23 | use Sunrise\Http\Router\OpenApi\OpenApiPhpTypeSchemaResolverManagerAwareInterface; |
||
24 | use Sunrise\Http\Router\OpenApi\OpenApiPhpTypeSchemaResolverManagerInterface; |
||
25 | use Sunrise\Http\Router\OpenApi\TypeFactory; |
||
26 | use Sunrise\Http\Router\RouteInterface; |
||
27 | |||
28 | /** |
||
29 | * @since 3.0.0 |
||
30 | */ |
||
31 | final class EncodableResponseOperationEnricher extends AbstractResponseOperationEnricher implements |
||
32 | OpenApiOperationEnricherInterface, |
||
33 | OpenApiConfigurationAwareInterface, |
||
34 | OpenApiPhpTypeSchemaResolverManagerAwareInterface |
||
35 | { |
||
36 | private readonly OpenApiConfiguration $openApiConfiguration; |
||
37 | private readonly OpenApiPhpTypeSchemaResolverManagerInterface $openApiPhpTypeSchemaResolverManager; |
||
38 | |||
39 | 5 | public function setOpenApiConfiguration(OpenApiConfiguration $openApiConfiguration): void |
|
40 | { |
||
41 | 5 | $this->openApiConfiguration = $openApiConfiguration; |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
42 | } |
||
43 | |||
44 | 5 | public function setOpenApiPhpTypeSchemaResolverManager( |
|
45 | OpenApiPhpTypeSchemaResolverManagerInterface $openApiPhpTypeSchemaResolverManager, |
||
46 | ): void { |
||
47 | 5 | $this->openApiPhpTypeSchemaResolverManager = $openApiPhpTypeSchemaResolverManager; |
|
0 ignored issues
–
show
|
|||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @inheritDoc |
||
52 | */ |
||
53 | 3 | public function enrichOperation( |
|
54 | RouteInterface $route, |
||
55 | ReflectionClass|ReflectionMethod $requestHandler, |
||
56 | array &$operation, |
||
57 | ): void { |
||
58 | 3 | if (! $requestHandler instanceof ReflectionMethod) { |
|
59 | 3 | return; |
|
60 | } |
||
61 | |||
62 | 3 | if ($requestHandler->getAttributes(EncodableResponse::class) === []) { |
|
63 | 3 | return; |
|
64 | } |
||
65 | |||
66 | 3 | $responseStatusCode = self::getResponseStatusCode($requestHandler) ?? StatusCodeInterface::STATUS_OK; |
|
67 | |||
68 | 3 | $operation['responses'][$responseStatusCode] = [ |
|
69 | 3 | 'description' => $this->openApiConfiguration->defaultResponseDescription, |
|
70 | 3 | ]; |
|
71 | |||
72 | 3 | self::enrichResponseWithHeaders($requestHandler, $operation['responses'][$responseStatusCode]); |
|
73 | |||
74 | 3 | $responseBodyType = TypeFactory::fromPhpTypeReflection($requestHandler->getReturnType()); |
|
75 | 3 | $responseBodySchema = $this->openApiPhpTypeSchemaResolverManager |
|
76 | 3 | ->resolvePhpTypeSchema($responseBodyType, $requestHandler); |
|
77 | |||
78 | 3 | foreach ($route->getProducedMediaTypes() as $producedMediaType) { |
|
79 | 3 | $operation['responses'][$responseStatusCode]['content'][$producedMediaType->getIdentifier()] = [ |
|
80 | 3 | 'schema' => $responseBodySchema, |
|
81 | 3 | ]; |
|
82 | } |
||
83 | } |
||
84 | |||
85 | 3 | public function getWeight(): int |
|
86 | { |
||
87 | 3 | return 10; |
|
88 | } |
||
89 | } |
||
90 |