|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ApiClients\Tools\OpenApiClientGenerator\Gatherer; |
| 4 | + |
| 5 | +use ApiClients\Tools\OpenApiClientGenerator\Representation\Hydrator; |
| 6 | +use ApiClients\Tools\OpenApiClientGenerator\Utils; |
| 7 | +use ApiClients\Tools\OpenApiClientGenerator\Registry\Schema as SchemaRegistry; |
| 8 | +use ApiClients\Tools\OpenApiClientGenerator\Representation\OperationRequestBody; |
| 9 | +use ApiClients\Tools\OpenApiClientGenerator\Representation\OperationResponse; |
| 10 | +use ApiClients\Tools\OpenApiClientGenerator\Representation\Parameter; |
| 11 | +use cebe\openapi\spec\Operation as openAPIOperation; |
| 12 | +use cebe\openapi\spec\PathItem; |
| 13 | +use Jawira\CaseConverter\Convert; |
| 14 | +use Psr\Http\Message\ResponseInterface; |
| 15 | + |
| 16 | +final class Operation |
| 17 | +{ |
| 18 | + public static function gather( |
| 19 | + string $className, |
| 20 | + string $method, |
| 21 | + string $path, |
| 22 | + openAPIOperation $operation, |
| 23 | + SchemaRegistry $schemaRegistry, |
| 24 | + ): \ApiClients\Tools\OpenApiClientGenerator\Representation\Operation { |
| 25 | + $returnType = []; |
| 26 | + $parameters = []; |
| 27 | + $hasPerPageParameter = false; |
| 28 | + $hasPageParameter = false; |
| 29 | + foreach ($operation->parameters as $parameter) { |
| 30 | + if ($parameter->name === 'per_page') { |
| 31 | + $hasPerPageParameter = true; |
| 32 | + } |
| 33 | + if ($parameter->name === 'page') { |
| 34 | + $hasPageParameter = true; |
| 35 | + } |
| 36 | + $parameterType = str_replace([ |
| 37 | + 'integer', |
| 38 | + 'any', |
| 39 | + 'boolean', |
| 40 | + ], [ |
| 41 | + 'int', |
| 42 | + 'mixed', |
| 43 | + 'bool', |
| 44 | + ], implode('|', is_array($parameter->schema->type) ? $parameter->schema->type : [$parameter->schema->type])); |
| 45 | + |
| 46 | + $parameters[] = new Parameter( |
| 47 | + $parameter->name, |
| 48 | + (string)$parameter->description, |
| 49 | + $parameterType, |
| 50 | + $parameter->in, |
| 51 | + $parameter->schema->default, |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + $classNameSanitized = str_replace('/', '\\', Utils::className($className)); |
| 56 | + $requestBody = []; |
| 57 | + if ($operation->requestBody !== null) { |
| 58 | + foreach ($operation->requestBody->content as $contentType => $requestBodyDetails) { |
| 59 | + $requestBodyClassname = $schemaRegistry->get($requestBodyDetails->schema, $classNameSanitized . '\\Request\\' . Utils::className(str_replace('/', '', $contentType))); |
| 60 | + $requestBody[] = new OperationRequestBody( |
| 61 | + $contentType, |
| 62 | + Schema::gather($requestBodyClassname, $requestBodyDetails->schema, $schemaRegistry), |
| 63 | + ); |
| 64 | + } |
| 65 | + } |
| 66 | + $response = []; |
| 67 | + foreach ($operation->responses as $code => $spec) { |
| 68 | + foreach ($spec->content as $contentType => $contentTypeMediaType) { |
| 69 | + $responseClassname = $schemaRegistry->get($contentTypeMediaType->schema, 'Operation\\' . $classNameSanitized . '\\Response\\' . Utils::className(str_replace('/', '', $contentType) . '\\H' . $code)); |
| 70 | + $response[] = new OperationResponse( |
| 71 | + $code, |
| 72 | + $contentType, |
| 73 | + $spec->description, |
| 74 | + Schema::gather($responseClassname, $contentTypeMediaType->schema, $schemaRegistry), |
| 75 | + ); |
| 76 | + $returnType[] = $responseClassname; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if (count($returnType) === 0) { |
| 81 | + $returnType[] = '\\' . ResponseInterface::class; |
| 82 | + } |
| 83 | + |
| 84 | + return new \ApiClients\Tools\OpenApiClientGenerator\Representation\Operation( |
| 85 | + Utils::fixKeyword($className), |
| 86 | + $classNameSanitized, |
| 87 | + lcfirst(trim(Utils::basename($className),'\\')), |
| 88 | + trim(Utils::dirname($className),'\\'), |
| 89 | + $operation->operationId, |
| 90 | + $method, |
| 91 | + $path, |
| 92 | + $hasPageParameter === true && $hasPerPageParameter === true, // This is very GitHub specific!!! |
| 93 | + array_unique($returnType), |
| 94 | + [ |
| 95 | + ...array_filter($parameters, static fn (Parameter $parameter): bool => $parameter->default === null), |
| 96 | + ...array_filter($parameters, static fn (Parameter $parameter): bool => $parameter->default !== null), |
| 97 | + ], |
| 98 | + $requestBody, |
| 99 | + $response, |
| 100 | + ); |
| 101 | + } |
| 102 | +} |
0 commit comments