Skip to content

Commit aeb2849

Browse files
[10.x] Allow to pass Arrayable or Stringble in rules In and NotIn (#49055)
* Allow to pass `Arrayable` or `Stringble` in rules `In` and `NotIn` * Add tests
1 parent cc5d95c commit aeb2849

File tree

4 files changed

+57
-6
lines changed

4 files changed

+57
-6
lines changed

src/Illuminate/Validation/Rules/In.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Validation\Rules;
44

55
use BackedEnum;
6+
use Illuminate\Contracts\Support\Arrayable;
67
use UnitEnum;
78

89
class In
@@ -24,12 +25,16 @@ class In
2425
/**
2526
* Create a new in rule instance.
2627
*
27-
* @param array $values
28+
* @param \Illuminate\Contracts\Support\Arrayable|array|string $values
2829
* @return void
2930
*/
30-
public function __construct(array $values)
31+
public function __construct($values)
3132
{
32-
$this->values = $values;
33+
if ($values instanceof Arrayable) {
34+
$values = $values->toArray();
35+
}
36+
37+
$this->values = is_array($values) ? $values : func_get_args();
3338
}
3439

3540
/**

src/Illuminate/Validation/Rules/NotIn.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Validation\Rules;
44

55
use BackedEnum;
6+
use Illuminate\Contracts\Support\Arrayable;
67
use UnitEnum;
78

89
class NotIn
@@ -24,12 +25,16 @@ class NotIn
2425
/**
2526
* Create a new "not in" rule instance.
2627
*
27-
* @param array $values
28+
* @param \Illuminate\Contracts\Support\Arrayable|array|string $values
2829
* @return void
2930
*/
30-
public function __construct(array $values)
31+
public function __construct($values)
3132
{
32-
$this->values = $values;
33+
if ($values instanceof Arrayable) {
34+
$values = $values->toArray();
35+
}
36+
37+
$this->values = is_array($values) ? $values : func_get_args();
3338
}
3439

3540
/**

tests/Validation/ValidationInRuleTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,22 @@ public function testItCorrectlyFormatsAStringVersionOfTheRule()
1717

1818
$this->assertSame('in:"Laravel","Framework","PHP"', (string) $rule);
1919

20+
$rule = new In(collect(['Taylor', 'Michael', 'Tim']));
21+
22+
$this->assertSame('in:"Taylor","Michael","Tim"', (string) $rule);
23+
2024
$rule = new In(['Life, the Universe and Everything', 'this is a "quote"']);
2125

2226
$this->assertSame('in:"Life, the Universe and Everything","this is a ""quote"""', (string) $rule);
2327

28+
$rule = Rule::in(collect([1, 2, 3, 4]));
29+
30+
$this->assertSame('in:"1","2","3","4"', (string) $rule);
31+
32+
$rule = Rule::in(collect([1, 2, 3, 4]));
33+
34+
$this->assertSame('in:"1","2","3","4"', (string) $rule);
35+
2436
$rule = new In(["a,b\nc,d"]);
2537

2638
$this->assertSame("in:\"a,b\nc,d\"", (string) $rule);
@@ -41,6 +53,10 @@ public function testItCorrectlyFormatsAStringVersionOfTheRule()
4153

4254
$this->assertSame('in:"1","2","3","4"', (string) $rule);
4355

56+
$rule = new In('1', '2', '3', '4');
57+
58+
$this->assertSame('in:"1","2","3","4"', (string) $rule);
59+
4460
$rule = Rule::in([StringStatus::done]);
4561

4662
$this->assertSame('in:"done"', (string) $rule);

tests/Validation/ValidationNotInRuleTest.php

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

33
namespace Illuminate\Tests\Validation;
44

5+
use Illuminate\Tests\Validation\fixtures\Values;
56
use Illuminate\Validation\Rule;
67
use Illuminate\Validation\Rules\NotIn;
78
use PHPUnit\Framework\TestCase;
@@ -16,6 +17,18 @@ public function testItCorrectlyFormatsAStringVersionOfTheRule()
1617

1718
$this->assertSame('not_in:"Laravel","Framework","PHP"', (string) $rule);
1819

20+
$rule = new NotIn(collect(['Taylor', 'Michael', 'Tim']));
21+
22+
$this->assertSame('not_in:"Taylor","Michael","Tim"', (string) $rule);
23+
24+
$rule = Rule::notIn(collect([1, 2, 3, 4]));
25+
26+
$this->assertSame('not_in:"1","2","3","4"', (string) $rule);
27+
28+
$rule = Rule::notIn(collect([1, 2, 3, 4]));
29+
30+
$this->assertSame('not_in:"1","2","3","4"', (string) $rule);
31+
1932
$rule = Rule::notIn([1, 2, 3, 4]);
2033

2134
$this->assertSame('not_in:"1","2","3","4"', (string) $rule);
@@ -24,10 +37,22 @@ public function testItCorrectlyFormatsAStringVersionOfTheRule()
2437

2538
$this->assertSame('not_in:"1","2","3","4"', (string) $rule);
2639

40+
$rule = Rule::notIn(new Values);
41+
42+
$this->assertSame('not_in:"1","2","3","4"', (string) $rule);
43+
44+
$rule = new NotIn(new Values);
45+
46+
$this->assertSame('not_in:"1","2","3","4"', (string) $rule);
47+
2748
$rule = Rule::notIn('1', '2', '3', '4');
2849

2950
$this->assertSame('not_in:"1","2","3","4"', (string) $rule);
3051

52+
$rule = new NotIn('1', '2', '3', '4');
53+
54+
$this->assertSame('not_in:"1","2","3","4"', (string) $rule);
55+
3156
$rule = Rule::notIn([StringStatus::done]);
3257

3358
$this->assertSame('not_in:"done"', (string) $rule);

0 commit comments

Comments
 (0)