Skip to content

Commit f46809a

Browse files
committed
Improve value object factory - Close #21
1 parent e29bd96 commit f46809a

8 files changed

+363
-184
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ Assume you have the following JSON schema
6464

6565
Then you can use the `ValueObjectFactory` to generate PHP code for the following classes:
6666
- `Order`
67-
- `BillingAddress`
6867
- `ShippingAddresses`
6968
- `Address`
7069
- `StreetAddress`
@@ -118,7 +117,7 @@ $classBuilder = ClassBuilder::fromScratch('Order', 'YourNamespaceFromComposer')-
118117

119118
$valueObjectFactory->generateClasses($classBuilder, $fileCollection, $typeSet, $srcFolder);
120119

121-
// $fileCollection contains 7 classes
120+
// $fileCollection contains 6 classes
122121

123122
// now let's add constants and getter methods of properties for non value objects
124123
$valueObjectFactory->addGetterMethodsForProperties($fileCollection, true);

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
},
3434
"require": {
3535
"php": "^7.4 || ^8.0",
36-
"open-code-modeling/json-schema-to-php": "^0.2.0 || 0.3.x-dev",
36+
"open-code-modeling/json-schema-to-php": "^0.3.0 || 0.4.x-dev",
3737
"open-code-modeling/php-code-ast": "^0.11.0 || 0.12.x-dev"
3838
},
3939
"require-dev": {

composer.lock

Lines changed: 55 additions & 55 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ValueObject/ArrayFactory.php

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
use OpenCodeModeling\JsonSchemaToPhp\Type\ArrayType;
2020
use OpenCodeModeling\JsonSchemaToPhp\Type\ReferenceType;
2121
use OpenCodeModeling\JsonSchemaToPhp\Type\ScalarType;
22-
use OpenCodeModeling\JsonSchemaToPhp\Type\StringType;
23-
use OpenCodeModeling\JsonSchemaToPhp\Type\TypeDefinition;
2422
use OpenCodeModeling\JsonSchemaToPhp\Type\TypeSet;
2523
use OpenCodeModeling\JsonSchemaToPhpAst\Common\IteratorFactory;
2624
use PhpParser\NodeVisitor;
@@ -201,7 +199,7 @@ public function classBuilder(ArrayType $typeDefinition): ClassBuilder
201199
return $this->classBuilderFromNative($name, ...$typeDefinition->items());
202200
}
203201

204-
private function determineType(string $name, TypeSet ...$typeSets): ?TypeDefinition
202+
private function determineTypeName(string $name, TypeSet ...$typeSets): ?string
205203
{
206204
if (\count($typeSets) !== 1) {
207205
throw new \RuntimeException('Can only handle one JSON type');
@@ -219,9 +217,7 @@ private function determineType(string $name, TypeSet ...$typeSets): ?TypeDefinit
219217
$resolvedTypeSet = $type->resolvedType();
220218

221219
if ($resolvedTypeSet === null) {
222-
$resolvedTypeSet = new TypeSet(
223-
StringType::fromDefinition(['type' => StringType::type(), 'name' => $type->name()])
224-
);
220+
return $type->extractNameFromReference();
225221
}
226222
if (\count($resolvedTypeSet) !== 1) {
227223
throw new \RuntimeException('Can only handle one JSON type');
@@ -236,7 +232,7 @@ private function determineType(string $name, TypeSet ...$typeSets): ?TypeDefinit
236232
);
237233
}
238234

239-
return $type;
235+
return $type->name();
240236
}
241237

242238
/**
@@ -246,8 +242,7 @@ private function determineType(string $name, TypeSet ...$typeSets): ?TypeDefinit
246242
*/
247243
public function nodeVisitorsFromNative(string $name, TypeSet ...$typeSets): array
248244
{
249-
$type = $this->determineType($name, ...$typeSets);
250-
$typeName = $type->name();
245+
$typeName = $this->determineTypeName($name, ...$typeSets);
251246

252247
$nodeVisitors = $this->iteratorFactory->nodeVisitorsFromNative(
253248
($this->propertyNameFilter)($name),
@@ -273,8 +268,7 @@ public function nodeVisitorsFromNative(string $name, TypeSet ...$typeSets): arra
273268

274269
public function classBuilderFromNative(string $name, TypeSet ...$typeSets): ClassBuilder
275270
{
276-
$type = $this->determineType($name, ...$typeSets);
277-
$typeName = $type->name();
271+
$typeName = $this->determineTypeName($name, ...$typeSets);
278272

279273
$classBuilder = $this->iteratorFactory->classBuilderFromNative(
280274
($this->propertyNameFilter)($name),
@@ -322,10 +316,10 @@ public function methodMagicConstruct(
322316
$method = new MethodGenerator(
323317
'__construct',
324318
[
325-
(new ParameterGenerator($argumentName, ($this->classNameFilter)($argumentType)))->setVariadic(true),
319+
(new ParameterGenerator(($this->propertyNameFilter)($argumentName), ($this->classNameFilter)($argumentType)))->setVariadic(true),
326320
],
327321
MethodGenerator::FLAG_PRIVATE,
328-
new BodyGenerator($this->parser, \sprintf('$this->%s = $%s;', $propertyName, $argumentName))
322+
new BodyGenerator($this->parser, \sprintf('$this->%s = $%s;', ($this->propertyNameFilter)($propertyName), ($this->propertyNameFilter)($argumentName)))
329323
);
330324
$method->setTyped($this->typed);
331325

@@ -349,7 +343,14 @@ public function methodAdd(
349343
(new ParameterGenerator(($this->propertyNameFilter)($argumentType), ($this->classNameFilter)($argumentType))),
350344
],
351345
MethodGenerator::FLAG_PUBLIC,
352-
new BodyGenerator($this->parser, \sprintf($body, $propertyName, ($this->propertyNameFilter)($argumentType)))
346+
new BodyGenerator(
347+
$this->parser,
348+
\sprintf(
349+
$body,
350+
($this->propertyNameFilter)($propertyName),
351+
($this->propertyNameFilter)($argumentType)
352+
)
353+
)
353354
);
354355
$method->setTyped($this->typed);
355356
$method->setReturnType('self');
@@ -378,7 +379,16 @@ static function($v) use ($%s) { return !$v->equals($%s); }
378379
(new ParameterGenerator(($this->propertyNameFilter)($argumentType), ($this->classNameFilter)($argumentType))),
379380
],
380381
MethodGenerator::FLAG_PUBLIC,
381-
new BodyGenerator($this->parser, \sprintf($body, $propertyName, $propertyName, ($this->propertyNameFilter)($argumentType), ($this->propertyNameFilter)($argumentType)))
382+
new BodyGenerator(
383+
$this->parser,
384+
\sprintf(
385+
$body,
386+
($this->propertyNameFilter)($propertyName),
387+
($this->propertyNameFilter)($propertyName),
388+
($this->propertyNameFilter)($argumentType),
389+
($this->propertyNameFilter)($argumentType)
390+
)
391+
)
382392
);
383393
$method->setTyped($this->typed);
384394
$method->setReturnType('self');
@@ -479,7 +489,7 @@ static function($%s) use ($filter) { return $filter($%s); }
479489
new ParameterGenerator('filter', 'callable'),
480490
],
481491
MethodGenerator::FLAG_PUBLIC,
482-
new BodyGenerator($this->parser, \sprintf($body, $propertyName, 'v', 'v'))
492+
new BodyGenerator($this->parser, \sprintf($body, ($this->propertyNameFilter)($propertyName), 'v', 'v'))
483493
);
484494
$method->setTyped($this->typed);
485495
$method->setReturnType('self');
@@ -566,10 +576,10 @@ public function methodFromItems(
566576
$method = new MethodGenerator(
567577
'fromItems',
568578
[
569-
(new ParameterGenerator($argumentName, ($this->classNameFilter)($argumentType)))->setVariadic(true),
579+
(new ParameterGenerator(($this->propertyNameFilter)($argumentName), ($this->classNameFilter)($argumentType)))->setVariadic(true),
570580
],
571581
MethodGenerator::FLAG_PUBLIC | MethodGenerator::FLAG_STATIC,
572-
new BodyGenerator($this->parser, \sprintf('return new self(...$%s);', $argumentName))
582+
new BodyGenerator($this->parser, \sprintf('return new self(...$%s);', ($this->propertyNameFilter)($argumentName)))
573583
);
574584
$method->setTyped($this->typed);
575585
$method->setReturnType('self');

0 commit comments

Comments
 (0)