Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/Grid/Column/HasDisplayers.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,15 @@ public function prepend($val)
{
return $this->display(function ($v, $column) use (&$val) {
if ($val instanceof \Closure) {
$val = $val->call($this, $v, $column->getOriginal(), $column);
$original = $column->getOriginal();

if ($original instanceof \BackedEnum) {
$original = $original->value;
}else if($original instanceof \UnitEnum){
$original = $original->name;
}

$val = $val->call($this, $v, $original, $column);
}

if (is_array($v)) {
Expand All @@ -131,6 +139,18 @@ public function prepend($val)
return $v->prepend($val);
}

// 如果是枚举类型
if ($v instanceof \UnitEnum) {
// 如果实现了 JsonSerializable 接口,优先取此值
if ($v instanceof \JsonSerializable) {
$v = (string)$v->jsonSerialize();
} else if ($v instanceof \StringBackedEnum) {
$v = $v->value;
} else {
$v = $v->name;
}
}

return $val.$v;
});
}
Expand Down
12 changes: 12 additions & 0 deletions src/Grid/Filter/Presenter/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ protected function buildOptions(): array
$this->options = $this->options->toArray();
}

$firstOption = $this->options[0] ?? null;
if ($firstOption instanceof \UnitEnum) {
// TODO 如果实现了 \JsonSerializable 接口,是否要取 jsonSerialize() 方法的值?

$isBackedEnum = $firstOption instanceof \BackedEnum;
$this->options = array_column(
array: $this->options,
column_key: 'name',
index_key: $isBackedEnum ? 'value' : 'name'
);
}

$this->addDefaultConfig([
'allowClear' => true,
'placeholder' => [
Expand Down