Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
);
}
}
Expand All @@ -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
);

Expand All @@ -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));
}
}
14 changes: 7 additions & 7 deletions src/Generator/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class Schema
* @param OpenAPiSchema $schema
* @return iterable<Node>
*/
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);
Expand Down Expand Up @@ -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)';
Expand Down Expand Up @@ -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)';
Expand Down