Skip to content

Commit 5d323f1

Browse files
author
Kevin Buchholz
committed
fix for number enums
1 parent c522e29 commit 5d323f1

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/Rules/EnumerationValue.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ class EnumerationValue implements Rule
2121
*/
2222
private $values;
2323

24+
/**
25+
* Determine whether this rule should check case sensitive or not.
26+
*
27+
* @var bool
28+
*/
29+
private $casesensitive = false;
30+
2431
/**
2532
* Create a new rule instance.
2633
*
@@ -45,7 +52,7 @@ public function __construct(string $enum, ?array $values = null)
4552
public function passes($attribute, $value): bool
4653
{
4754
try {
48-
return $this->enumClass::memberByValue($value)
55+
return $this->enumClass::memberByValue($value, $this->casesensitive)
4956
->anyOfArray($this->values ?? $this->enumClass::members());
5057
} catch (UndefinedMemberExceptionInterface $e) {
5158
return false;
@@ -61,4 +68,17 @@ public function message(): string
6168
{
6269
return __('laravel_enumeration::validation.enum_value');
6370
}
71+
72+
/**
73+
* Set the case sensitivity for this rule.
74+
*
75+
* It is not recommended to set this to true for enums with number-values.
76+
*
77+
* @param bool $caseSensitive
78+
* @return void
79+
*/
80+
public function setCaseSensitivity(bool $caseSensitive): void
81+
{
82+
$this->casesensitive = $caseSensitive;
83+
}
6484
}

0 commit comments

Comments
 (0)