4
4
5
5
class Generator
6
6
{
7
+ private array $ arrayReturns = [];
8
+
7
9
private string $ definitionNamespace = '\\PHPFUI \\ConstantContact \\Definition ' ;
8
10
9
11
private array $ duplicateClasses = [
10
- 'CustomFieldResource2 ' => 'CustomFieldResource ' ,
11
- 'ContactResource2 ' => 'ContactResource ' ,
12
- 'ContactList2 ' => 'ContactList ' ,
13
- 'Lin ' => 'Link ' ,
14
- 'Link2 ' => 'Link ' ,
15
- 'Links ' => 'Link ' ,
16
- 'PagingLinks2 ' => 'PagingLinks ' ,
17
12
'Results ' => 'Result ' ,
18
- 'Tag2 ' => 'Tag ' ,
19
- 'Status ' => 'Status ' ,
20
- 'Source ' => 'Source ' ,
13
+ 'ActivityGetExport ' => 'ActivityExportStatus ' ,
21
14
];
22
15
23
16
private array $ generatedClasses = [];
@@ -65,11 +58,9 @@ public function generateDefinition(string $namespacedClass, array $properties) :
65
58
$ parts = \explode ('\\' , $ namespacedClass );
66
59
$ class = \array_pop ($ parts );
67
60
$ namespace = \implode ('\\' , $ parts );
68
- $ originalType = '' ;
69
61
70
62
if (! isset ($ properties ['type ' ]))
71
63
{
72
-
73
64
if (\str_contains ($ namespacedClass , 'ResendToNonOpenersInput ' ))
74
65
{
75
66
$ properties ['type ' ] = 'object ' ;
@@ -84,112 +75,12 @@ public function generateDefinition(string $namespacedClass, array $properties) :
84
75
85
76
if ('object ' === $ properties ['type ' ])
86
77
{
87
- $ fields = [];
88
- $ readonly = [];
89
- $ minLength = [];
90
- $ maxLength = [];
91
- $ docBlock = [];
92
- $ required = [];
93
- $ dollar = '$ ' ;
94
-
95
- foreach ($ properties ['properties ' ] ?? [] as $ name => $ details )
96
- {
97
- if ('self ' == $ name )
98
- {
99
- $ name = $ class ;
100
- }
101
-
102
- if (isset ($ details ['$ref ' ]))
103
- {
104
- $ docType = $ type = $ this ->getTypeNameFromRef ($ details ['$ref ' ]);
105
- }
106
- else
107
- {
108
- $ type = $ details ['type ' ];
109
-
110
- if ('object ' == $ type )
111
- {
112
- $ namespace = $ this ->definitionNamespace ;
113
- $ type = $ this ->getUniqueClassName ($ this ->definitionNamespace , $ name );
114
- $ this ->generateDefinition ($ type , $ details );
115
- }
116
-
117
- if (isset ($ details ['format ' ]))
118
- {
119
- $ type = $ details ['format ' ];
120
- }
121
-
122
- $ docType = $ type = $ this ->getPHPType ($ type );
123
-
124
- if (isset ($ details ['enum ' ]))
125
- {
126
- $ originalType = $ type ;
127
- $ type = $ details ['enum ' ];
128
- }
129
-
130
- if (isset ($ details ['items ' ]))
131
- {
132
- $ items = $ details ['items ' ];
133
- $ itemType = '' ;
134
-
135
- if (isset ($ items ['$ref ' ]))
136
- {
137
- $ itemType = $ this ->getTypeNameFromRef ($ items ['$ref ' ]);
138
- }
139
- elseif (isset ($ items ['type ' ]) && ($ items ['format ' ] ?? '' ) == 'uuid ' )
140
- {
141
- $ itemType = '\PHPFUI\ConstantContact\UUID ' ;
142
- }
143
-
144
- if ('array ' == $ type && $ itemType )
145
- {
146
- $ type = 'array< ' . $ itemType . '> ' ;
147
-
148
- if ($ details ['maxItems ' ] ?? false )
149
- {
150
- $ maxLength [$ name ] = (int )$ details ['maxItems ' ];
151
- }
152
-
153
- if ($ details ['minItems ' ] ?? false )
154
- {
155
- $ minLength [$ name ] = (int )$ details ['minItems ' ];
156
- }
157
- }
158
- }
159
-
160
- }
161
- $ fields [$ name ] = $ type ;
162
-
163
- if (isset ($ details ['minLength ' ]))
164
- {
165
- $ minLength [$ name ] = (int )$ details ['minLength ' ];
166
- }
167
-
168
- if (isset ($ details ['maxLength ' ]))
169
- {
170
- $ maxLength [$ name ] = (int )$ details ['maxLength ' ];
171
- }
172
-
173
- if (isset ($ details ['required ' ]))
174
- {
175
- $ required = $ details ['required ' ];
176
- }
177
-
178
- $ description = '' ;
179
-
180
- if (isset ($ details ['description ' ]))
181
- {
182
- $ description = $ this ->cleanDescription (\trim ($ details ['description ' ]));
183
- }
184
-
185
- if (\is_array ($ type ))
186
- {
187
- $ type = $ originalType ;
188
- }
189
- $ type = \str_replace ('\\\\' , '\\' , $ type );
190
- $ docBlock [] = \trim ("{$ type } {$ dollar }{$ name } {$ description }" );
191
- }
192
- $ this ->generateFromTemplate ($ class , ['fields ' => $ fields , 'maxLength ' => $ maxLength , 'minLength ' => $ minLength , 'requiredFields ' => $ required ], $ docBlock );
78
+ $ this ->generateObject ($ class , $ properties ['properties ' ] ?? []);
79
+ }
80
+ elseif ('array ' === $ properties ['type ' ])
81
+ {
82
+ $ this ->arrayReturns [$ class ] = 'array ' ;
83
+ $ this ->generateObject ($ class , $ properties ['items ' ]['properties ' ] ?? []);
193
84
}
194
85
}
195
86
@@ -360,15 +251,126 @@ class ~class~ extends {$this->definitionNamespace}\Base
360
251
$ output .= "\n\t]; \n" ;
361
252
$ template .= $ output ;
362
253
}
363
- $ template = \str_replace ('~class~ ' , $ name , $ template ) . "\t} \n" ;
364
- $ template = \str_replace ("/** {$ this ->nl } */ {$ this ->nl }" , '' , $ template );
254
+ $ template = \str_replace ('~class~ ' , $ name , $ template ) . "\t} \n" ;
255
+ $ template = \str_replace ("/** {$ this ->nl } */ {$ this ->nl }" , '' , $ template );
256
+
257
+ $ path = __DIR__ . "/../src/ConstantContact/Definition/ {$ name }.php " ;
258
+
259
+ if (! \file_put_contents ($ path , $ template ))
260
+ {
261
+ throw new \Exception ("Error writing file {$ path }" );
262
+ }
263
+ }
365
264
366
- $ path = __DIR__ . "/../src/ConstantContact/Definition/ {$ name }.php " ;
265
+ private function generateObject (string $ class , array $ properties ) : void
266
+ {
267
+ $ originalType = '' ;
268
+ $ fields = [];
269
+ $ readonly = [];
270
+ $ minLength = [];
271
+ $ maxLength = [];
272
+ $ docBlock = [];
273
+ $ required = [];
274
+ $ dollar = '$ ' ;
367
275
368
- if (! \file_put_contents ($ path , $ template ))
276
+ foreach ($ properties as $ name => $ details )
277
+ {
278
+ if ('self ' == $ name )
369
279
{
370
- throw new \ Exception ( " Error writing file { $ path }" ) ;
280
+ $ name = $ class ;
371
281
}
282
+
283
+ if (isset ($ details ['$ref ' ]))
284
+ {
285
+ $ docType = $ type = $ this ->getTypeNameFromRef ($ details ['$ref ' ]);
286
+ }
287
+ else
288
+ {
289
+ $ type = $ details ['type ' ];
290
+
291
+ if ('object ' == $ type )
292
+ {
293
+ $ namespace = $ this ->definitionNamespace ;
294
+ $ type = $ this ->getUniqueClassName ($ this ->definitionNamespace , $ name );
295
+ $ this ->generateDefinition ($ type , $ details );
296
+ }
297
+
298
+ if (isset ($ details ['format ' ]))
299
+ {
300
+ $ type = $ details ['format ' ];
301
+ }
302
+
303
+ $ docType = $ type = $ this ->getPHPType ($ type );
304
+
305
+ if (isset ($ details ['enum ' ]))
306
+ {
307
+ $ originalType = $ type ;
308
+ $ type = $ details ['enum ' ];
309
+ }
310
+
311
+ if (isset ($ details ['items ' ]))
312
+ {
313
+ $ items = $ details ['items ' ];
314
+ $ itemType = '' ;
315
+
316
+ if (isset ($ items ['$ref ' ]))
317
+ {
318
+ $ itemType = $ this ->getTypeNameFromRef ($ items ['$ref ' ]);
319
+ }
320
+ elseif (isset ($ items ['type ' ]) && ($ items ['format ' ] ?? '' ) == 'uuid ' )
321
+ {
322
+ $ itemType = '\PHPFUI\ConstantContact\UUID ' ;
323
+ }
324
+
325
+ if ('array ' == $ type && $ itemType )
326
+ {
327
+ $ type = 'array< ' . $ itemType . '> ' ;
328
+
329
+ if ($ details ['maxItems ' ] ?? false )
330
+ {
331
+ $ maxLength [$ name ] = (int )$ details ['maxItems ' ];
332
+ }
333
+
334
+ if ($ details ['minItems ' ] ?? false )
335
+ {
336
+ $ minLength [$ name ] = (int )$ details ['minItems ' ];
337
+ }
338
+ }
339
+ }
340
+
341
+ }
342
+ $ fields [$ name ] = $ type ;
343
+
344
+ if (isset ($ details ['minLength ' ]))
345
+ {
346
+ $ minLength [$ name ] = (int )$ details ['minLength ' ];
347
+ }
348
+
349
+ if (isset ($ details ['maxLength ' ]))
350
+ {
351
+ $ maxLength [$ name ] = (int )$ details ['maxLength ' ];
352
+ }
353
+
354
+ if (isset ($ details ['required ' ]))
355
+ {
356
+ $ required = $ details ['required ' ];
357
+ }
358
+
359
+ $ description = '' ;
360
+
361
+ if (isset ($ details ['description ' ]))
362
+ {
363
+ $ description = $ this ->cleanDescription (\trim ($ details ['description ' ]));
364
+ }
365
+
366
+ if (\is_array ($ type ))
367
+ {
368
+ $ type = $ originalType ;
369
+ }
370
+ $ type = \str_replace ('\\\\' , '\\' , $ type );
371
+ $ docBlock [] = \trim ("{$ type } {$ dollar }{$ name } {$ description }" );
372
+ }
373
+ $ this ->generateFromTemplate ($ class , ['fields ' => $ fields , 'maxLength ' => $ maxLength , 'minLength ' => $ minLength , 'requiredFields ' => $ required ], $ docBlock );
372
374
}
373
375
374
376
private function getClassName (string $ path ) : string
@@ -491,6 +493,7 @@ private function writeClass(string $namespacedClass, string $apiPath, array $pro
491
493
$ enums = [];
492
494
$ csv = [];
493
495
$ parameters = [];
496
+ $ passedParameters = [];
494
497
$ docblock = '' ;
495
498
496
499
foreach ($ specs ['parameters ' ] ?? [] as $ parameter )
@@ -516,6 +519,7 @@ private function writeClass(string $namespacedClass, string $apiPath, array $pro
516
519
}
517
520
518
521
$ description = $ parameter ['description ' ] ?? '' ;
522
+ $ passedParameters [] = "{$ dollar }{$ name }" ;
519
523
$ docblock .= "\n\t * @param {$ type } {$ dollar }{$ name } {$ description }" ;
520
524
$ required = $ parameter ['required ' ] ?? false ;
521
525
$ parameterString = $ required ? '' : '? ' ;
@@ -536,7 +540,7 @@ private function writeClass(string $namespacedClass, string $apiPath, array $pro
536
540
~description~
537
541
*~docblock~
538
542
*/
539
- public function ~method~(~parameters~) : ~returnType~
543
+ public function ~method~(~parameters~) : array
540
544
{
541
545
~code~
542
546
METHOD;
@@ -602,9 +606,70 @@ public function ~method~(~parameters~) : ~returnType~
602
606
$ code .= "\n" ;
603
607
$ summary = $ this ->formatDescription ($ specs ['summary ' ]);
604
608
$ description = $ this ->formatDescription ($ specs ['description ' ]);
609
+
610
+ // add in ReturnSchema methods
611
+ foreach ($ specs ['responses ' ] ?? [] as $ returnCode => $ parameter )
612
+ {
613
+ $ returnCode = (int )$ returnCode ;
614
+
615
+ if ($ returnCode >= 200 && $ returnCode <= 299 && isset ($ parameter ['schema ' ]['$ref ' ]))
616
+ {
617
+ $ baseName = \str_replace ('#/definitions/ ' , '' , $ parameter ['schema ' ]['$ref ' ]);
618
+ $ schema = $ this ->getUniqueClassName ($ this ->definitionNamespace , $ baseName );
619
+ $ returnType = $ this ->arrayReturns [$ baseName ] ?? '' ;
620
+
621
+ if ('array ' === $ returnType )
622
+ {
623
+ $ returnSchema = <<<SCHEMA
624
+
625
+ /**
626
+ * @return array< {$ schema }>
627
+ */
628
+ public function ~method~ReturnSchema(~parameters~) : array
629
+ {
630
+ {$ dollar }array = [];
631
+ foreach ( {$ dollar }this->~method~(~passedParameters~) as {$ dollar }object)
632
+ {
633
+ {$ dollar }array[] = new {$ schema }( {$ dollar }object);
634
+ }
635
+
636
+ return {$ dollar }array;
637
+ }
638
+
639
+ SCHEMA ;
640
+ }
641
+ // else if ('string' === $returnType)
642
+ // {
643
+ // $returnSchema = <<<SCHEMA
644
+ //
645
+ //
646
+ // public function ~method~ReturnSchema(~parameters~) : string
647
+ // {
648
+ // return {$dollar}this->~method~(~passedParameters~);
649
+ // }
650
+ //
651
+ //SCHEMA;
652
+ // }
653
+ else
654
+ {
655
+ $ returnSchema = <<<SCHEMA
656
+
657
+
658
+ public function ~method~ReturnSchema(~parameters~) : {$ schema }
659
+ {
660
+ return new {$ schema }( {$ dollar }this->~method~(~passedParameters~));
661
+ }
662
+
663
+ SCHEMA ;
664
+ }
665
+ $ methodBody .= $ returnSchema ;
666
+
667
+ break ; // just generate the first one found
668
+ }
669
+ }
605
670
$ methods .= \str_replace (
606
- ['~method~ ' , '~parameters~ ' , '~docblock~ ' , '~summary~ ' , '~description~ ' , '~code~ ' , '~returnType ~ ' ],
607
- [$ method , \implode (', ' , $ parameters ), $ docblock , $ summary , $ description , $ code , ' delete ' != $ method ? ' array ' : ' bool ' ],
671
+ ['~method~ ' , '~parameters~ ' , '~docblock~ ' , '~summary~ ' , '~description~ ' , '~code~ ' , '~passedParameters ~ ' ],
672
+ [$ method , \implode (', ' , $ parameters ), $ docblock , $ summary , $ description , $ code , \implode ( ' , ' , $ passedParameters ) ],
608
673
$ methodBody
609
674
);
610
675
$ methods .= "\n" ;
0 commit comments