New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow to provide extra values to EnumCase attribute #170
Conversation
9507772
to
a6389b6
Compare
Makes sense to me 💯 |
Might be. But I think your use-case might be covered by: /**
* @final
*/
#[\Attribute(\Attribute::TARGET_CLASS_CONSTANT)]
class RenderableCase extends EnumCase
{
public function __construct(string $label, string $color, string $icon)
{
parent::__construct($label, extra: compact('color', 'icon'))
}
} while still requiring only |
My concern is type safety. With this Something like: /**
* @return EnumCase[]
*/
public function getAttributes(): array
{
// ...
}
public function getColor(): ?string
{
foreach ($this->getAttributes() as $attribute) {
if ($attribute instanceof HasColor) {
return $attribute->color;
}
}
return null;
} We could combine both approaches - for quick results, developers might use this |
But the example in #170 (comment) is enough to provide type safety to me (unless you use both |
a6389b6
to
617c7e7
Compare
|
||
trait EnumCaseAttributesTrait | ||
{ | ||
private function getEnumCaseAttribute(): ?EnumCase |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(methods are private, but can be used by any userland implementation using this trait, as not marked @internal
)
209bacd
to
028cf9d
Compare
028cf9d
to
2b4b0bc
Compare
cc @IonBazan
https://github.com/Elao/PhpEnums/blob/enum-case-extras/README.md#extra-values
Would this fit to your needs?