src/Swagger/SwaggerDecorator.php line 24

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Swagger;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  6. final class SwaggerDecorator implements NormalizerInterface
  7. {
  8.     private $decorated;
  9.     public function __construct(NormalizerInterface $decorated)
  10.     {
  11.         $this->decorated $decorated;
  12.     }
  13.     public function supportsNormalization($datastring $format null): bool
  14.     {
  15.         return $this->decorated->supportsNormalization($data$format);
  16.     }
  17.     public function normalize($objectstring $format null, array $context = [])
  18.     {
  19.         $docs $this->decorated->normalize($object$format$context);
  20.         $docs['components']['schemas']['Token'] = [
  21.             'type' => 'object',
  22.             'properties' => [
  23.                 'token' => [
  24.                     'type' => 'string',
  25.                     'readOnly' => true,
  26.                 ],
  27.             ],
  28.         ];
  29.         $docs['components']['schemas']['Credentials'] = [
  30.             'type' => 'object',
  31.             'properties' => [
  32.                 'username' => [
  33.                     'type' => 'string',
  34.                     'example' => 'api',
  35.                 ],
  36.                 'password' => [
  37.                     'type' => 'string',
  38.                     'example' => 'api',
  39.                 ],
  40.             ],
  41.         ];
  42.         $tokenDocumentation = [
  43.             'paths' => [
  44.                 '/authentication_token' => [
  45.                     'post' => [
  46.                         'tags' => ['Token'],
  47.                         'operationId' => 'postCredentialsItem',
  48.                         'summary' => 'Get JWT token to login.',
  49.                         'requestBody' => [
  50.                             'description' => 'Create new JWT Token',
  51.                             'content' => [
  52.                                 'application/json' => [
  53.                                     'schema' => [
  54.                                         '$ref' => '#/components/schemas/Credentials',
  55.                                     ],
  56.                                 ],
  57.                             ],
  58.                         ],
  59.                         'responses' => [
  60.                             Response::HTTP_OK => [
  61.                                 'description' => 'Get JWT token',
  62.                                 'content' => [
  63.                                     'application/json' => [
  64.                                         'schema' => [
  65.                                             '$ref' => '#/components/schemas/Token',
  66.                                         ],
  67.                                     ],
  68.                                 ],
  69.                             ],
  70.                         ],
  71.                     ],
  72.                 ],
  73.             ],
  74.         ];
  75.         return array_merge_recursive($docs$tokenDocumentation);
  76.     }
  77. }