Issues (125)

AbstractResponseOperationEnricher.php (1 issue)

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 ReflectionAttribute;
17
use ReflectionClass;
18
use ReflectionMethod;
19
use Sunrise\Http\Router\Annotation\ResponseHeader;
20
use Sunrise\Http\Router\Annotation\ResponseStatus;
21
use Sunrise\Http\Router\OpenApi\Type;
22
23
/**
24
 * @since 3.0.0
25
 */
26
abstract class AbstractResponseOperationEnricher
27
{
28
    /**
29
     * @param ReflectionClass<object>|ReflectionMethod $requestHandler
30
     */
31 3
    final protected static function getResponseStatusCode(
32
        ReflectionClass|ReflectionMethod $requestHandler,
33
    ): ?int {
34 3
        if ($requestHandler instanceof ReflectionMethod) {
35
            /** @var list<ReflectionAttribute<ResponseStatus>> $annotations */
36 3
            $annotations = $requestHandler->getAttributes(ResponseStatus::class);
37 3
            if (isset($annotations[0])) {
38 3
                $responseStatus = $annotations[0]->newInstance();
39
40 3
                return $responseStatus->code;
41
            }
42
        }
43
44 3
        return null;
45
    }
46
47
    /**
48
     * @param ReflectionClass<object>|ReflectionMethod $requestHandler
49
     * @param array<array-key, mixed> $response
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, mixed> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, mixed>.
Loading history...
50
     * @param-out array<array-key, mixed> $response
51
     */
52 3
    final protected static function enrichResponseWithHeaders(
53
        ReflectionClass|ReflectionMethod $requestHandler,
54
        array &$response,
55
    ): void {
56 3
        if ($requestHandler instanceof ReflectionMethod) {
57
            /** @var list<ReflectionAttribute<ResponseHeader>> $annotations */
58 3
            $annotations = $requestHandler->getAttributes(ResponseHeader::class);
59 3
            foreach ($annotations as $annotation) {
60 3
                $responseHeader = $annotation->newInstance();
61
62 3
                $response['headers'][$responseHeader->name] = [
63 3
                    'schema' => [
64 3
                        'type' => Type::OAS_TYPE_NAME_STRING,
65 3
                        'example' => $responseHeader->value,
66 3
                    ],
67 3
                ];
68
            }
69
        }
70
    }
71
}
72