From 8571c36af5dd640fa7a76c4b89f4a0757ac8ef09 Mon Sep 17 00:00:00 2001 From: starter-dev Date: Tue, 9 Jul 2024 02:02:27 +0800 Subject: [PATCH] Fix: mutators not overridden by custom interfaces --- src/Actions/BuildModelDetails.php | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/Actions/BuildModelDetails.php b/src/Actions/BuildModelDetails.php index ed01488..cc680d6 100644 --- a/src/Actions/BuildModelDetails.php +++ b/src/Actions/BuildModelDetails.php @@ -6,6 +6,7 @@ use FumeApp\ModelTyper\Exceptions\NestedCommandException; use FumeApp\ModelTyper\Traits\ClassBaseName; use FumeApp\ModelTyper\Traits\ModelRefClass; +use Illuminate\Support\Collection; use ReflectionException; use Symfony\Component\Finder\SplFileInfo; @@ -61,18 +62,12 @@ public function __invoke(SplFileInfo $modelFile, bool $resolveAbstract = false): ->unique() ->values(); - $columns = $columns->map(function ($column) use (&$interfaces) { - $interfaces->each(function ($interface, $key) use (&$column, &$interfaces) { - if ($key === $column['name']) { - $column['type'] = $interface['type']; - $column['forceType'] = true; + // Override all columns, mutators and relationships with custom interfaces + $columns = $this->overrideCollectionWithInterfaces($columns, $interfaces); - $interfaces->forget($key); - } - }); + $nonColumns = $this->overrideCollectionWithInterfaces($nonColumns, $interfaces); - return $column; - }); + $relations = $this->overrideCollectionWithInterfaces($relations, $interfaces); return [ 'reflectionModel' => $reflectionModel, @@ -105,4 +100,20 @@ private function getModelDetails(SplFileInfo $modelFile, bool $resolveAbstract): throw $exception; } } + + private function overrideCollectionWithInterfaces(Collection $columns, Collection $interfaces): Collection + { + return $columns->map(function ($column) use ($interfaces) { + $interfaces->each(function ($interface, $key) use (&$column, $interfaces) { + if ($key === $column['name']) { + $column['type'] = $interface['type']; + $column['forceType'] = true; + + $interfaces->forget($key); + } + }); + + return $column; + }); + } }