Commit 115227ea authored by Адлан Шамавов's avatar Адлан Шамавов
Browse files

Реализация мапперов

parent ec50dcdc
Loading
Loading
Loading
Loading

.env

deleted100644 → 0
+0 −3
Original line number Diff line number Diff line
APP_BASE_DIR=./

XDEBUG_IDE_KEY=openapi
 No newline at end of file
+46 −30
Original line number Diff line number Diff line
@@ -932,6 +932,9 @@ components:
  responses:
    SaveResp:
      description: "Успешный ответ"
      content:
        application/json:
          schema:
            type: object
            properties:
              status:
@@ -941,8 +944,12 @@ components:
              message:
                type: string
                description: "Cообщение"

    SuccessRegister:
      description: "Успешный ответ"
      content:
        application/json:
          schema:
            type: object
            properties:
              message:
@@ -951,6 +958,9 @@ components:
                example: "Успешная регистрация"
    NotFound:
      description: "Успешный ответ"
      content:
        application/json:
          schema:
            type: object
            properties:
              message:
@@ -958,6 +968,9 @@ components:
                example: "Элемент не найден"
    AlreadyExists:
      description: "Конфликт в запросе"
      content:
        application/json:
          schema:
            type: object
            properties:
              message:
@@ -965,6 +978,9 @@ components:
                example: "Пользователь с таким email/телефоном уже существует"
    ValidateErrorResp:
      description: "Ошибка валидации полей"
      content:
        application/json:
          schema:
            type: object
            properties:
              message:
+116 −3
Original line number Diff line number Diff line
@@ -5,8 +5,12 @@ declare(strict_types = 1);
namespace IQDev\OpenAPI\Mapper;

use IQDev\OpenAPI\Model\Components\ExternalDocumentation;
use IQDev\OpenAPI\Model\Components\MediaType;
use IQDev\OpenAPI\Model\Components\OAuthFlow;
use IQDev\OpenAPI\Model\Components\OAuthFlows;
use IQDev\OpenAPI\Model\Components\Parameter;
use IQDev\OpenAPI\Model\Components\RequestBody;
use IQDev\OpenAPI\Model\Components\Response;
use IQDev\OpenAPI\Model\Components\Schema;
use IQDev\OpenAPI\Model\Components\SecuritySchema;

@@ -17,8 +21,17 @@ class ComponentMapper
        $schemas = [];
        self::mapToSchema($components['schemas'], $schemas, $components['schemas']);
        $securitySchemes = self::mapToSecuritySchemas($components['securitySchemes']);
        $requestBodies = self::mapToRequestBody($components['requestBodies'], $schemas);
        $parameters = self::mapToParameter($components['parameters'], $schemas);
        $responses = self::mapToResponse($components['responses'], $schemas);

        return [];
        return [
            'schemas' => $schemas,
            'securitySchemes' => $securitySchemes,
            'requestBodies' => $requestBodies,
            'parameters' => $parameters,
            'responses' => $responses,
        ];
    }

    public static function mapToExternalDocumentation(array $externalDoc): ExternalDocumentation
