Skip to content

EnumRule issue with different Enums, same value #93

@robert-abram

Description

@robert-abram

Hi,
When we have 2 different enums and 1 rule EnumRule(EmailScopeEnum::class

/**
@method static self DEFAULT()
**/
EmailScopeEnum {
}

/**
@method static self DEFAULT()
**/
PhoneScopeEnum {
}

When we try to check rule against PhoneScopeEnum::DEFAULT() it's working, because DEFAULT can be resolved also in EmailScopeEnum

I fix this by changing passes function in EnumRule

public function passes($attribute, $value): bool
    {
        $this->attribute = $attribute;
        $this->value = $value;

        if ($value instanceof $this->enum) {
            return true;
        }

        if ($value instanceof Enum && !$value instanceof $this->enum) {
            return false;
        }

        try {
            $this->asEnum($value);

            return true;
        } catch (Throwable $ex) {
            return false;
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions