Skip to content

Commit

Permalink
Add class builder factory methods - Close #1
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrokeil committed Nov 12, 2020
1 parent ddfac36 commit 36299ec
Show file tree
Hide file tree
Showing 13 changed files with 310 additions and 88 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"require": {
"php": "^7.4 || ^8.0",
"open-code-modeling/json-schema-to-php": "dev-master",
"open-code-modeling/php-code-ast": "^0.3.0|^0.4.0|dev-master"
"open-code-modeling/php-code-ast": "^0.8.2|dev-master"
},
"require-dev": {
"jangregor/phpstan-prophecy": "^0.8.0",
Expand Down
7 changes: 6 additions & 1 deletion src/PropertyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,16 @@ public function nodeVisitorFromNative(string $name, string $type): array
{
return [
new \OpenCodeModeling\CodeAst\NodeVisitor\Property(
new PropertyGenerator($name, $type, null, $this->typed)
$this->propertyGenerator($name, $type)
),
];
}

public function propertyGenerator(string $name, string $type): PropertyGenerator
{
return new PropertyGenerator($name, $type, null, $this->typed);
}

/**
* @param ReferenceType $type
* @return array<NodeVisitor>
Expand Down
50 changes: 35 additions & 15 deletions src/ValueObject/BooleanFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace OpenCodeModeling\JsonSchemaToPhpAst\ValueObject;

use OpenCodeModeling\CodeAst\Builder\ClassBuilder;
use OpenCodeModeling\CodeAst\Code\BodyGenerator;
use OpenCodeModeling\CodeAst\Code\MethodGenerator;
use OpenCodeModeling\CodeAst\Code\ParameterGenerator;
Expand Down Expand Up @@ -79,23 +80,42 @@ public function nodeVisitors(BooleanType $typeDefinition): array
return $this->nodeVisitorsFromNative($name);
}

public function classBuilder(BooleanType $typeDefinition): ClassBuilder
{
$name = $typeDefinition->name() ?: 'boolean';

return $this->classBuilderFromNative($name)->setTyped($this->typed);
}

/**
* @param string $name
* @return array<NodeVisitor>
*/
public function nodeVisitorsFromNative(string $name): array
{
$nodeVisitors = $this->propertyFactory->nodeVisitorFromNative($name, 'bool');
$nodeVisitors[] = $this->methodFromBool($name);
$nodeVisitors[] = $this->methodMagicConstruct($name);
$nodeVisitors[] = $this->methodToBool($name);
$nodeVisitors[] = $this->methodEquals($name);
$nodeVisitors[] = $this->methodMagicToString($name);
$nodeVisitors[] = new ClassMethod($this->methodFromBool($name));
$nodeVisitors[] = new ClassMethod($this->methodMagicConstruct($name));
$nodeVisitors[] = new ClassMethod($this->methodToBool($name));
$nodeVisitors[] = new ClassMethod($this->methodEquals($name));
$nodeVisitors[] = new ClassMethod($this->methodMagicToString($name));

return $nodeVisitors;
}

public function methodFromBool(string $argumentName): NodeVisitor
public function classBuilderFromNative(string $name): ClassBuilder
{
return ClassBuilder::fromNodes(
$this->propertyFactory->propertyGenerator($name, 'bool')->generate(),
$this->methodFromBool($name)->generate(),
$this->methodMagicConstruct($name)->generate(),
$this->methodToBool($name)->generate(),
$this->methodEquals($name)->generate(),
$this->methodMagicToString($name)->generate(),
)->setTyped($this->typed);
}

public function methodFromBool(string $argumentName): MethodGenerator
{
$method = new MethodGenerator(
'fromBool',
Expand All @@ -108,10 +128,10 @@ public function methodFromBool(string $argumentName): NodeVisitor
$method->setTyped($this->typed);
$method->setReturnType('self');

return new ClassMethod($method);
return $method;
}

public function methodMagicConstruct(string $argumentName): NodeVisitor
public function methodMagicConstruct(string $argumentName): MethodGenerator
{
$method = new MethodGenerator(
'__construct',
Expand All @@ -123,10 +143,10 @@ public function methodMagicConstruct(string $argumentName): NodeVisitor
);
$method->setTyped($this->typed);

return new ClassMethod($method);
return $method;
}

public function methodToBool(string $argumentName): NodeVisitor
public function methodToBool(string $argumentName): MethodGenerator
{
$method = new MethodGenerator(
'toBool',
Expand All @@ -137,10 +157,10 @@ public function methodToBool(string $argumentName): NodeVisitor
$method->setTyped($this->typed);
$method->setReturnType('bool');

return new ClassMethod($method);
return $method;
}

public function methodEquals(string $propertyName, string $argumentName = 'other'): NodeVisitor
public function methodEquals(string $propertyName, string $argumentName = 'other'): MethodGenerator
{
$body = <<<PHP
if(!\$$argumentName instanceof self) {
Expand All @@ -161,10 +181,10 @@ public function methodEquals(string $propertyName, string $argumentName = 'other
$method->setTyped($this->typed);
$method->setReturnType('bool');

return new ClassMethod($method);
return $method;
}

public function methodMagicToString(string $argumentName): NodeVisitor
public function methodMagicToString(string $argumentName): MethodGenerator
{
$method = new MethodGenerator(
'__toString',
Expand All @@ -175,6 +195,6 @@ public function methodMagicToString(string $argumentName): NodeVisitor
$method->setTyped($this->typed);
$method->setReturnType('string');

return new ClassMethod($method);
return $method;
}
}
80 changes: 54 additions & 26 deletions src/ValueObject/DateTimeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace OpenCodeModeling\JsonSchemaToPhpAst\ValueObject;

use OpenCodeModeling\CodeAst\Builder\ClassBuilder;
use OpenCodeModeling\CodeAst\Code\BodyGenerator;
use OpenCodeModeling\CodeAst\Code\ClassConstGenerator;
use OpenCodeModeling\CodeAst\Code\IdentifierGenerator;
Expand Down Expand Up @@ -107,6 +108,13 @@ public function nodeVisitors(StringType $typeDefinition): array
return $this->nodeVisitorsFromNative($name);
}

public function classBuilder(StringType $typeDefinition): ClassBuilder
{
$name = $typeDefinition->name() ?: 'dateTime';

return $this->classBuilderFromNative($name)->setTyped($this->typed);
}

/**
* @param string $name
* @param string $outputFormat
Expand All @@ -121,27 +129,47 @@ public function nodeVisitorsFromNative(string $name, string $outputFormat = DATE
new ClassConstant(
new IdentifierGenerator(
'OUTPUT_FORMAT',
new ClassConstGenerator(
'OUTPUT_FORMAT',
$outputFormat,
ClassConstGenerator::FLAG_PRIVATE
)
$this->classConstant($outputFormat)
)
)
);

$nodeVisitors[] = $this->methodFromDateTime($name);
$nodeVisitors[] = $this->methodFromString($name);
$nodeVisitors[] = $this->methodMagicConstruct($name);
$nodeVisitors[] = $this->methodToString($name);
$nodeVisitors[] = $this->methodDateTime($name);
$nodeVisitors[] = $this->methodMagicToString();
$nodeVisitors[] = $this->methodEnsureUtc($name);
$nodeVisitors[] = new ClassMethod($this->methodFromDateTime($name));
$nodeVisitors[] = new ClassMethod($this->methodFromString($name));
$nodeVisitors[] = new ClassMethod($this->methodMagicConstruct($name));
$nodeVisitors[] = new ClassMethod($this->methodToString($name));
$nodeVisitors[] = new ClassMethod($this->methodDateTime($name));
$nodeVisitors[] = new ClassMethod($this->methodMagicToString());
$nodeVisitors[] = new ClassMethod($this->methodEnsureUtc($name));

return $nodeVisitors;
}

public function methodFromDateTime(string $argumentName): NodeVisitor
public function classBuilderFromNative(string $name, string $outputFormat = DATE_ATOM): ClassBuilder
{
return ClassBuilder::fromNodes(
$this->classConstant($outputFormat)->generate(),
$this->propertyFactory->propertyGenerator($name, 'DateTimeImmutable')->generate(),
$this->methodFromDateTime($name)->generate(),
$this->methodFromString($name)->generate(),
$this->methodMagicConstruct($name)->generate(),
$this->methodToString($name)->generate(),
$this->methodDateTime($name)->generate(),
$this->methodMagicToString()->generate(),
$this->methodEnsureUtc($name)->generate(),
)->setTyped($this->typed);
}

public function classConstant(string $outputFormat = DATE_ATOM): ClassConstGenerator
{
return new ClassConstGenerator(
'OUTPUT_FORMAT',
$outputFormat,
ClassConstGenerator::FLAG_PRIVATE
);
}

public function methodFromDateTime(string $argumentName): MethodGenerator
{
$method = new MethodGenerator(
'fromDateTime',
Expand All @@ -154,10 +182,10 @@ public function methodFromDateTime(string $argumentName): NodeVisitor
$method->setTyped($this->typed);
$method->setReturnType('self');

return new ClassMethod($method);
return $method;
}

public function methodFromString(string $argumentName): NodeVisitor
public function methodFromString(string $argumentName): MethodGenerator
{
$body = <<<PHP
try {
Expand All @@ -182,10 +210,10 @@ public function methodFromString(string $argumentName): NodeVisitor
$method->setTyped($this->typed);
$method->setReturnType('self');

return new ClassMethod($method);
return $method;
}

public function methodMagicConstruct(string $argumentName): NodeVisitor
public function methodMagicConstruct(string $argumentName): MethodGenerator
{
$method = new MethodGenerator(
'__construct',
Expand All @@ -197,10 +225,10 @@ public function methodMagicConstruct(string $argumentName): NodeVisitor
);
$method->setTyped($this->typed);

return new ClassMethod($method);
return $method;
}

public function methodToString(string $argumentName): NodeVisitor
public function methodToString(string $argumentName): MethodGenerator
{
$method = new MethodGenerator(
'toString',
Expand All @@ -211,10 +239,10 @@ public function methodToString(string $argumentName): NodeVisitor
$method->setTyped($this->typed);
$method->setReturnType('string');

return new ClassMethod($method);
return $method;
}

public function methodDateTime(string $argumentName): NodeVisitor
public function methodDateTime(string $argumentName): MethodGenerator
{
$method = new MethodGenerator(
'dateTime',
Expand All @@ -225,10 +253,10 @@ public function methodDateTime(string $argumentName): NodeVisitor
$method->setTyped($this->typed);
$method->setReturnType('DateTimeImmutable');

return new ClassMethod($method);
return $method;
}

public function methodMagicToString(): NodeVisitor
public function methodMagicToString(): MethodGenerator
{
$method = new MethodGenerator(
'__toString',
Expand All @@ -239,10 +267,10 @@ public function methodMagicToString(): NodeVisitor
$method->setTyped($this->typed);
$method->setReturnType('string');

return new ClassMethod($method);
return $method;
}

public function methodEnsureUtc(string $argumentName): NodeVisitor
public function methodEnsureUtc(string $argumentName): MethodGenerator
{
$body = <<<'PHP'
if ($argumentName->getTimezone()->getName() !== 'UTC') {
Expand All @@ -264,6 +292,6 @@ public function methodEnsureUtc(string $argumentName): NodeVisitor
$method->setTyped($this->typed);
$method->setReturnType('DateTimeImmutable');

return new ClassMethod($method);
return $method;
}
}
Loading

0 comments on commit 36299ec

Please sign in to comment.