Skip to content

Commit 4989e6d

Browse files
geisitaylorotwell
andauthored
[10.x] Add Enum Support to the In and NotIn Validation Rules (#48247)
* Add Enum compatability to in and not_in validation rules * fix tests * styleci changes * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent a2dc7ef commit 4989e6d

File tree

5 files changed

+47
-1
lines changed

5 files changed

+47
-1
lines changed

src/Illuminate/Validation/Rules/In.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Illuminate\Validation\Rules;
44

5+
use BackedEnum;
6+
use UnitEnum;
7+
58
class In
69
{
710
/**
@@ -39,6 +42,12 @@ public function __construct(array $values)
3942
public function __toString()
4043
{
4144
$values = array_map(function ($value) {
45+
$value = match (true) {
46+
$value instanceof BackedEnum => $value->value,
47+
$value instanceof UnitEnum => $value->name,
48+
default => $value,
49+
};
50+
4251
return '"'.str_replace('"', '""', $value).'"';
4352
}, $this->values);
4453

src/Illuminate/Validation/Rules/NotIn.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Illuminate\Validation\Rules;
44

5+
use BackedEnum;
6+
use UnitEnum;
7+
58
class NotIn
69
{
710
/**
@@ -37,6 +40,12 @@ public function __construct(array $values)
3740
public function __toString()
3841
{
3942
$values = array_map(function ($value) {
43+
$value = match (true) {
44+
$value instanceof BackedEnum => $value->value,
45+
$value instanceof UnitEnum => $value->name,
46+
default => $value,
47+
};
48+
4049
return '"'.str_replace('"', '""', $value).'"';
4150
}, $this->values);
4251

tests/Validation/ValidationEnumRuleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Illuminate\Validation\Validator;
1212
use PHPUnit\Framework\TestCase;
1313

14-
include 'Enums.php';
14+
include_once 'Enums.php';
1515

1616
class ValidationEnumRuleTest extends TestCase
1717
{

tests/Validation/ValidationInRuleTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Illuminate\Validation\Rules\In;
88
use PHPUnit\Framework\TestCase;
99

10+
include_once 'Enums.php';
11+
1012
class ValidationInRuleTest extends TestCase
1113
{
1214
public function testItCorrectlyFormatsAStringVersionOfTheRule()
@@ -38,5 +40,17 @@ public function testItCorrectlyFormatsAStringVersionOfTheRule()
3840
$rule = Rule::in('1', '2', '3', '4');
3941

4042
$this->assertSame('in:"1","2","3","4"', (string) $rule);
43+
44+
$rule = Rule::in([StringStatus::done]);
45+
46+
$this->assertSame('in:"done"', (string) $rule);
47+
48+
$rule = Rule::in([IntegerStatus::done]);
49+
50+
$this->assertSame('in:"2"', (string) $rule);
51+
52+
$rule = Rule::in([PureEnum::one]);
53+
54+
$this->assertSame('in:"one"', (string) $rule);
4155
}
4256
}

tests/Validation/ValidationNotInRuleTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use Illuminate\Validation\Rules\NotIn;
77
use PHPUnit\Framework\TestCase;
88

9+
include_once 'Enums.php';
10+
911
class ValidationNotInRuleTest extends TestCase
1012
{
1113
public function testItCorrectlyFormatsAStringVersionOfTheRule()
@@ -25,5 +27,17 @@ public function testItCorrectlyFormatsAStringVersionOfTheRule()
2527
$rule = Rule::notIn('1', '2', '3', '4');
2628

2729
$this->assertSame('not_in:"1","2","3","4"', (string) $rule);
30+
31+
$rule = Rule::notIn([StringStatus::done]);
32+
33+
$this->assertSame('not_in:"done"', (string) $rule);
34+
35+
$rule = Rule::notIn([IntegerStatus::done]);
36+
37+
$this->assertSame('not_in:"2"', (string) $rule);
38+
39+
$rule = Rule::notIn([PureEnum::one]);
40+
41+
$this->assertSame('not_in:"one"', (string) $rule);
2842
}
2943
}

0 commit comments

Comments
 (0)