Skip to content

Commit 0304a06

Browse files
committed
Add code generation for value objects of type boolean and number
1 parent 590f204 commit 0304a06

File tree

8 files changed

+572
-2
lines changed

8 files changed

+572
-2
lines changed

.github/workflows/integration.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ jobs:
4444
run: |
4545
composer install --no-progress --prefer-dist --optimize-autoloader
4646
47-
#- name: Run Tests
48-
# run: php vendor/bin/phpunit --coverage-text
47+
- name: Run Tests
48+
run: php vendor/bin/phpunit --coverage-text
4949

5050
coding-standard:
5151
name: Coding Standard

src/ValueObject/BooleanFactory.php

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
<?php
2+
3+
/**
4+
* @see https://github.com/open-code-modeling/json-schema-to-php-ast for the canonical source repository
5+
* @copyright https://github.com/open-code-modeling/json-schema-to-php-ast/blob/master/COPYRIGHT.md
6+
* @license https://github.com/open-code-modeling/json-schema-to-php-ast/blob/master/LICENSE.md MIT License
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace OpenCodeModeling\JsonSchemaToPhpAst\ValueObject;
12+
13+
use OpenCodeModeling\CodeAst\Code\BodyGenerator;
14+
use OpenCodeModeling\CodeAst\Code\MethodGenerator;
15+
use OpenCodeModeling\CodeAst\Code\ParameterGenerator;
16+
use OpenCodeModeling\CodeAst\NodeVisitor\ClassMethod;
17+
use OpenCodeModeling\JsonSchemaToPhp\Type\BooleanType;
18+
use OpenCodeModeling\JsonSchemaToPhpAst\PropertyFactory;
19+
use PhpParser\NodeVisitor;
20+
use PhpParser\Parser;
21+
22+
/**
23+
* This file creates node visitors for a value object of type bool.
24+
*
25+
* The following code will be generated:
26+
*
27+
* private bool $boolean;
28+
*
29+
* public static function fromBool(bool $boolean): self
30+
* {
31+
* return new self($boolean);
32+
* }
33+
*
34+
* private function __construct(bool $boolean)
35+
* {
36+
* $this->boolean = $boolean;
37+
* }
38+
*
39+
* public function toBool(): bool
40+
* {
41+
* return $this->boolean;
42+
* }
43+
*
44+
* public function equals($other): bool
45+
* {
46+
* if(!$other instanceof self) {
47+
* return false;
48+
* }
49+
*
50+
* return $this->boolean === $other->boolean;
51+
* }
52+
*
53+
* public function __toString(): string
54+
* {
55+
* return $this->boolean ? 'TRUE' : 'FALSE';
56+
* }
57+
*/
58+
final class BooleanFactory
59+
{
60+
private Parser $parser;
61+
private PropertyFactory $propertyFactory;
62+
63+
public function __construct(Parser $parser, bool $typed)
64+
{
65+
$this->parser = $parser;
66+
$this->propertyFactory = new PropertyFactory($typed);
67+
}
68+
69+
/**
70+
* @param BooleanType $typeDefinition
71+
* @return array<NodeVisitor>
72+
*/
73+
public function nodeVisitors(BooleanType $typeDefinition): array
74+
{
75+
$name = $typeDefinition->name() ?: 'boolean';
76+
77+
return $this->nodeVisitorsFromNative($name);
78+
}
79+
80+
/**
81+
* @param string $name
82+
* @return array<NodeVisitor>
83+
*/
84+
public function nodeVisitorsFromNative(string $name): array
85+
{
86+
$nodeVisitors = $this->propertyFactory->nodeVisitorFromNative($name, 'bool');
87+
$nodeVisitors[] = $this->methodFromBool($name);
88+
$nodeVisitors[] = $this->methodMagicConstruct($name);
89+
$nodeVisitors[] = $this->methodToBool($name);
90+
$nodeVisitors[] = $this->methodEquals($name);
91+
$nodeVisitors[] = $this->methodMagicToString($name);
92+
93+
return $nodeVisitors;
94+
}
95+
96+
public function methodFromBool(string $argumentName): NodeVisitor
97+
{
98+
$method = new MethodGenerator(
99+
'fromBool',
100+
[
101+
new ParameterGenerator($argumentName, 'bool'),
102+
],
103+
MethodGenerator::FLAG_STATIC | MethodGenerator::FLAG_PUBLIC,
104+
new BodyGenerator($this->parser, 'return new self($' . $argumentName . ');')
105+
);
106+
$method->setReturnType('self');
107+
108+
return new ClassMethod($method);
109+
}
110+
111+
public function methodMagicConstruct(string $argumentName): NodeVisitor
112+
{
113+
$method = new MethodGenerator(
114+
'__construct',
115+
[
116+
new ParameterGenerator($argumentName, 'bool'),
117+
],
118+
MethodGenerator::FLAG_PRIVATE,
119+
new BodyGenerator($this->parser, \sprintf('$this->%s = $%s;', $argumentName, $argumentName))
120+
);
121+
122+
return new ClassMethod($method);
123+
}
124+
125+
public function methodToBool(string $argumentName): NodeVisitor
126+
{
127+
$method = new MethodGenerator(
128+
'toBool',
129+
[],
130+
MethodGenerator::FLAG_PUBLIC,
131+
new BodyGenerator($this->parser, 'return $this->' . $argumentName . ';')
132+
);
133+
$method->setReturnType('bool');
134+
135+
return new ClassMethod($method);
136+
}
137+
138+
public function methodEquals(string $propertyName, string $argumentName = 'other'): NodeVisitor
139+
{
140+
$body = <<<PHP
141+
if(!\$$argumentName instanceof self) {
142+
return false;
143+
}
144+
145+
return \$this->$propertyName === \$$argumentName->$propertyName;
146+
PHP;
147+
148+
$method = new MethodGenerator(
149+
'equals',
150+
[
151+
new ParameterGenerator($argumentName),
152+
],
153+
MethodGenerator::FLAG_PUBLIC,
154+
new BodyGenerator($this->parser, $body)
155+
);
156+
$method->setReturnType('bool');
157+
158+
return new ClassMethod($method);
159+
}
160+
161+
public function methodMagicToString(string $argumentName): NodeVisitor
162+
{
163+
$method = new MethodGenerator(
164+
'__toString',
165+
[],
166+
MethodGenerator::FLAG_PUBLIC,
167+
new BodyGenerator($this->parser, 'return $this->' . $argumentName . " ? 'TRUE' : 'FALSE';")
168+
);
169+
$method->setReturnType('string');
170+
171+
return new ClassMethod($method);
172+
}
173+
}

