Skip to content

Commit 36299ec

Browse files
committed
Add class builder factory methods - Close #1
1 parent ddfac36 commit 36299ec

13 files changed

+310
-88
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"require": {
3535
"php": "^7.4 || ^8.0",
3636
"open-code-modeling/json-schema-to-php": "dev-master",
37-
"open-code-modeling/php-code-ast": "^0.3.0|^0.4.0|dev-master"
37+
"open-code-modeling/php-code-ast": "^0.8.2|dev-master"
3838
},
3939
"require-dev": {
4040
"jangregor/phpstan-prophecy": "^0.8.0",

src/PropertyFactory.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,16 @@ public function nodeVisitorFromNative(string $name, string $type): array
9393
{
9494
return [
9595
new \OpenCodeModeling\CodeAst\NodeVisitor\Property(
96-
new PropertyGenerator($name, $type, null, $this->typed)
96+
$this->propertyGenerator($name, $type)
9797
),
9898
];
9999
}
100100

101+
public function propertyGenerator(string $name, string $type): PropertyGenerator
102+
{
103+
return new PropertyGenerator($name, $type, null, $this->typed);
104+
}
105+
101106
/**
102107
* @param ReferenceType $type
103108
* @return array<NodeVisitor>

src/ValueObject/BooleanFactory.php

+35-15
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace OpenCodeModeling\JsonSchemaToPhpAst\ValueObject;
1212

13+
use OpenCodeModeling\CodeAst\Builder\ClassBuilder;
1314
use OpenCodeModeling\CodeAst\Code\BodyGenerator;
1415
use OpenCodeModeling\CodeAst\Code\MethodGenerator;
1516
use OpenCodeModeling\CodeAst\Code\ParameterGenerator;
@@ -79,23 +80,42 @@ public function nodeVisitors(BooleanType $typeDefinition): array
7980
return $this->nodeVisitorsFromNative($name);
8081
}
8182

83+
public function classBuilder(BooleanType $typeDefinition): ClassBuilder
84+
{
85+
$name = $typeDefinition->name() ?: 'boolean';
86+
87+
return $this->classBuilderFromNative($name)->setTyped($this->typed);
88+
}
89+
8290
/**
8391
* @param string $name
8492
* @return array<NodeVisitor>
8593
*/
8694
public function nodeVisitorsFromNative(string $name): array
8795
{
8896
$nodeVisitors = $this->propertyFactory->nodeVisitorFromNative($name, 'bool');
89-
$nodeVisitors[] = $this->methodFromBool($name);
90-
$nodeVisitors[] = $this->methodMagicConstruct($name);
91-
$nodeVisitors[] = $this->methodToBool($name);
92-
$nodeVisitors[] = $this->methodEquals($name);
93-
$nodeVisitors[] = $this->methodMagicToString($name);
97+
$nodeVisitors[] = new ClassMethod($this->methodFromBool($name));
98+
$nodeVisitors[] = new ClassMethod($this->methodMagicConstruct($name));
99+
$nodeVisitors[] = new ClassMethod($this->methodToBool($name));
100+
$nodeVisitors[] = new ClassMethod($this->methodEquals($name));
101+
$nodeVisitors[] = new ClassMethod($this->methodMagicToString($name));
94102

95103
return $nodeVisitors;
96104
}
97105

98-
public function methodFromBool(string $argumentName): NodeVisitor
106+
public function classBuilderFromNative(string $name): ClassBuilder
107+
{
108+
return ClassBuilder::fromNodes(
109+
$this->propertyFactory->propertyGenerator($name, 'bool')->generate(),
110+
$this->methodFromBool($name)->generate(),
111+
$this->methodMagicConstruct($name)->generate(),
112+
$this->methodToBool($name)->generate(),
113+
$this->methodEquals($name)->generate(),
114+
$this->methodMagicToString($name)->generate(),
115+
)->setTyped($this->typed);
116+
}
117+
118+
public function methodFromBool(string $argumentName): MethodGenerator
99119
{
100120
$method = new MethodGenerator(
101121
'fromBool',
@@ -108,10 +128,10 @@ public function methodFromBool(string $argumentName): NodeVisitor
108128
$method->setTyped($this->typed);
109129
$method->setReturnType('self');
110130

111-
return new ClassMethod($method);
131+
return $method;
112132
}
113133

114-
public function methodMagicConstruct(string $argumentName): NodeVisitor
134+
public function methodMagicConstruct(string $argumentName): MethodGenerator
115135
{
116136
$method = new MethodGenerator(
117137
'__construct',
@@ -123,10 +143,10 @@ public function methodMagicConstruct(string $argumentName): NodeVisitor
123143
);
124144
$method->setTyped($this->typed);
125145

126-
return new ClassMethod($method);
146+
return $method;
127147
}
128148

129-
public function methodToBool(string $argumentName): NodeVisitor
149+
public function methodToBool(string $argumentName): MethodGenerator
130150
{
131151
$method = new MethodGenerator(
132152
'toBool',
@@ -137,10 +157,10 @@ public function methodToBool(string $argumentName): NodeVisitor
137157
$method->setTyped($this->typed);
138158
$method->setReturnType('bool');
139159

140-
return new ClassMethod($method);
160+
return $method;
141161
}
142162

143-
public function methodEquals(string $propertyName, string $argumentName = 'other'): NodeVisitor
163+
public function methodEquals(string $propertyName, string $argumentName = 'other'): MethodGenerator
144164
{
145165
$body = <<<PHP
146166
if(!\$$argumentName instanceof self) {
@@ -161,10 +181,10 @@ public function methodEquals(string $propertyName, string $argumentName = 'other
161181
$method->setTyped($this->typed);
162182
$method->setReturnType('bool');
163183

164-
return new ClassMethod($method);
184+
return $method;
165185
}
166186

167-
public function methodMagicToString(string $argumentName): NodeVisitor
187+
public function methodMagicToString(string $argumentName): MethodGenerator
168188
{
169189
$method = new MethodGenerator(
170190
'__toString',
@@ -175,6 +195,6 @@ public function methodMagicToString(string $argumentName): NodeVisitor
175195
$method->setTyped($this->typed);
176196
$method->setReturnType('string');
177197

178-
return new ClassMethod($method);
198+
return $method;
179199
}
180200
}

src/ValueObject/DateTimeFactory.php

+54-26
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace OpenCodeModeling\JsonSchemaToPhpAst\ValueObject;
1212

13+
use OpenCodeModeling\CodeAst\Builder\ClassBuilder;
1314
use OpenCodeModeling\CodeAst\Code\BodyGenerator;
1415
use OpenCodeModeling\CodeAst\Code\ClassConstGenerator;
1516
use OpenCodeModeling\CodeAst\Code\IdentifierGenerator;
@@ -107,6 +108,13 @@ public function nodeVisitors(StringType $typeDefinition): array
107108
return $this->nodeVisitorsFromNative($name);
108109
}
109110

111+
public function classBuilder(StringType $typeDefinition): ClassBuilder
112+
{
113+
$name = $typeDefinition->name() ?: 'dateTime';
114+
115+
return $this->classBuilderFromNative($name)->setTyped($this->typed);
116+
}
117+
110118
/**
111119
* @param string $name
112120
* @param string $outputFormat
@@ -121,27 +129,47 @@ public function nodeVisitorsFromNative(string $name, string $outputFormat = DATE
121129
new ClassConstant(
122130
new IdentifierGenerator(
123131
'OUTPUT_FORMAT',
124-
new ClassConstGenerator(
125-
'OUTPUT_FORMAT',
126-
$outputFormat,
127-
ClassConstGenerator::FLAG_PRIVATE
128-
)
132+
$this->classConstant($outputFormat)
129133
)
130134
)
131135
);
132136

133-
$nodeVisitors[] = $this->methodFromDateTime($name);
134-
$nodeVisitors[] = $this->methodFromString($name);
135-
$nodeVisitors[] = $this->methodMagicConstruct($name);
136-
$nodeVisitors[] = $this->methodToString($name);
137-
$nodeVisitors[] = $this->methodDateTime($name);
138-
$nodeVisitors[] = $this->methodMagicToString();
139-
$nodeVisitors[] = $this->methodEnsureUtc($name);
137+
$nodeVisitors[] = new ClassMethod($this->methodFromDateTime($name));
138+
$nodeVisitors[] = new ClassMethod($this->methodFromString($name));
139+
$nodeVisitors[] = new ClassMethod($this->methodMagicConstruct($name));
140+
$nodeVisitors[] = new ClassMethod($this->methodToString($name));
141+
$nodeVisitors[] = new ClassMethod($this->methodDateTime($name));
142+
$nodeVisitors[] = new ClassMethod($this->methodMagicToString());
143+
$nodeVisitors[] = new ClassMethod($this->methodEnsureUtc($name));
140144

141145
return $nodeVisitors;
142146
}
143147

144-
public function methodFromDateTime(string $argumentName): NodeVisitor
148+
public function classBuilderFromNative(string $name, string $outputFormat = DATE_ATOM): ClassBuilder
149+
{
150+
return ClassBuilder::fromNodes(
151+
$this->classConstant($outputFormat)->generate(),
152+
$this->propertyFactory->propertyGenerator($name, 'DateTimeImmutable')->generate(),
153+
$this->methodFromDateTime($name)->generate(),
154+
$this->methodFromString($name)->generate(),
155+
$this->methodMagicConstruct($name)->generate(),
156+
$this->methodToString($name)->generate(),
157+
$this->methodDateTime($name)->generate(),
158+
$this->methodMagicToString()->generate(),
159+
$this->methodEnsureUtc($name)->generate(),
160+
)->setTyped($this->typed);
161+
}
162+
163+
public function classConstant(string $outputFormat = DATE_ATOM): ClassConstGenerator
164+
{
165+
return new ClassConstGenerator(
166+
'OUTPUT_FORMAT',
167+
$outputFormat,
168+
ClassConstGenerator::FLAG_PRIVATE
169+
);
170+
}
171+
172+
public function methodFromDateTime(string $argumentName): MethodGenerator
145173
{
146174
$method = new MethodGenerator(
147175
'fromDateTime',
@@ -154,10 +182,10 @@ public function methodFromDateTime(string $argumentName): NodeVisitor
154182
$method->setTyped($this->typed);
155183
$method->setReturnType('self');
156184

157-
return new ClassMethod($method);
185+
return $method;
158186
}
159187

160-
public function methodFromString(string $argumentName): NodeVisitor
188+
public function methodFromString(string $argumentName): MethodGenerator
161189
{
162190
$body = <<<PHP
163191
try {
@@ -182,10 +210,10 @@ public function methodFromString(string $argumentName): NodeVisitor
182210
$method->setTyped($this->typed);
183211
$method->setReturnType('self');
184212

185-
return new ClassMethod($method);
213+
return $method;
186214
}
187215

188-
public function methodMagicConstruct(string $argumentName): NodeVisitor
216+
public function methodMagicConstruct(string $argumentName): MethodGenerator
189217
{
190218
$method = new MethodGenerator(
191219
'__construct',
@@ -197,10 +225,10 @@ public function methodMagicConstruct(string $argumentName): NodeVisitor
197225
);
198226
$method->setTyped($this->typed);
199227

200-
return new ClassMethod($method);
228+
return $method;
201229
}
202230

203-
public function methodToString(string $argumentName): NodeVisitor
231+
public function methodToString(string $argumentName): MethodGenerator
204232
{
205233
$method = new MethodGenerator(
206234
'toString',
@@ -211,10 +239,10 @@ public function methodToString(string $argumentName): NodeVisitor
211239
$method->setTyped($this->typed);
212240
$method->setReturnType('string');
213241

214-
return new ClassMethod($method);
242+
return $method;
215243
}
216244

217-
public function methodDateTime(string $argumentName): NodeVisitor
245+
public function methodDateTime(string $argumentName): MethodGenerator
218246
{
219247
$method = new MethodGenerator(
220248
'dateTime',
@@ -225,10 +253,10 @@ public function methodDateTime(string $argumentName): NodeVisitor
225253
$method->setTyped($this->typed);
226254
$method->setReturnType('DateTimeImmutable');
227255

228-
return new ClassMethod($method);
256+
return $method;
229257
}
230258

231-
public function methodMagicToString(): NodeVisitor
259+
public function methodMagicToString(): MethodGenerator
232260
{
233261
$method = new MethodGenerator(
234262
'__toString',
@@ -239,10 +267,10 @@ public function methodMagicToString(): NodeVisitor
239267
$method->setTyped($this->typed);
240268
$method->setReturnType('string');
241269

242-
return new ClassMethod($method);
270+
return $method;
243271
}
244272

245-
public function methodEnsureUtc(string $argumentName): NodeVisitor
273+
public function methodEnsureUtc(string $argumentName): MethodGenerator
246274
{
247275
$body = <<<'PHP'
248276
if ($argumentName->getTimezone()->getName() !== 'UTC') {
@@ -264,6 +292,6 @@ public function methodEnsureUtc(string $argumentName): NodeVisitor
264292
$method->setTyped($this->typed);
265293
$method->setReturnType('DateTimeImmutable');
266294

267-
return new ClassMethod($method);
295+
return $method;
268296
}
269297
}

0 commit comments

Comments
 (0)