diff --git a/src/Generator.php b/src/Generator.php index 678edf7..9d2cf4a 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -34,7 +34,7 @@ public function generate(string $namespace, string $destinationPath) private function className(string $className): string { - return str_replace(['{', '}', '-', '$'], ['Cb', 'Rcb', 'Dash', '_'], (new Convert($className))->toPascal()); + return str_replace(['{', '}', '-', '$', '_'], ['Cb', 'Rcb', 'Dash', '_', '\\'], (new Convert($className))->toPascal()); } private function cleanUpNamespace(string $namespace): string @@ -69,10 +69,11 @@ private function all(string $namespace): iterable yield from Schema::generate( $name, - $this->cleanUpNamespace($namespace . dirname('Schema/' . $schemaClassName)), - strrev(explode('/', strrev($schemaClassName))[0]), + $this->dirname($namespace . 'Schema/' . $schemaClassName), + $this->basename($namespace . 'Schema/' . $schemaClassName), $schema, - $schemaClassNameMap + $schemaClassNameMap, + $namespace . 'Schema' ); } } @@ -86,9 +87,9 @@ private function all(string $namespace): iterable yield from Path::generate( $path, - $this->cleanUpNamespace($namespace . dirname('Path/' . $pathClassName)), + $this->dirname($namespace . 'Path/' . $pathClassName), $namespace, - strrev(explode('/', strrev($pathClassName))[0]), + $this->basename($namespace . 'Path/' . $pathClassName), $pathItem ); @@ -102,12 +103,26 @@ private function all(string $namespace): iterable yield from Operation::generate( $path, $method, - $this->cleanUpNamespace($namespace . dirname('Operation/' . $operationClassName)), - strrev(explode('/', strrev($operationClassName))[0]), + $this->dirname($namespace . 'Operation/' . $operationClassName), + $this->basename($namespace . 'Operation/' . $operationClassName), $operation ); } } } } + + private function dirname(string $fqcn): string + { + $fqcn = str_replace('\\', '/', $fqcn); + + return $this->cleanUpNamespace(dirname($fqcn)); + } + + private function basename(string $fqcn): string + { + $fqcn = str_replace('\\', '/', $fqcn); + + return $this->cleanUpNamespace(basename($fqcn)); + } } diff --git a/src/Generator/Schema.php b/src/Generator/Schema.php index 4628934..77356d8 100644 --- a/src/Generator/Schema.php +++ b/src/Generator/Schema.php @@ -21,7 +21,7 @@ final class Schema * @param OpenAPiSchema $schema * @return iterable */ - public static function generate(string $name, string $namespace, string $className, OpenAPiSchema $schema, array $schemaClassNameMap): iterable + public static function generate(string $name, string $namespace, string $className, OpenAPiSchema $schema, array $schemaClassNameMap, string $rootNamespace): iterable { $factory = new BuilderFactory(); $stmt = $factory->namespace($namespace); @@ -80,11 +80,11 @@ public static function generate(string $name, string $namespace, string $classNa if (is_string($property->type)) { if ($property->type === 'array' && $property->items instanceof OpenAPiSchema) { if (array_key_exists(spl_object_hash($property->items), $schemaClassNameMap)) { - $methodDocBlock[] = '@return array<\\' . $namespace . '\\' . $schemaClassNameMap[spl_object_hash($property->items)] . '>'; - $docBlock[] = '@var array<\\' . $namespace . '\\' . $schemaClassNameMap[spl_object_hash($property->items)] . '>'; - $docBlock[] = '@\WyriHaximus\Hydrator\Attribute\HydrateArray(\\' . $namespace . '\\' . $schemaClassNameMap[spl_object_hash($property->items)] . '::class)'; + $methodDocBlock[] = '@return array<\\' . $rootNamespace . '\\' . $schemaClassNameMap[spl_object_hash($property->items)] . '>'; + $docBlock[] = '@var array<\\' . $rootNamespace . '\\' . $schemaClassNameMap[spl_object_hash($property->items)] . '>'; + $docBlock[] = '@\WyriHaximus\Hydrator\Attribute\HydrateArray(\\' . $rootNamespace . '\\' . $schemaClassNameMap[spl_object_hash($property->items)] . '::class)'; } elseif ($property->items->type === 'object') { - yield from self::generate($name . '::' . $propertyName, $namespace . '\\' . $className, (new Convert($propertyName))->toPascal(), $property->items, $schemaClassNameMap); + yield from self::generate($name . '::' . $propertyName, $namespace . '\\' . $className, (new Convert($propertyName))->toPascal(), $property->items, $schemaClassNameMap, $rootNamespace); $methodDocBlock[] = '@return array<\\' . $namespace . '\\' . $className . '\\' . (new Convert($propertyName))->toPascal() . '>'; $docBlock[] = '@var array<\\' . $namespace . '\\' . $className . '\\' . (new Convert($propertyName))->toPascal() . '>'; $docBlock[] = '@\WyriHaximus\Hydrator\Attribute\HydrateArray(\\' . $namespace . '\\' . $className . '\\' . (new Convert($propertyName))->toPascal() . '::class)'; @@ -113,14 +113,14 @@ public static function generate(string $name, string $namespace, string $classNa } if (is_array($property->anyOf) && $property->anyOf[0] instanceof OpenAPiSchema && array_key_exists(spl_object_hash($property->anyOf[0]), $schemaClassNameMap)) { - $fqcnn = '\\' . $namespace . '\\' . $schemaClassNameMap[spl_object_hash($property->anyOf[0])]; + $fqcnn = '\\' . $rootNamespace . '\\' . $schemaClassNameMap[spl_object_hash($property->anyOf[0])]; $propertyStmt->setType('?' . $fqcnn)->setDefault(null); $method->setReturnType('?' . $fqcnn); $propertyDocBlock[] = '@\WyriHaximus\Hydrator\Attribute\Hydrate(' . $fqcnn . '::class)'; } if ($property->type === 'object' && $property instanceof OpenAPiSchema && array_key_exists(spl_object_hash($property), $schemaClassNameMap)) { - $fqcnn = '\\' . $namespace . '\\' . $schemaClassNameMap[spl_object_hash($property)]; + $fqcnn = '\\' . $rootNamespace . '\\' . $schemaClassNameMap[spl_object_hash($property)]; $propertyStmt->setType('?' . $fqcnn)->setDefault(null); $method->setReturnType('?' . $fqcnn); $propertyDocBlock[] = '@\WyriHaximus\Hydrator\Attribute\Hydrate(' . $fqcnn . '::class)';