From df1e935f9c5ee1957e9b4267b570b5762a4c22eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ewilan=20Rivi=C3=A8re?= Date: Sun, 18 Jun 2023 13:27:07 +0200 Subject: [PATCH] add `isRelationMorph` --- README.md | 4 ++-- composer.json | 2 +- src/Typed/Eloquent/EloquentItem.php | 1 + src/Typed/Eloquent/Utils/EloquentProperty.php | 1 + src/Typed/Eloquent/Utils/EloquentRelation.php | 7 +++++++ 5 files changed, 12 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6e99127..929a031 100644 --- a/README.md +++ b/README.md @@ -298,8 +298,8 @@ composer test ## Roadmap -- [ ] 90% coverage -- [ ] Generate TS types for `morphTo` +- [x] 90% coverage +- [x] Generate TS types for `morphTo` - [ ] Use `appends` to define type for `Casts\Attribute` methods ## Changelog diff --git a/composer.json b/composer.json index 3847e04..7f24571 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "kiwilan/typescriptable-laravel", "description": "PHP package for Laravel to type Eloquent models and routes with autogenerated TypeScript, ready for Inertia with associated NPM package.", - "version": "1.3.0", + "version": "1.4.0", "keywords": [ "kiwilan", "laravel", diff --git a/src/Typed/Eloquent/EloquentItem.php b/src/Typed/Eloquent/EloquentItem.php index ffe8daf..9006470 100644 --- a/src/Typed/Eloquent/EloquentItem.php +++ b/src/Typed/Eloquent/EloquentItem.php @@ -128,6 +128,7 @@ private function setProperties(): array type: $relation->type, typeTs: $relation->typeTs, isRelation: true, + isRelationMorph: $relation->isMorph, isNullable: true, isArray: $relation->isArray, ); diff --git a/src/Typed/Eloquent/Utils/EloquentProperty.php b/src/Typed/Eloquent/Utils/EloquentProperty.php index c83b595..b7a6520 100644 --- a/src/Typed/Eloquent/Utils/EloquentProperty.php +++ b/src/Typed/Eloquent/Utils/EloquentProperty.php @@ -16,6 +16,7 @@ public function __construct( public bool $isHidden = false, public bool $isEnum = false, public bool $isRelation = false, + public bool $isRelationMorph = false, public bool $isArray = false, public bool $isAttribute = false, public bool $isCount = false, diff --git a/src/Typed/Eloquent/Utils/EloquentRelation.php b/src/Typed/Eloquent/Utils/EloquentRelation.php index 821f5c9..b98eb5e 100644 --- a/src/Typed/Eloquent/Utils/EloquentRelation.php +++ b/src/Typed/Eloquent/Utils/EloquentRelation.php @@ -11,6 +11,7 @@ public function __construct( public string $model, public string $field, public bool $isArray = false, + public bool $isMorph = false, public ?string $type = null, public ?string $typeTs = null, ) { @@ -44,6 +45,7 @@ private static function make(ReflectionMethod $method): self model: $method->getDeclaringClass()->getName(), field: $method->getName(), isArray: str_contains($method->getReturnType(), 'Many'), + isMorph: str_contains($method->getReturnType(), 'Morph'), ); $return_line = $method->getEndLine() - 2; @@ -62,6 +64,11 @@ private static function make(ReflectionMethod $method): self ? "{$relation->type}[]" : $relation->type; + if ($relation->isMorph && ! $relation->type) { + $relation->type = 'mixed'; + $relation->typeTs = 'any'; + } + return $relation; } }