Skip to content

Commit

Permalink
Merge pull request #75 from starter-dev/patch-1
Browse files Browse the repository at this point in the history
Fix: mutators not overridden by custom interfaces
  • Loading branch information
tcampbPPU authored Jul 8, 2024
2 parents 998c7af + 8571c36 commit cf7a095
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 cf7a095

Please sign in to comment.