forked from laravel/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[11.x] Introduce method
Rule::array()
(laravel#51250)
* [11.x] Introduce method `Rule::array()` * CS fixes * Add `func_get_args` test * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
- Loading branch information
1 parent
8f9cba8
commit 8c684a2
Showing
4 changed files
with
129 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace Illuminate\Validation\Rules; | ||
|
||
use BackedEnum; | ||
use Illuminate\Contracts\Support\Arrayable; | ||
use Stringable; | ||
use UnitEnum; | ||
|
||
class ArrayRule implements Stringable | ||
{ | ||
/** | ||
* The accepted keys. | ||
* | ||
* @var array | ||
*/ | ||
protected $keys; | ||
|
||
/** | ||
* Create a new array rule instance. | ||
* | ||
* @param array|null $keys | ||
* @return void | ||
*/ | ||
public function __construct($keys = null) | ||
{ | ||
if ($keys instanceof Arrayable) { | ||
$keys = $keys->toArray(); | ||
} | ||
|
||
$this->keys = is_array($keys) ? $keys : func_get_args(); | ||
} | ||
|
||
/** | ||
* Convert the rule to a validation string. | ||
* | ||
* @return string | ||
*/ | ||
public function __toString() | ||
{ | ||
if (empty($this->keys)) { | ||
return 'array'; | ||
} | ||
|
||
$keys = array_map( | ||
static fn ($key) => match (true) { | ||
$key instanceof BackedEnum => $key->value, | ||
$key instanceof UnitEnum => $key->name, | ||
default => $key, | ||
}, | ||
$this->keys, | ||
); | ||
|
||
return 'array:'.implode(',', $keys); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Validation; | ||
|
||
use Illuminate\Validation\Rule; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
include_once 'Enums.php'; | ||
|
||
class ValidationArrayRuleTest extends TestCase | ||
{ | ||
public function testItCorrectlyFormatsAStringVersionOfTheRule() | ||
{ | ||
$rule = Rule::array(); | ||
|
||
$this->assertSame('array', (string) $rule); | ||
|
||
$rule = Rule::array('key_1', 'key_2', 'key_3'); | ||
|
||
$this->assertSame('array:key_1,key_2,key_3', (string) $rule); | ||
|
||
$rule = Rule::array(['key_1', 'key_2', 'key_3']); | ||
|
||
$this->assertSame('array:key_1,key_2,key_3', (string) $rule); | ||
|
||
$rule = Rule::array(collect(['key_1', 'key_2', 'key_3'])); | ||
|
||
$this->assertSame('array:key_1,key_2,key_3', (string) $rule); | ||
|
||
$rule = Rule::array([ArrayKeys::key_1, ArrayKeys::key_2, ArrayKeys::key_3]); | ||
|
||
$this->assertSame('array:key_1,key_2,key_3', (string) $rule); | ||
|
||
$rule = Rule::array([ArrayKeysBacked::key_1, ArrayKeysBacked::key_2, ArrayKeysBacked::key_3]); | ||
|
||
$this->assertSame('array:key_1,key_2,key_3', (string) $rule); | ||
} | ||
} |