Skip to content

Commit 59d8b45

Browse files
committed
Return hydrated objects
1 parent de6db2a commit 59d8b45

File tree

99 files changed

+1107
-195
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+1107
-195
lines changed

Tool/Generator.php

Lines changed: 191 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,13 @@
44

55
class Generator
66
{
7+
private array $arrayReturns = [];
8+
79
private string $definitionNamespace = '\\PHPFUI\\ConstantContact\\Definition';
810

911
private array $duplicateClasses = [
10-
'CustomFieldResource2' => 'CustomFieldResource',
11-
'ContactResource2' => 'ContactResource',
12-
'ContactList2' => 'ContactList',
13-
'Lin' => 'Link',
14-
'Link2' => 'Link',
15-
'Links' => 'Link',
16-
'PagingLinks2' => 'PagingLinks',
1712
'Results' => 'Result',
18-
'Tag2' => 'Tag',
19-
'Status' => 'Status',
20-
'Source' => 'Source',
13+
'ActivityGetExport' => 'ActivityExportStatus',
2114
];
2215

2316
private array $generatedClasses = [];
@@ -65,11 +58,9 @@ public function generateDefinition(string $namespacedClass, array $properties) :
6558
$parts = \explode('\\', $namespacedClass);
6659
$class = \array_pop($parts);
6760
$namespace = \implode('\\', $parts);
68-
$originalType = '';
6961

7062
if (! isset($properties['type']))
7163
{
72-
7364
if (\str_contains($namespacedClass, 'ResendToNonOpenersInput'))
7465
{
7566
$properties['type'] = 'object';
@@ -84,112 +75,12 @@ public function generateDefinition(string $namespacedClass, array $properties) :
8475

8576
if ('object' === $properties['type'])
8677
{
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'] ?? []);
19384
}
19485
}
19586

@@ -360,15 +251,126 @@ class ~class~ extends {$this->definitionNamespace}\Base
360251
$output .= "\n\t];\n";
361252
$template .= $output;
362253
}
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+
}
365264

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 = '$';
367275

368-
if (! \file_put_contents($path, $template))
276+
foreach ($properties as $name => $details)
277+
{
278+
if ('self' == $name)
369279
{
370-
throw new \Exception("Error writing file {$path}");
280+
$name = $class;
371281
}
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);
372374
}
373375

374376
private function getClassName(string $path) : string
@@ -491,6 +493,7 @@ private function writeClass(string $namespacedClass, string $apiPath, array $pro
491493
$enums = [];
492494
$csv = [];
493495
$parameters = [];
496+
$passedParameters = [];
494497
$docblock = '';
495498

496499
foreach ($specs['parameters'] ?? [] as $parameter)
@@ -516,6 +519,7 @@ private function writeClass(string $namespacedClass, string $apiPath, array $pro
516519
}
517520

518521
$description = $parameter['description'] ?? '';
522+
$passedParameters[] = "{$dollar}{$name}";
519523
$docblock .= "\n\t * @param {$type} {$dollar}{$name} {$description}";
520524
$required = $parameter['required'] ?? false;
521525
$parameterString = $required ? '' : '?';
@@ -536,7 +540,7 @@ private function writeClass(string $namespacedClass, string $apiPath, array $pro
536540
~description~
537541
*~docblock~
538542
*/
539-
public function ~method~(~parameters~) : ~returnType~
543+
public function ~method~(~parameters~) : array
540544
{
541545
~code~
542546
METHOD;
@@ -602,9 +606,70 @@ public function ~method~(~parameters~) : ~returnType~
602606
$code .= "\n";
603607
$summary = $this->formatDescription($specs['summary']);
604608
$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+
}
605670
$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)],
608673
$methodBody
609674
);
610675
$methods .= "\n";

phpunit.xml.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
colors="true"
55
processIsolation="false"
66
stopOnFailure="false"
7+
displayDetailsOnTestsThatTriggerWarnings="true"
8+
displayDetailsOnTestsThatTriggerDeprecations="true"
79
bootstrap="./Tests/bootstrap.php"
810
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.2/phpunit.xsd"
911
cacheDirectory=".phpunit.cache"

0 commit comments

Comments
 (0)