Skip to content

Commit 504ca9b

Browse files
committed
Add model type and property type generic filters
1 parent 3fac784 commit 504ca9b

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/SDK/Language/Kotlin.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,12 @@ public function getFilters(): array
447447
new TwigFilter('returnType', function (array $method, array $spec, string $namespace, string $generic = 'T') {
448448
return $this->getReturnType($method, $spec, $namespace, $generic);
449449
}),
450+
new TwigFilter('modelType', function (array $property, array $spec, string $generic = 'T') {
451+
return $this->getModelType($property, $spec, $generic);
452+
}),
453+
new TwigFilter('propertyType', function (array $property, array $spec, string $generic = 'T') {
454+
return $this->getPropertyType($property, $spec, $generic);
455+
}),
450456
new TwigFilter('hasGenericType', function (string $model, array $spec) {
451457
return $this->hasGenericType($model, $spec);
452458
}),
@@ -477,6 +483,37 @@ protected function getReturnType(array $method, array $spec, string $namespace,
477483
return $namespace . '.models.' . $ret;
478484
}
479485

486+
protected function getModelType(array $definition, array $spec, string $generic = 'T'): string
487+
{
488+
if ($this->hasGenericType($definition['name'], $spec)) {
489+
return $this->toUpperCase($definition['name']) . '<' . $generic . '>';
490+
}
491+
return $this->toUpperCase($definition['name']);
492+
}
493+
494+
protected function getPropertyType(array $property, array $spec, string $generic = 'T'): string
495+
{
496+
if (\array_key_exists('sub_schema', $property)) {
497+
$type = $this->toUpperCase($property['sub_schema']);
498+
499+
if ($this->hasGenericType($property['sub_schema'], $spec)) {
500+
$type .= '<' . $generic . '>';
501+
}
502+
503+
if ($property['type'] === 'array') {
504+
$type = 'List<' . $type . '>';
505+
}
506+
} else {
507+
$type = $this->getTypeName($property);
508+
}
509+
510+
if (!$property['required']) {
511+
$type .= '?';
512+
}
513+
514+
return $type;
515+
}
516+
480517
protected function hasGenericType(?string $model, array $spec): string
481518
{
482519
if (empty($model)) {

0 commit comments

Comments
 (0)