Skip to content

Commit eb115a0

Browse files
authored
Merge pull request #25 from php-api-clients/Turn_underscores_into_namespace_seperators
Turn underscores into namespace seperators
2 parents b4f13f2 + 00fb213 commit eb115a0

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

src/Generator.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function generate(string $namespace, string $destinationPath)
3434

3535
private function className(string $className): string
3636
{
37-
return str_replace(['{', '}', '-', '$'], ['Cb', 'Rcb', 'Dash', '_'], (new Convert($className))->toPascal());
37+
return str_replace(['{', '}', '-', '$', '_'], ['Cb', 'Rcb', 'Dash', '_', '\\'], (new Convert($className))->toPascal());
3838
}
3939

4040
private function cleanUpNamespace(string $namespace): string
@@ -69,10 +69,11 @@ private function all(string $namespace): iterable
6969

7070
yield from Schema::generate(
7171
$name,
72-
$this->cleanUpNamespace($namespace . dirname('Schema/' . $schemaClassName)),
73-
strrev(explode('/', strrev($schemaClassName))[0]),
72+
$this->dirname($namespace . 'Schema/' . $schemaClassName),
73+
$this->basename($namespace . 'Schema/' . $schemaClassName),
7474
$schema,
75-
$schemaClassNameMap
75+
$schemaClassNameMap,
76+
$namespace . 'Schema'
7677
);
7778
}
7879
}
@@ -86,9 +87,9 @@ private function all(string $namespace): iterable
8687

8788
yield from Path::generate(
8889
$path,
89-
$this->cleanUpNamespace($namespace . dirname('Path/' . $pathClassName)),
90+
$this->dirname($namespace . 'Path/' . $pathClassName),
9091
$namespace,
91-
strrev(explode('/', strrev($pathClassName))[0]),
92+
$this->basename($namespace . 'Path/' . $pathClassName),
9293
$pathItem
9394
);
9495

@@ -102,12 +103,26 @@ private function all(string $namespace): iterable
102103
yield from Operation::generate(
103104
$path,
104105
$method,
105-
$this->cleanUpNamespace($namespace . dirname('Operation/' . $operationClassName)),
106-
strrev(explode('/', strrev($operationClassName))[0]),
106+
$this->dirname($namespace . 'Operation/' . $operationClassName),
107+
$this->basename($namespace . 'Operation/' . $operationClassName),
107108
$operation
108109
);
109110
}
110111
}
111112
}
112113
}
114+
115+
private function dirname(string $fqcn): string
116+
{
117+
$fqcn = str_replace('\\', '/', $fqcn);
118+
119+
return $this->cleanUpNamespace(dirname($fqcn));
120+
}
121+
122+
private function basename(string $fqcn): string
123+
{
124+
$fqcn = str_replace('\\', '/', $fqcn);
125+
126+
return $this->cleanUpNamespace(basename($fqcn));
127+
}
113128
}

src/Generator/Schema.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ final class Schema
2121
* @param OpenAPiSchema $schema
2222
* @return iterable<Node>
2323
*/
24-
public static function generate(string $name, string $namespace, string $className, OpenAPiSchema $schema, array $schemaClassNameMap): iterable
24+
public static function generate(string $name, string $namespace, string $className, OpenAPiSchema $schema, array $schemaClassNameMap, string $rootNamespace): iterable
2525
{
2626
$factory = new BuilderFactory();
2727
$stmt = $factory->namespace($namespace);
@@ -80,11 +80,11 @@ public static function generate(string $name, string $namespace, string $classNa
8080
if (is_string($property->type)) {
8181
if ($property->type === 'array' && $property->items instanceof OpenAPiSchema) {
8282
if (array_key_exists(spl_object_hash($property->items), $schemaClassNameMap)) {
83-
$methodDocBlock[] = '@return array<\\' . $namespace . '\\' . $schemaClassNameMap[spl_object_hash($property->items)] . '>';
84-
$docBlock[] = '@var array<\\' . $namespace . '\\' . $schemaClassNameMap[spl_object_hash($property->items)] . '>';
85-
$docBlock[] = '@\WyriHaximus\Hydrator\Attribute\HydrateArray(\\' . $namespace . '\\' . $schemaClassNameMap[spl_object_hash($property->items)] . '::class)';
83+
$methodDocBlock[] = '@return array<\\' . $rootNamespace . '\\' . $schemaClassNameMap[spl_object_hash($property->items)] . '>';
84+
$docBlock[] = '@var array<\\' . $rootNamespace . '\\' . $schemaClassNameMap[spl_object_hash($property->items)] . '>';
85+
$docBlock[] = '@\WyriHaximus\Hydrator\Attribute\HydrateArray(\\' . $rootNamespace . '\\' . $schemaClassNameMap[spl_object_hash($property->items)] . '::class)';
8686
} elseif ($property->items->type === 'object') {
87-
yield from self::generate($name . '::' . $propertyName, $namespace . '\\' . $className, (new Convert($propertyName))->toPascal(), $property->items, $schemaClassNameMap);
87+
yield from self::generate($name . '::' . $propertyName, $namespace . '\\' . $className, (new Convert($propertyName))->toPascal(), $property->items, $schemaClassNameMap, $rootNamespace);
8888
$methodDocBlock[] = '@return array<\\' . $namespace . '\\' . $className . '\\' . (new Convert($propertyName))->toPascal() . '>';
8989
$docBlock[] = '@var array<\\' . $namespace . '\\' . $className . '\\' . (new Convert($propertyName))->toPascal() . '>';
9090
$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
113113
}
114114

115115
if (is_array($property->anyOf) && $property->anyOf[0] instanceof OpenAPiSchema && array_key_exists(spl_object_hash($property->anyOf[0]), $schemaClassNameMap)) {
116-
$fqcnn = '\\' . $namespace . '\\' . $schemaClassNameMap[spl_object_hash($property->anyOf[0])];
116+
$fqcnn = '\\' . $rootNamespace . '\\' . $schemaClassNameMap[spl_object_hash($property->anyOf[0])];
117117
$propertyStmt->setType('?' . $fqcnn)->setDefault(null);
118118
$method->setReturnType('?' . $fqcnn);
119119
$propertyDocBlock[] = '@\WyriHaximus\Hydrator\Attribute\Hydrate(' . $fqcnn . '::class)';
120120
}
121121

122122
if ($property->type === 'object' && $property instanceof OpenAPiSchema && array_key_exists(spl_object_hash($property), $schemaClassNameMap)) {
123-
$fqcnn = '\\' . $namespace . '\\' . $schemaClassNameMap[spl_object_hash($property)];
123+
$fqcnn = '\\' . $rootNamespace . '\\' . $schemaClassNameMap[spl_object_hash($property)];
124124
$propertyStmt->setType('?' . $fqcnn)->setDefault(null);
125125
$method->setReturnType('?' . $fqcnn);
126126
$propertyDocBlock[] = '@\WyriHaximus\Hydrator\Attribute\Hydrate(' . $fqcnn . '::class)';

0 commit comments

Comments
 (0)