Skip to content

Commit

Permalink
Fix: mutators not overridden by custom interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
starter-dev committed Jul 8, 2024
1 parent 998c7af commit 8571c36
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/Actions/BuildModelDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
});
}
}

0 comments on commit 8571c36

Please sign in to comment.