1
1
<?php
2
2
3
- namespace Tools ;
3
+ namespace Tool ;
4
4
5
5
class Generator
6
6
{
7
7
private string $ rootPath = 'src/ConstantContact/ ' ;
8
8
9
- private string $ definitionNamespace = 'PHPFUI \\ConstantContact \\Definition ' ;
9
+ private string $ definitionNamespace = '\\ PHPFUI \\ConstantContact \\Definition ' ;
10
10
11
11
private string $ nl ;
12
12
13
13
private array $ duplicateClasses = [
14
14
'CustomFieldResource2 ' => 'CustomFieldResource ' ,
15
15
'ContactResource2 ' => 'ContactResource ' ,
16
16
'ContactList2 ' => 'ContactList ' ,
17
+ 'Lin ' => 'Link ' ,
18
+ 'Link2 ' => 'Link ' ,
19
+ 'Links ' => 'Link ' ,
20
+ 'PagingLinks2 ' => 'PagingLinks ' ,
21
+ 'Results ' => 'Result ' ,
17
22
'Tag2 ' => 'Tag ' ,
18
23
'Status ' => 'Status ' ,
19
24
'Source ' => 'Source ' ,
@@ -26,6 +31,11 @@ public function __construct()
26
31
$ this ->nl = 'WIN ' === \strtoupper (\substr (PHP_OS , 0 , 3 )) ? "\r\n" : "\n" ;
27
32
}
28
33
34
+ public function deleteClasses (string $ version ) : void
35
+ {
36
+ $ this ->deleteFileTree ($ version );
37
+ }
38
+
29
39
public function makeClasses (string $ version , array $ paths ) : void
30
40
{
31
41
\ksort ($ paths );
@@ -50,21 +60,30 @@ public function generateClass(string $version, string $path, array $properties)
50
60
\mkdir ($ dir , recursive: true );
51
61
}
52
62
53
- $ class = $ this ->getUniqueClassName ($ namespace , $ class );
63
+ $ namespacedClass = $ this ->getUniqueClassName ($ namespace , $ class );
64
+
65
+ $ this ->writeClass ($ namespacedClass , $ apiPath , $ properties );
66
+ }
54
67
55
- $ this ->writeClass ($ namespace , $ class , $ apiPath , $ properties );
68
+ public function deleteDefinitions () : void
69
+ {
70
+ $ this ->deleteFileTree ('/Definition ' );
56
71
}
57
72
58
73
public function makeDefinitions (array $ definitions ) : void
59
74
{
60
75
foreach ($ definitions as $ class => $ properties )
61
76
{
62
- $ this ->generateDefinition ($ class , $ properties );
77
+ $ this ->generateDefinition ($ this -> getUniqueClassName ( $ this -> definitionNamespace , $ class) , $ properties );
63
78
}
64
79
}
65
80
66
- public function generateDefinition (string $ class , array $ properties ) : void
81
+ public function generateDefinition (string $ namespacedClass , array $ properties ) : void
67
82
{
83
+ $ parts = explode ('\\' , $ namespacedClass );
84
+ $ class = array_pop ($ parts );
85
+ $ namespace = implode ('\\' , $ parts );
86
+
68
87
if (! isset ($ properties ['type ' ]))
69
88
{
70
89
return ;
@@ -99,9 +118,8 @@ public function generateDefinition(string $class, array $properties) : void
99
118
if ('object ' == $ type )
100
119
{
101
120
$ namespace = $ this ->definitionNamespace ;
102
- $ baseName = $ this ->getUniqueClassName ($ namespace , $ this ->getClassName ($ name ));
103
- $ type = '\\' . $ namespace . '\\' . $ baseName ;
104
- $ this ->generateDefinition ($ baseName , $ details );
121
+ $ type = $ this ->getUniqueClassName ($ this ->definitionNamespace , $ name );
122
+ $ this ->generateDefinition ($ type , $ details );
105
123
}
106
124
107
125
if (isset ($ details ['format ' ]))
@@ -160,60 +178,69 @@ public function generateDefinition(string $class, array $properties) : void
160
178
$ maxLength [$ name ] = (int )$ details ['maxLength ' ];
161
179
}
162
180
181
+ $ description = '' ;
163
182
if (isset ($ details ['description ' ]))
164
183
{
165
184
$ description = $ this ->cleanDescription (\trim ($ details ['description ' ]));
185
+ }
166
186
167
- if (\is_array ($ type ))
168
- {
169
- $ type = $ originalType ;
170
- }
171
- $ type = \str_replace ('\\\\' , '\\' , $ type );
172
- $ docBlock [] = "{$ type } {$ dollar }{$ name } {$ description }" ;
187
+ if (\is_array ($ type ))
188
+ {
189
+ $ type = $ originalType ;
173
190
}
191
+ $ type = \str_replace ('\\\\' , '\\' , $ type );
192
+ $ docBlock [] = trim ("{$ type } {$ dollar }{$ name } {$ description }" );
174
193
}
175
194
$ this ->generateFromTemplate ($ class , ['fields ' => $ fields , 'minLength ' => $ minLength , 'maxLength ' => $ maxLength , ], $ docBlock );
176
195
}
177
196
}
178
197
198
+ /**
199
+ * Given a namespace and a class in the namespace, get a unique name that combines duplicate classes
200
+ *
201
+ * @return string fully namespaced class name
202
+ */
179
203
private function getUniqueClassName (string $ namespace , string $ class ) : string
180
204
{
205
+ $ namespace = trim ($ namespace , '\\' );
206
+ $ class = $ this ->getClassName ($ class );
181
207
if (isset ($ this ->duplicateClasses [$ class ]))
182
208
{
183
- return $ this ->duplicateClasses [$ class ];
209
+ $ class = $ this ->duplicateClasses [$ class ];
210
+ $ fullName = '\\' . $ namespace . '\\' . $ class ;
211
+ $ this ->generatedClasses [$ fullName ] = true ;
212
+
213
+ return $ fullName ;
184
214
}
185
215
186
- $ fullName = $ namespace . '\\' . $ class ;
216
+ $ fullName = '\\' . $ namespace . '\\' . $ class ;
187
217
188
- if (isset ($ this ->generatedClasses [$ fullName ]))
218
+ // if we have seen this class before, then it is the plural version and it should be singular because CC does not know how to design an API (among other things).
219
+ if (! str_contains ($ fullName , 'Definition ' ) && isset ($ this ->generatedClasses [$ fullName ]))
189
220
{
190
- if ('Links ' == $ class )
191
- {
192
- $ class = 'Link ' ;
193
- }
194
- elseif ('StreetAddress ' == $ class )
195
- {
196
- $ class = 'StreetAddressRecord ' ;
197
- }
198
- // trim the s off the end point for the singular
199
- elseif (\str_ends_with ($ class , 'ies ' ))
221
+ if (\str_ends_with ($ class , 'ies ' ))
200
222
{
201
223
$ class = \substr ($ class , 0 , \strlen ($ class ) - 3 );
202
224
$ class .= 'y ' ;
203
225
}
204
- else
226
+ else // trim the s off the end point for the singular
205
227
{
206
228
$ class = \substr ($ class , 0 , \strlen ($ class ) - 1 );
207
229
}
208
- $ fullName = $ namespace . '\\' . $ class ;
230
+ $ fullName = '\\' . $ namespace . '\\' . $ class ;
209
231
}
232
+
210
233
$ this ->generatedClasses [$ fullName ] = true ;
211
234
212
- return $ class ;
235
+ return $ fullName ;
213
236
}
214
237
215
- private function writeClass (string $ namespace , string $ class , string $ apiPath , array $ properties ) : void
238
+ private function writeClass (string $ namespacedClass , string $ apiPath , array $ properties ) : void
216
239
{
240
+ $ parts = explode ('\\' , $ namespacedClass );
241
+ $ class = array_pop ($ parts );
242
+ $ namespace = trim (implode ('\\' , $ parts ), '\\' );
243
+
217
244
$ methods = '' ;
218
245
$ dollar = '$ ' ;
219
246
@@ -379,7 +406,7 @@ private function getPHPType(string $type) : string
379
406
}
380
407
elseif ('date-time ' == $ type )
381
408
{
382
- $ type = 'DateTime ' ;
409
+ $ type = '\PHPFUI\ConstantContact\ DateTime ' ;
383
410
}
384
411
elseif ('date ' == $ type )
385
412
{
@@ -462,15 +489,23 @@ private function formatDescription(string $description) : string
462
489
return \implode ("\n" , $ blocks );
463
490
}
464
491
492
+ /**
493
+ * Generate a definition from a template
494
+ *
495
+ * @param string $name of the class, no namespace
496
+ * @param array $properties from YAML file
497
+ * @param array $docBlocks @var docblocks to output
498
+ */
465
499
private function generateFromTemplate (string $ name , array $ properties , array $ docBlocks ) : void
466
500
{
467
- $ backSlash = '\\' ;
501
+ $ namespace = trim ($ this ->definitionNamespace , '\\' );
502
+
468
503
$ template = <<<PHP
469
504
<?php
470
505
471
506
// Generated file. Do not edit by hand. Use update.php in project root.
472
507
473
- namespace {$ this -> definitionNamespace };
508
+ namespace {$ namespace };
474
509
475
510
/**
476
511
@@ -483,7 +518,7 @@ private function generateFromTemplate(string $name, array $properties, array $do
483
518
}
484
519
485
520
$ template .= " */
486
- class ~class~ extends {$ backSlash }{ $ this ->definitionNamespace }\Base
521
+ class ~class~ extends {$ this ->definitionNamespace }\Base
487
522
{ " ;
488
523
489
524
foreach ($ properties as $ fields => $ values )
@@ -544,6 +579,29 @@ private function getTypeNameFromRef(string $ref) : string
544
579
{
545
580
$ parts = \explode ('/ ' , \str_replace ('_ ' , '' , $ ref ));
546
581
547
- return '\\' . $ this ->definitionNamespace . '\\' . \array_pop ($ parts );
582
+ return $ this ->getUniqueClassName ($ this ->definitionNamespace , \array_pop ($ parts ));
583
+ }
584
+
585
+ private function deleteFileTree (string $ path ) : void
586
+ {
587
+ $ directory = __DIR__ . '/../src/ConstantContact ' . $ path ;
588
+
589
+ $ iterator = new \RecursiveIteratorIterator (
590
+ new \RecursiveDirectoryIterator ($ directory , \RecursiveDirectoryIterator::SKIP_DOTS ),
591
+ \RecursiveIteratorIterator::SELF_FIRST );
592
+
593
+ foreach ($ iterator as $ item )
594
+ {
595
+ if (! $ item ->isDir ())
596
+ {
597
+ $ fileName = "{$ item }" ;
598
+ // don't delete base classes
599
+ if (! str_ends_with ($ fileName , 'Base.php ' ))
600
+ {
601
+ unlink ($ fileName );
602
+ }
603
+ }
604
+ }
548
605
}
606
+
549
607
}
0 commit comments