Skip to content

Commit

Permalink
Merge pull request #3 from AndreSchwarzer/main-1
Browse files Browse the repository at this point in the history
EnumFilter: Display the value returned by the method defined in "property"
  • Loading branch information
trippo committed Aug 16, 2023
2 parents e44d000 + 55639aa commit 60e9c3f
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Traits/EnumFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,23 @@ public function name($name = null)

public function options(Request $request): array
{
$key = (is_subclass_of($this->class, BackedEnum::class)) ? 'value' : 'name';

if (method_exists($this->class, 'dynamicByKey')) {
try {
return array_flip($this->class::dynamicByKey('value', $this->property, $this->cases));
} catch (TranslationMissing $e) {
throw $e;
} catch (\Exception) {
}
} elseif (method_exists($this->class, $this->property)) {
return collect(call_user_func([$this->class, 'cases']))
->mapWithKeys(fn($case) => [
$case->{$this->property}() => $case->{$key},
])
->toArray();
}

$key = (is_subclass_of($this->class, BackedEnum::class)) ? 'value' : 'name';

return collect(call_user_func([$this->class, 'cases']))->pluck($key, 'name')->toArray();
}
}

0 comments on commit 60e9c3f

Please sign in to comment.