Skip to content

Commit

Permalink
Merge pull request #7 from tcampbPPU/feature/closure-type
Browse files Browse the repository at this point in the history
Use return type of closure
  • Loading branch information
acidjazz authored Mar 17, 2022
2 parents d7ed2b4 + 45430ed commit 35e9778
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/ModelInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@

namespace FumeApp\ModelTyper;


use Exception;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Opis\Closure\SerializableClosure;
use ReflectionClass;
use ReflectionMethod;
use ReflectionFunction;
use ReflectionException;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Types\Type;
Expand Down Expand Up @@ -286,19 +285,24 @@ public function getMutators(Model $model): array

// If Model is using v9 Attributes
if ($returnType == 'Illuminate\Database\Eloquent\Casts\Attribute') {

/* TODO: figure out a way to get the returnType from a closure
if ($reflection->name === 'isCaptain') {
$closure = $reflection->getClosure($model)->getReturnType();
ray($closure);
}
*/

// Check to see if the Model has Custom interfaces & has the mutator set with its type
if (isset($model->attrs) && isset($model->attrs[$mutator])) {
$mutations[$mutator] = $model->attrs[$mutator];
continue;
}

$closure = call_user_func($reflection->getClosure($model), 1);
if (! is_null($closure->get)) {
$rf = new ReflectionFunction($closure->get);
if ($rf->hasReturnType()) {
$returnType = $rf->getReturnType()->getName();
$mutations[$mutator] = $this->mapReturnType((string) $returnType);
continue;
}else {
// warn user to add return type to closure
throw new Exception('Unable to determine return type for ' . $mutator . ' Please add a return type to the get closure');
}
}
throw new Exception(
"Model for table {$model->getTable()} is using new mutator: {$mutator}. You must define them inside your models \$attrs array"
);
Expand Down

0 comments on commit 35e9778

Please sign in to comment.