Skip to content

Commit c8d53a5

Browse files
Merge pull request #283 from DonnieRich/custom-cast-return-type
Added cast custom return type from get method
2 parents f33d057 + 13021fe commit c8d53a5

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

php-templates/models.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,27 @@ function getBuilderMethod($method, $factory)
5151
"parameters" => $params,
5252
"return" => $return,
5353
];
54-
};
54+
}
55+
56+
function getCastReturnType($className)
57+
{
58+
if ($className === null) {
59+
return null;
60+
}
61+
62+
try {
63+
$class = new \ReflectionClass($className);
64+
$method = $class->getMethod('get');
65+
66+
if ($method->hasReturnType()) {
67+
return $method->getReturnType()->getName();
68+
}
69+
70+
return $className;
71+
} catch (\Exception | \Throwable $e) {
72+
return $className;
73+
}
74+
}
5575

5676
function getModelInfo($className, $factory)
5777
{
@@ -91,6 +111,7 @@ function getModelInfo($className, $factory)
91111
->map(fn($attrs) => array_merge($attrs, [
92112
'title_case' => str_replace('_', '', \Illuminate\Support\Str::title($attrs['name'])),
93113
'documented' => $existingProperties->contains($attrs['name']),
114+
'cast' => getCastReturnType($attrs['cast'])
94115
]))
95116
->toArray();
96117

src/templates/models.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,27 @@ function getBuilderMethod($method, $factory)
5151
"parameters" => $params,
5252
"return" => $return,
5353
];
54-
};
54+
}
55+
56+
function getCastReturnType($className)
57+
{
58+
if ($className === null) {
59+
return null;
60+
}
61+
62+
try {
63+
$class = new \\ReflectionClass($className);
64+
$method = $class->getMethod('get');
65+
66+
if ($method->hasReturnType()) {
67+
return $method->getReturnType()->getName();
68+
}
69+
70+
return $className;
71+
} catch (\\Exception | \\Throwable $e) {
72+
return $className;
73+
}
74+
}
5575
5676
function getModelInfo($className, $factory)
5777
{
@@ -91,6 +111,7 @@ function getModelInfo($className, $factory)
91111
->map(fn($attrs) => array_merge($attrs, [
92112
'title_case' => str_replace('_', '', \\Illuminate\\Support\\Str::title($attrs['name'])),
93113
'documented' => $existingProperties->contains($attrs['name']),
114+
'cast' => getCastReturnType($attrs['cast'])
94115
]))
95116
->toArray();
96117

0 commit comments

Comments
 (0)