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

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

parent 4f3db3ce
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -10,4 +10,6 @@ $reader = new YamlReader();
$yaml = $reader->read('file.yaml');

$resolver = new OpenAPIResolver();
$resolver->resolve($yaml);
 No newline at end of file
$result = $resolver->resolve($yaml);

var_dump($result);
 No newline at end of file
+2 −2
Original line number Diff line number Diff line
@@ -227,7 +227,7 @@ class ComponentMapper
        return $result;
    }

    private static function mapToParameter(array $parameters, array $schemas): array
    public static function mapToParameter(array $parameters, array $schemas): array
    {
        return array_map(
            static function (array $parameter) use ($schemas) {
@@ -255,7 +255,7 @@ class ComponentMapper
        );
    }

    private static function mapToResponse(array $responses, array $schemas): array
    public static function mapToResponse(array $responses, array $schemas): array
    {
        return array_map(
            static function (array $response) use ($schemas) {
+77 −11
Original line number Diff line number Diff line
@@ -7,41 +7,107 @@ namespace IQDev\OpenAPI\Mapper;
use IQDev\OpenAPI\Model\Paths\Operation;
use IQDev\OpenAPI\Model\Paths\PathItem;
use IQDev\OpenAPI\Model\Paths\Paths;
use IQDev\OpenAPI\Model\Paths\SecurityRequirement;

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

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

    private static function mapToOperation(array $operations): array
    private static function mapToOperation(array $operations, array $components): array
    {
        return array_map(
            static function (string $key, array $operation) {
            static function (string $key, array $operation) use ($components) {
                if (in_array($key, Operation::METHODS, true)) {
                    return (new Operation())
                    $op = (new Operation())
                    ->setDescription($operation['description'] ?? null)
                    ->setSummary($operation['summary'] ?? null)
                    ->setDeprecated($operation['deprecated'] ?? null)
                    ->setDeprecated($operation['deprecated'] ?? false)
                    ->setOperationId($operation['operationId'] ?? null)
                    ->setExternalDocs(ComponentMapper::mapToExternalDocumentation($operation['externalDocs']));
                    ->setExternalDocs(
                        array_map(
                            static function (array $externalDoc) {
                                return ComponentMapper::mapToExternalDocumentation($externalDoc);
                            }, $operation['externalDocs'] ?? []
                        )
                    )
                    ->setTags($operation['tags'] ?? null)
                    ->setServers($operation['servers'] ?? null)
                    ->setSecurity(
                    isset($operation['security']) ?
                        array_map(
                            static function (array $security) {
                                return new SecurityRequirement($security);
                            }, $operation['security']
                        ) :
                        null
                    );

                    $parameters = [];
                    if (isset($operation['parameters'])) {
                        foreach ($operation['parameters'] as $parameter) {
                            if (key($parameter) === '$ref') {
                                $path = explode('/', $parameter['$ref']);
                                $schemaRefName = array_pop($path);
                                $parameters[] = $components['parameters'][$schemaRefName];
                            } else {
                                $parameters[] = ComponentMapper::mapToParameter([$parameter], $components['schemas']);
                            }
                        }
                    }
                    $op->setParameters($parameters);

                    if (isset($operation['responses'])) {
                        $op->setResponses(
                            array_map(
                                static function (string $key, array $response) use ($components) {
                                    $result = [];

                                    if (isset($response['content'])) {
                                        $result[$key] = array_map(
                                            static function (array $content) use ($components) {
                                                if (isset($content['schema']['$ref'])) {
                                                    $path = explode('/', $content['schema']['$ref']);
                                                    $schemaRefName = array_pop($path);
                                                    $tableName = array_pop($path);
                                                    return $components[$tableName][$schemaRefName];
                                                }
                                            }, $response['content']
                                        );
                                    }

                                    return $result;
                                }, array_keys($operation['responses']), array_values($operation['responses'])
                            )
                        );
                    }

                    if (isset($operation['requestBodies'])) {
                        if (isset($operation['requestBodies']['$ref'])) {
                            $path = explode('/', $operation['requestBodies']['$ref']);
                            $schemaRefName = array_pop($path);
                            $op->setRequestBody($components['requestBodies'][$schemaRefName]);
                        }
                    }

                    return $op;
                }
            }, array_keys($operations), array_values($operations)
        );
+4 −3
Original line number Diff line number Diff line
@@ -37,7 +37,8 @@ class Operation

    private bool $deprecated = false;

    private ?SecurityRequirement $security = null;
    /** @var SecurityRequirement[] */
    private ?array $security = null;

    /** @var Server[] */
    private ?array $servers = null;
@@ -150,12 +151,12 @@ class Operation
        return $this;
    }

    public function getSecurity(): ?SecurityRequirement
    public function getSecurity(): ?array
    {
        return $this->security;
    }

    public function setSecurity(?SecurityRequirement $security): self
    public function setSecurity(?array $security): self
    {
        $this->security = $security;

+6 −4
Original line number Diff line number Diff line
@@ -19,10 +19,12 @@ class OpenAPIResolver
        $servers = ServerMapper::map($yaml['servers']);
        $tags = TagMapper::map($yaml['tags']);
        $components = ComponentMapper::map($yaml['components']);
        $paths = PathMapper::map($yaml['paths'], $components['schemas']);
        $paths = PathMapper::map($yaml['paths'], $components);


        # var_dump($components);
        # var_dump($yaml);
        return (new OpenAPI($info))
        ->setTags($tags)
        ->setServers($servers)
        ->setComponents($components)
        ->setPaths($paths);
    }
}
 No newline at end of file