1010
1111namespace OpenCodeModeling \JsonSchemaToPhpAst \ValueObject ;
1212
13+ use OpenCodeModeling \CodeAst \Builder \ClassBuilder ;
1314use OpenCodeModeling \CodeAst \Code \BodyGenerator ;
1415use OpenCodeModeling \CodeAst \Code \ClassConstGenerator ;
1516use 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