10
10
11
11
namespace OpenCodeModeling \JsonSchemaToPhpAst \ValueObject ;
12
12
13
+ use OpenCodeModeling \CodeAst \Builder \ClassBuilder ;
13
14
use OpenCodeModeling \CodeAst \Code \BodyGenerator ;
14
15
use OpenCodeModeling \CodeAst \Code \ClassConstGenerator ;
15
16
use OpenCodeModeling \CodeAst \Code \IdentifierGenerator ;
@@ -107,6 +108,13 @@ public function nodeVisitors(StringType $typeDefinition): array
107
108
return $ this ->nodeVisitorsFromNative ($ name );
108
109
}
109
110
111
+ public function classBuilder (StringType $ typeDefinition ): ClassBuilder
112
+ {
113
+ $ name = $ typeDefinition ->name () ?: 'dateTime ' ;
114
+
115
+ return $ this ->classBuilderFromNative ($ name )->setTyped ($ this ->typed );
116
+ }
117
+
110
118
/**
111
119
* @param string $name
112
120
* @param string $outputFormat
@@ -121,27 +129,47 @@ public function nodeVisitorsFromNative(string $name, string $outputFormat = DATE
121
129
new ClassConstant (
122
130
new IdentifierGenerator (
123
131
'OUTPUT_FORMAT ' ,
124
- new ClassConstGenerator (
125
- 'OUTPUT_FORMAT ' ,
126
- $ outputFormat ,
127
- ClassConstGenerator::FLAG_PRIVATE
128
- )
132
+ $ this ->classConstant ($ outputFormat )
129
133
)
130
134
)
131
135
);
132
136
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) );
140
144
141
145
return $ nodeVisitors ;
142
146
}
143
147
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
145
173
{
146
174
$ method = new MethodGenerator (
147
175
'fromDateTime ' ,
@@ -154,10 +182,10 @@ public function methodFromDateTime(string $argumentName): NodeVisitor
154
182
$ method ->setTyped ($ this ->typed );
155
183
$ method ->setReturnType ('self ' );
156
184
157
- return new ClassMethod ( $ method) ;
185
+ return $ method ;
158
186
}
159
187
160
- public function methodFromString (string $ argumentName ): NodeVisitor
188
+ public function methodFromString (string $ argumentName ): MethodGenerator
161
189
{
162
190
$ body = <<<PHP
163
191
try {
@@ -182,10 +210,10 @@ public function methodFromString(string $argumentName): NodeVisitor
182
210
$ method ->setTyped ($ this ->typed );
183
211
$ method ->setReturnType ('self ' );
184
212
185
- return new ClassMethod ( $ method) ;
213
+ return $ method ;
186
214
}
187
215
188
- public function methodMagicConstruct (string $ argumentName ): NodeVisitor
216
+ public function methodMagicConstruct (string $ argumentName ): MethodGenerator
189
217
{
190
218
$ method = new MethodGenerator (
191
219
'__construct ' ,
@@ -197,10 +225,10 @@ public function methodMagicConstruct(string $argumentName): NodeVisitor
197
225
);
198
226
$ method ->setTyped ($ this ->typed );
199
227
200
- return new ClassMethod ( $ method) ;
228
+ return $ method ;
201
229
}
202
230
203
- public function methodToString (string $ argumentName ): NodeVisitor
231
+ public function methodToString (string $ argumentName ): MethodGenerator
204
232
{
205
233
$ method = new MethodGenerator (
206
234
'toString ' ,
@@ -211,10 +239,10 @@ public function methodToString(string $argumentName): NodeVisitor
211
239
$ method ->setTyped ($ this ->typed );
212
240
$ method ->setReturnType ('string ' );
213
241
214
- return new ClassMethod ( $ method) ;
242
+ return $ method ;
215
243
}
216
244
217
- public function methodDateTime (string $ argumentName ): NodeVisitor
245
+ public function methodDateTime (string $ argumentName ): MethodGenerator
218
246
{
219
247
$ method = new MethodGenerator (
220
248
'dateTime ' ,
@@ -225,10 +253,10 @@ public function methodDateTime(string $argumentName): NodeVisitor
225
253
$ method ->setTyped ($ this ->typed );
226
254
$ method ->setReturnType ('DateTimeImmutable ' );
227
255
228
- return new ClassMethod ( $ method) ;
256
+ return $ method ;
229
257
}
230
258
231
- public function methodMagicToString (): NodeVisitor
259
+ public function methodMagicToString (): MethodGenerator
232
260
{
233
261
$ method = new MethodGenerator (
234
262
'__toString ' ,
@@ -239,10 +267,10 @@ public function methodMagicToString(): NodeVisitor
239
267
$ method ->setTyped ($ this ->typed );
240
268
$ method ->setReturnType ('string ' );
241
269
242
- return new ClassMethod ( $ method) ;
270
+ return $ method ;
243
271
}
244
272
245
- public function methodEnsureUtc (string $ argumentName ): NodeVisitor
273
+ public function methodEnsureUtc (string $ argumentName ): MethodGenerator
246
274
{
247
275
$ body = <<<'PHP'
248
276
if ($argumentName->getTimezone()->getName() !== 'UTC') {
@@ -264,6 +292,6 @@ public function methodEnsureUtc(string $argumentName): NodeVisitor
264
292
$ method ->setTyped ($ this ->typed );
265
293
$ method ->setReturnType ('DateTimeImmutable ' );
266
294
267
- return new ClassMethod ( $ method) ;
295
+ return $ method ;
268
296
}
269
297
}
0 commit comments