src/ValueObject/NumberFactory.php

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
<?php
2+
3+
/**
4+
* @see https://github.com/open-code-modeling/json-schema-to-php-ast for the canonical source repository
5+
* @copyright https://github.com/open-code-modeling/json-schema-to-php-ast/blob/master/COPYRIGHT.md
6+
* @license https://github.com/open-code-modeling/json-schema-to-php-ast/blob/master/LICENSE.md MIT License
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace OpenCodeModeling\JsonSchemaToPhpAst\ValueObject;
12+
13+
use OpenCodeModeling\CodeAst\Code\BodyGenerator;
14+
use OpenCodeModeling\CodeAst\Code\MethodGenerator;
15+
use OpenCodeModeling\CodeAst\Code\ParameterGenerator;
16+
use OpenCodeModeling\CodeAst\NodeVisitor\ClassMethod;
17+
use OpenCodeModeling\JsonSchemaToPhp\Type\NumberType;
18+
use OpenCodeModeling\JsonSchemaToPhpAst\PropertyFactory;
19+
use PhpParser\NodeVisitor;
20+
use PhpParser\Parser;
21+
22+
/**
23+
* This file creates node visitors for a value object of type float.
24+
*
25+
* The following code will be generated:
26+
*
27+
* private float $number;
28+
*
29+
* public static function fromFloat(float $number): self
30+
* {
31+
* return new self($number);
32+
* }
33+
*
34+
* private function __construct(float $number)
35+
* {
36+
* $this->number = $number;
37+
* }
38+
*
39+
* public function toFloat(): float
40+
* {
41+
* return $this->number;
42+
* }
43+
*
44+
* public function equals($other): bool
45+
* {
46+
* if(!$other instanceof self) {
47+
* return false;
48+
* }
49+
*
50+
* return $this->number === $other->number;
51+
* }
52+
*
53+
* public function __toString(): string
54+
* {
55+
* return (string)$this->number;
56+
* }
57+
*/
58+
final class NumberFactory
59+
{
60+
private Parser $parser;
61+
private PropertyFactory $propertyFactory;
62+
63+
public function __construct(Parser $parser, bool $typed)
64+
{
65+
$this->parser = $parser;
66+
$this->propertyFactory = new PropertyFactory($typed);
67+
}
68+
69+
/**
70+
* @param NumberType $typeDefinition
71+
* @return array<NodeVisitor>
72+
*/
73+
public function nodeVisitors(NumberType $typeDefinition): array
74+
{
75+
$name = $typeDefinition->name() ?: 'number';
76+
77+
return $this->nodeVisitorsFromNative($name);
78+
}
79+
80+
/**
81+
* @param string $name
82+
* @return array<NodeVisitor>
83+
*/
84+
public function nodeVisitorsFromNative(string $name): array
85+
{
86+
$nodeVisitors = $this->propertyFactory->nodeVisitorFromNative($name, 'float');
87+
$nodeVisitors[] = $this->methodFromFloat($name);
88+
$nodeVisitors[] = $this->methodMagicConstruct($name);
89+
$nodeVisitors[] = $this->methodToFloat($name);
90+
$nodeVisitors[] = $this->methodEquals($name);
91+
$nodeVisitors[] = $this->methodMagicToString($name);
92+
93+
return $nodeVisitors;
94+
}
95+
96+
public function methodFromFloat(string $argumentName): NodeVisitor
97+
{
98+
$method = new MethodGenerator(
99+
'fromFloat',
100+
[
101+
new ParameterGenerator($argumentName, 'float'),
102+
],
103+
MethodGenerator::FLAG_STATIC | MethodGenerator::FLAG_PUBLIC,
104+
new BodyGenerator($this->parser, 'return new self($' . $argumentName . ');')
105+
);
106+
$method->setReturnType('self');
107+
108+
return new ClassMethod($method);
109+
}
110+
111+
public function methodMagicConstruct(string $argumentName): NodeVisitor
112+
{
113+
$method = new MethodGenerator(
114+
'__construct',
115+
[
116+
new ParameterGenerator($argumentName, 'float'),
117+
],
118+
MethodGenerator::FLAG_PRIVATE,
119+
new BodyGenerator($this->parser, \sprintf('$this->%s = $%s;', $argumentName, $argumentName))
120+
);
121+
122+
return new ClassMethod($method);
123+
}
124+
125+
public function methodToFloat(string $argumentName): NodeVisitor
126+
{
127+
$method = new MethodGenerator(
128+
'toFloat',
129+
[],
130+
MethodGenerator::FLAG_PUBLIC,
131+
new BodyGenerator($this->parser, 'return $this->' . $argumentName . ';')
132+
);
133+
$method->setReturnType('float');
134+
135+
return new ClassMethod($method);
136+
}
137+
138+
public function methodEquals(string $propertyName, string $argumentName = 'other'): NodeVisitor
139+
{
140+
$body = <<<PHP
141+
if(!\$$argumentName instanceof self) {
142+
return false;
143+
}
144+
145+
return \$this->$propertyName === \$$argumentName->$propertyName;
146+
PHP;
147+
148+
$method = new MethodGenerator(
149+
'equals',
150+
[
151+
new ParameterGenerator($argumentName),
152+
],
153+
MethodGenerator::FLAG_PUBLIC,
154+
new BodyGenerator($this->parser, $body)
155+
);
156+
$method->setReturnType('bool');
157+
158+
return new ClassMethod($method);
159+
}
160+
161+
public function methodMagicToString(string $argumentName): NodeVisitor
162+
{
163+
$method = new MethodGenerator(
164+
'__toString',
165+
[],
166+
MethodGenerator::FLAG_PUBLIC,
167+
new BodyGenerator($this->parser, 'return (string)$this->' . $argumentName . ';')
168+
);
169+
$method->setReturnType('string');
170+
171+
return new ClassMethod($method);
172+
}
173+
}

0 commit comments

Comments
 (0)