From 13c2ff3734a2dc3e36ec7cdf03a654e4d522286e Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Tue, 31 Jan 2023 00:06:45 +0100 Subject: [PATCH] Add ClientInterface --- src/Generator.php | 6 ++ src/Generator/Client.php | 2 +- src/Generator/ClientInterface.php | 97 +++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 src/Generator/ClientInterface.php diff --git a/src/Generator.php b/src/Generator.php index 36d378a..336f012 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -4,6 +4,7 @@ use ApiClients\Tools\OpenApiClientGenerator\Generator\Authentication; use ApiClients\Tools\OpenApiClientGenerator\Generator\Client; +use ApiClients\Tools\OpenApiClientGenerator\Generator\ClientInterface; use ApiClients\Tools\OpenApiClientGenerator\Generator\Clients; use ApiClients\Tools\OpenApiClientGenerator\Generator\Operation; use ApiClients\Tools\OpenApiClientGenerator\Generator\Path; @@ -146,6 +147,11 @@ private function all(string $namespace, string $rootPath): iterable ); } + yield from ClientInterface::generate( + $namespace, + $clients, + $schemaRegistry, + ); yield from Client::generate( $namespace, $clients, diff --git a/src/Generator/Client.php b/src/Generator/Client.php index 055e89c..5b142e4 100644 --- a/src/Generator/Client.php +++ b/src/Generator/Client.php @@ -34,7 +34,7 @@ public static function generate(string $namespace, array $clients, SchemaRegistr $factory = new BuilderFactory(); $stmt = $factory->namespace(rtrim($namespace, '\\')); - $class = $factory->class('Client')->makeFinal()->addStmt( + $class = $factory->class('Client')->implement(new Node\Name('ClientInterface'))->makeFinal()->addStmt( $factory->property('authentication')->setType('\\' . AuthenticationInterface::class)->makeReadonly()->makePrivate() )->addStmt( $factory->property('browser')->setType('\\' . Browser::class)->makeReadonly()->makePrivate() diff --git a/src/Generator/ClientInterface.php b/src/Generator/ClientInterface.php new file mode 100644 index 0000000..3528576 --- /dev/null +++ b/src/Generator/ClientInterface.php @@ -0,0 +1,97 @@ + + * @throws \Jawira\CaseConverter\CaseConverterException + */ + public static function generate(string $namespace, array $clients, SchemaRegistry $schemaRegistry): iterable + { + $factory = new BuilderFactory(); + $stmt = $factory->namespace(rtrim($namespace, '\\')); + + $class = $factory->interface('ClientInterface'); + $operationCalls = []; + $callReturnTypes = []; + + foreach ($clients as $operationGroup => $operations) { + $cn = str_replace('/', '\\', '\\' . $namespace . 'Operation/' . $operationGroup); + $casedOperationgroup = lcfirst($operationGroup); + foreach ($operations as $operationOperation => $operationDetails) { + $returnType = []; + foreach ($operationDetails['operation']->responses as $code => $spec) { + $contentTypeCases = []; + foreach ($spec->content as $contentType => $contentTypeSchema) { + $fallbackName = 'Operation\\' . $operationGroup . '\\Response\\' . (new Convert(str_replace('/', '\\', $contentType) . '\\H' . $code ))->toPascal(); + $object = '\\' . $namespace . 'Schema\\' . $schemaRegistry->get($contentTypeSchema->schema, $fallbackName); + $callReturnTypes[] = ($contentTypeSchema->schema->type === 'array' ? '\\' . Observable::class . '<' : '') . $object . ($contentTypeSchema->schema->type === 'array' ? '>' : ''); + $contentTypeCases[] = $returnType[] = $contentTypeSchema->schema->type === 'array' ? '\\' . Observable::class : $object; + } + if (count($contentTypeCases) === 0) { + $returnType[] = $callReturnTypes[] = 'int'; + } + } + $operationCalls[] = [ + 'operationGroupMethod' => $casedOperationgroup, + 'operationMethod' => lcfirst($operationOperation), + 'className' => str_replace('/', '\\', '\\' . $namespace . 'Operation\\' . $operationDetails['class']), + 'params' => iterator_to_array((function (array $operationDetails): iterable { + foreach ($operationDetails['operation']->parameters as $parameter) { + yield $parameter->name; + } + })($operationDetails)), + 'returnType' => $returnType, + ]; + } + $class->addStmt( + $factory->method($casedOperationgroup)->setReturnType($cn)->makePublic() + ); + } + + $class->addStmt( + $factory->method('call')->makePublic()->setReturnType( + new Node\Name('\\' . PromiseInterface::class) + )->setDocComment( + new Doc(implode(PHP_EOL, [ + '/**', + ' * @return \\' . PromiseInterface::class . '<' . implode('|', array_unique($callReturnTypes)) . '>', + ' */', + ])) + )->addParam((new Param('call'))->setType('string'))->addParam((new Param('params'))->setType('array')->setDefault([])) + ); + + $class->addStmt( + $factory->method('hydrateObject')->makePublic()->setReturnType('object')->addParam( + (new Param('className'))->setType('string') + )->addParam( + (new Param('data'))->setType('array') + ) + ); + + yield new File($namespace . '\\' . 'ClientInterface', $stmt->addStmt($class)->getNode()); + } +}