我想用Ben Sampo Package元来做腊肠. 此包有一个名为‘Description’的属性作为新属性. 但是我不能向我的枚举添加额外的属性(属性).
我想要有一个具有‘isfinal’属性的枚举‘Status’.来确定这个密钥是不是最终密钥?
我真的需要为一些枚举添加一些属性. 有什么办法吗?
我也查了this package张.添加额外的属性非常好,而且非常容易.但我也需要本地化,而默认情况下它没有.
编辑: 我需要的枚举是这样的:
final class Status extends Enum
{
#[Color('red')]
#[Description('Initial status')]
#[IsFinal(false)]
const Initial = 1;
#[Color('blue')]
#[Description('suspended status')]
#[IsFinal(false)]
const Pending = 2;
#[Color('white')]
#[Description('completed status')]
#[IsFinal(true)]
const Completed = 3;
#[Color('red')]
#[Description('canceled status')]
#[IsFinal(true)]
const Canceled = 4;
}
然后,我想要检索 colored颜色 值,或者是如下所示的最终值:
Status::getColor(1);
和
Status::Initial->getColor();
和
$enum->getColor();
更新:我现在有这个trait 了:
<?php
namespace App\Enums\Concerns;
use BenSampo\Enum\Attributes\Description;
use Exception;
/**
* Get Enum attributes
*/
trait GetEnumAttributes
{
public $color;
public $isFinal;
public function __construct(mixed $enumValue)
{
$this->color = $this->getCustomAttribute($enumValue, 'color');
$this->isFinal = $this->getCustomAttribute($enumValue, 'is_final');
$this->description = self::getDescription($enumValue);
parent::__construct($enumValue);
}
protected static function getCustomAttribute(mixed $value, $key)
{
$reflection = self::getReflection();
$constantName = static::getKey($value);
$constReflection = $reflection->getReflectionConstant($constantName);
if ($constReflection === false) {
return null;
}
switch ($key) {
case 'color':
// dd( $constReflection->getAttributes(Color::class));
$customAttributes = $constReflection->getAttributes(Color::class);
break;
case 'is_final':
$customAttributes = $constReflection->getAttributes(IsFinal::class);
break;
case 'description':
$customAttributes = $constReflection->getAttributes(Description::class);
break;
default:
$customAttributes = [];
break;
}
return match (count($customAttributes)) {
0 => null,
1 => $customAttributes[0]->newInstance()->$key,
default => throw new Exception('You cannot use more than 1 custom attribute on ' . class_basename(static::class) . '::' . $constantName),
};
}
public static function getCustomInstances()
{
return array_map(
function ($constantValue) {
$newStatic = new static($constantValue);
$newStatic = (array) $newStatic;
$newStatic['description'] = self::getCustomAttribute($constantValue,'description');//getDescription($constantValue);
$newStatic['color'] = self::getCustomAttribute($constantValue,'color');
$newStatic['isFinal'] = self::getCustomAttribute($constantValue,'is_final');
$newStatic = (object) $newStatic;
return $newStatic;
},
static::getConstants(),
);
}
}
当我使用以下命令时会发生这种情况:
dd(Status::getInstances());
结果:
array:4 [▼ // app\Http\Controllers\testController.php:19
"Initial" => App\Enums\Status {#416 ▼
+value: 1
+key: "Initial"
+description: "initial status"
+color: null
+isFinal: null
}
"Pending" => App\Enums\Status {#418 ▼
+value: 2
+key: "Pending"
+description: "suspended status"
+color: null
+isFinal: null
}
"Completed" => App\Enums\Status {#421 ▶}
"Canceled" => App\Enums\Status {#422 ▶}
]
我在‘getCustomAttribute’中判断了这个问题,以防万一:
case 'color':
dd( $constReflection->getAttributes(Color::class));
$customAttributes = $constReflection->getAttributes(Color::class);
break;
返回NULL,但是$stRefltion具有正确的所有属性. 同样的情况发生在我使用
Status::getCustomInstances();