@@ -27,7 +40,7 @@ class ComponentMapper
        ->setDescription($externalDoc['description'] ?? null);
    }

    public static function mapToSchema(array $yamlSchemas, array &$schemas, array &$globalYamlSchemas = null): ?Schema
    private static function mapToSchema(array $yamlSchemas, array &$schemas, array &$globalYamlSchemas = null): ?Schema
    {
        $schemaObj = new Schema();

@@ -121,7 +134,7 @@ class ComponentMapper
        return $schemaObj;
    }

    public static function mapToSecuritySchemas(array $securitySchemes): array
    private static function mapToSecuritySchemas(array $securitySchemes): array
    {
        return array_map(
            static function (array $securityScheme) {
@@ -175,4 +188,104 @@ class ComponentMapper
                ))->setRefreshUrl($oAuthFlows['authorizationCode']['refreshUrl'] ?? null) :
            null);
    }

    private static function mapToRequestBody(array $requestBodies, array $schemas): array
    {
        $result = [];
        array_walk(
            $requestBodies,
            static function (&$requestBody, $key) use (&$result, $schemas) {
                $content = [];

                array_walk(
                    $requestBody['content'],
                    static function (&$contentValue, $key) use (&$content, $schemas) {
                        $content[$key] = new MediaType();

                        if (isset($contentValue['schema'])) {
                            if (isset($contentValue['schema']['$ref'])) {
                                $path = explode('/', $contentValue['schema']['$ref']);
                                $schemaRefName = array_pop($path);
                                $content[$key]->setSchema($schemas[$schemaRefName]);
                            } else {
                                $content[$key]->setSchema(self::mapToSchema([$contentValue['schema']], $schemas));
                            }
                        }

                        $content[$key]->setExample($contentValue['example'] ?? null);
                        $content[$key]->setExamples($contentValue['examples'] ?? null);
                        $content[$key]->setEncoding($contentValue['encoding'] ?? null);
                    }
                );

                $result[$key] = (new RequestBody($content))
                ->setDescription($requestBody['description'] ?? null)
                ->setRequired($requestBody['required'] ?? false);
            }
        );

        return $result;
    }

    private static function mapToParameter(array $parameters, array $schemas): array
    {
        return array_map(
            static function (array $parameter) use ($schemas) {
                $param = (new Parameter(
                    $parameter['name'],
                    $parameter['in'],
                    $parameter['required']
                ))
                ->setDescription($parameter['description'] ?? null)
                ->setDeprecated($parameter['deprecated'] ?? false)
                ->setAllowEmptyValue($parameter['allowEmptyValue'] ?? false);

                if (isset($parameter['schema'])) {
                    if (isset($parameter['schema']['$ref'])) {
                        $path = explode('/', $parameter['schema']['$ref']);
                        $schemaRefName = array_pop($path);
                        $param->setSchema($schemas[$schemaRefName]);
                    } else {
                        $param->setSchema(self::mapToSchema([$parameter['schema']], $schemas));
                    }
                }

                return $param;
            }, $parameters
        );
    }

    private static function mapToResponse(array $responses, array $schemas): array
    {
        return array_map(
            static function (array $response) use ($schemas) {
                $result = new Response($response['description']);

                if (isset($response['content'])) {
                    $result->setContent(
                        array_map(
                            static function (array $contentValue) use ($schemas) {
                                if (isset($contentValue['schema'])) {
                                    if (isset($contentValue['schema']['$ref'])) {
                                        $path = explode('/', $contentValue['schema']['$ref']);
                                        $schemaRefName = array_pop($path);
                                        return $schemas[$schemaRefName];
                                    }

                                    return self::mapToSchema([$contentValue['schema']], $schemas);
                                }

                                return null;
                            }, $response['content']
                        )
                    );
                }

                $result->setHeaders($response['headers'] ?? null);
                $result->setLinks($response['links'] ?? null);

                return $result;
            }, $responses
        );
    }
}
+27 −7
Original line number Diff line number Diff line
@@ -4,26 +4,46 @@ declare(strict_types = 1);

namespace IQDev\OpenAPI\Mapper;

use IQDev\OpenAPI\Model\Paths\Operation;
use IQDev\OpenAPI\Model\Paths\PathItem;
use IQDev\OpenAPI\Model\Paths\Paths;

// TODO: Разобраться с $ref для pathItems

class PathMapper
{
    public static function map(array $paths): Paths
    public static function map(array $paths, array $schemas): Paths
    {
        return new Paths(
            array_map(
                static function ($pathItem) {
                    return self::mapToPathItem($pathItem);
                }, $paths
                static function (string $key, array $pathItem) use ($schemas) {
                    return self::mapToPathItem($pathItem, $key, $schemas);
                }, array_keys($paths), array_values($paths)
            )
        );
    }

    private static function mapToPathItem(array $pathItem): PathItem
    private static function mapToPathItem(array $pathItem, string $path, array $schemas): PathItem
    {
        return (new PathItem($path))
        ->setDescription($pathItem['description'] ?? null)
        ->setSummary($pathItem['summary'] ?? null)
        ->setServers($pathItem['servers'] ?? null)
        ->setOperations(self::mapToOperation($pathItem))
        ->setOperations($pathItem['parameters']);
    }

    private static function mapToOperation(array $operations): array
    {
        return array_map(
            static function (string $key, array $operation) {
                if (in_array($key, Operation::METHODS, true)) {
                    return (new Operation())
                    ->setDescription($operation['description'] ?? null)
                    ->setSummary($operation['summary'] ?? null)
                    ->setDeprecated($operation['deprecated'] ?? null)
                    ->setOperationId($operation['operationId'] ?? null)
                    ->setExternalDocs(ComponentMapper::mapToExternalDocumentation($operation['externalDocs']));
                }
            }, array_keys($operations), array_values($operations)
        );
    }
}
+64 −0
Original line number Diff line number Diff line
<?php

declare(strict_types = 1);

namespace IQDev\OpenAPI\Model\Components;

class MediaType
{
    private ?Schema $schema = null;

    private ?array $example = null;

    private ?array $examples = null;

    private ?array $encoding = null;

    public function getSchema(): ?Schema
    {
        return $this->schema;
    }

    public function setSchema(?Schema $schema): self
    {
        $this->schema = $schema;

        return $this;
    }

    public function getExample(): ?array
    {
        return $this->example;
    }

    public function setExample(?array $example): self
    {
        $this->example = $example;

        return $this;
    }

    public function getExamples(): ?array
    {
        return $this->examples;
    }

    public function setExamples(?array $examples): self
    {
        $this->examples = $examples;

        return $this;
    }

    public function getEncoding(): ?array
    {
        return $this->encoding;
    }

    public function setEncoding(?array $encoding): self
    {
        $this->encoding = $encoding;

        return $this;
    }
}
Loading