Skip to content
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

Cannot transition state on Backed Enum if value is non-lowercased #6

Closed
fmiqbal opened this issue Sep 30, 2024 · 0 comments
Closed

Comments

@fmiqbal
Copy link

fmiqbal commented Sep 30, 2024

Given I have this enum

enum PostStatus: string
{
    use State;

    case DRAFT = 'DRAFT';
    case READY = 'READY';
    case PUBLISHED = 'PUBLISHED';
    case ARCHIVED = 'ARCHIVED';
}

Even the default allowedTransitions won't work, this is because when doint the comparison, specifically on

private static function getTransitions(UnitEnum $currentTransition): array
{
$transitions = array_change_key_case(
EnumState::transitions($currentTransition::class, $currentTransition::class::transitions())
);
$transitions = $transitions[$currentTransition->name]
?? $transitions[EnumValue::value($currentTransition)]
?? [];
return array_filter(is_array($transitions) ? $transitions : [$transitions]);
}

the array_change_key_case make the key to be lowercased, meanwhile

the $transitions[EnumValue::value($currentTransition)] has

public static function value(UnitEnum $enum, bool $keepCase = null): string|int
{
if (is_null($keepCase)) {
$keepCase = self::isStrict($enum);
}
return $enum->value ?? ($keepCase ? $enum->name : strtolower($enum->name));
}

in which if there are value it will use that, case is when the value is an uppercased letter, it will be compared to lowercased $transitions array from before

Our specific use case is because Laravel has Rule::enum for validation, already but in need to be backed, meanwhile, the transition is having this issue, for now my alternative is using Rule::isEnum this package provide,

henzeb added a commit that referenced this issue Oct 3, 2024
@henzeb henzeb closed this as completed in adff7ec Oct 3, 2024
henzeb added a commit that referenced this issue Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant