mirror of
https://github.com/Respect/Validation.git
synced 2024-06-04 14:52:17 +02:00
Create "ArrayType" rule
This commit is contained in:
parent
767fcaaccf
commit
f08a1fa9fc
19
docs/ArrayType.md
Normal file
19
docs/ArrayType.md
Normal file
|
@ -0,0 +1,19 @@
|
|||
# ArrayType
|
||||
|
||||
- `v::arrayType()`
|
||||
|
||||
Validates whether the type of an input is array.
|
||||
|
||||
```php
|
||||
v::arrayType()->validate([]); // true
|
||||
v::arrayType()->validate([1, 2, 3]); // true
|
||||
v::arrayType()->validate(new ArrayObject()); // false
|
||||
```
|
||||
|
||||
***
|
||||
See also:
|
||||
|
||||
* [ArrayVal](ArrayVal.md)
|
||||
* [Countable](Countable.md)
|
||||
* [Iterable](Iterable.md)
|
||||
* [Iterable](Iterable.md)
|
|
@ -3,6 +3,7 @@
|
|||
## Types
|
||||
|
||||
* [ArrayVal](ArrayVal.md)
|
||||
* [ArrayType](ArrayType.md)
|
||||
* [BoolType](BoolType.md)
|
||||
* [CallableType](CallableType.md)
|
||||
* [Countable](Countable.md)
|
||||
|
@ -99,6 +100,7 @@
|
|||
## Arrays
|
||||
|
||||
* [ArrayVal](ArrayVal.md)
|
||||
* [ArrayType](ArrayType.md)
|
||||
* [Contains](Contains.md)
|
||||
* [Each](Each.md)
|
||||
* [EndsWith](EndsWith.md)
|
||||
|
@ -195,6 +197,7 @@
|
|||
* [AlwaysInvalid](AlwaysInvalid.md)
|
||||
* [AlwaysValid](AlwaysValid.md)
|
||||
* [ArrayVal](ArrayVal.md)
|
||||
* [ArrayType](ArrayType.md)
|
||||
* [Attribute](Attribute.md)
|
||||
* [Bank](Bank.md)
|
||||
* [BankAccount](BankAccount.md)
|
||||
|
|
24
library/Exceptions/ArrayTypeException.php
Normal file
24
library/Exceptions/ArrayTypeException.php
Normal file
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Respect/Validation.
|
||||
*
|
||||
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
*
|
||||
* For the full copyright and license information, please view the "LICENSE.md"
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class ArrayTypeException extends ValidationException
|
||||
{
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} must be of the type array',
|
||||
],
|
||||
self::MODE_NEGATIVE => [
|
||||
self::STANDARD => '{{name}} must not be of the type array',
|
||||
],
|
||||
];
|
||||
}
|
20
library/Rules/ArrayType.php
Normal file
20
library/Rules/ArrayType.php
Normal file
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Respect/Validation.
|
||||
*
|
||||
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
*
|
||||
* For the full copyright and license information, please view the "LICENSE.md"
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class ArrayType extends AbstractRule
|
||||
{
|
||||
public function validate($input)
|
||||
{
|
||||
return is_array($input);
|
||||
}
|
||||
}
|
|
@ -25,6 +25,7 @@ use Respect\Validation\Rules\Key;
|
|||
* @method static Validator alwaysInvalid()
|
||||
* @method static Validator alwaysValid()
|
||||
* @method static Validator arrayVal()
|
||||
* @method static Validator arrayType()
|
||||
* @method static Validator attribute(string $reference, Validatable $validator = null, bool $mandatory = true)
|
||||
* @method static Validator bank(string $countryCode)
|
||||
* @method static Validator bankAccount(string $countryCode)
|
||||
|
|
10
tests/integration/arrayType_1.phpt
Normal file
10
tests/integration/arrayType_1.phpt
Normal file
|
@ -0,0 +1,10 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
v::arrayType()->assert([]);
|
||||
v::arrayType()->check([1, 2, 3]);
|
||||
?>
|
||||
--EXPECTF--
|
15
tests/integration/arrayType_2.phpt
Normal file
15
tests/integration/arrayType_2.phpt
Normal file
|
@ -0,0 +1,15 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\ArrayTypeException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::arrayType()->check('teste');
|
||||
} catch (ArrayTypeException $exception) {
|
||||
echo $exception->getMainMessage();
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
"teste" must be of the type array
|
15
tests/integration/arrayType_3.phpt
Normal file
15
tests/integration/arrayType_3.phpt
Normal file
|
@ -0,0 +1,15 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\AllOfException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::arrayType()->assert(new ArrayObject());
|
||||
} catch (AllOfException $exception) {
|
||||
echo $exception->getFullMessage();
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
\-`[traversable] (ArrayObject: { })` must be of the type array
|
15
tests/integration/arrayType_4.phpt
Normal file
15
tests/integration/arrayType_4.phpt
Normal file
|
@ -0,0 +1,15 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\ArrayTypeException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::not(v::arrayType())->check([]);
|
||||
} catch (ArrayTypeException $exception) {
|
||||
echo $exception->getMainMessage();
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
{ } must not be of the type array
|
15
tests/integration/arrayType_5.phpt
Normal file
15
tests/integration/arrayType_5.phpt
Normal file
|
@ -0,0 +1,15 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\AllOfException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::not(v::arrayType())->assert([1, 2, 3]);
|
||||
} catch (AllOfException $exception) {
|
||||
echo $exception->getFullMessage();
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
\-{ 1, 2, 3 } must not be of the type array
|
43
tests/unit/Rules/ArrayTypeTest.php
Normal file
43
tests/unit/Rules/ArrayTypeTest.php
Normal file
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Respect/Validation.
|
||||
*
|
||||
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
*
|
||||
* For the full copyright and license information, please view the "LICENSE.md"
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
* @covers Respect\Validation\Rules\ArrayType
|
||||
*/
|
||||
class ArrayTypeTest extends RuleTestCase
|
||||
{
|
||||
public function providerForValidInput()
|
||||
{
|
||||
$rule = new ArrayType();
|
||||
|
||||
return [
|
||||
[$rule, []],
|
||||
[$rule, [1, 2, 3]],
|
||||
];
|
||||
}
|
||||
|
||||
public function providerForInvalidInput()
|
||||
{
|
||||
$rule = new ArrayType();
|
||||
|
||||
return [
|
||||
[$rule, 'test'],
|
||||
[$rule, 1],
|
||||
[$rule, 1.0],
|
||||
[$rule, true],
|
||||
[$rule, new \ArrayObject()],
|
||||
[$rule, new \ArrayIterator()],
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue