Create "ArrayType" rule

This commit is contained in:
João Torquato 2015-10-17 17:51:42 -03:00 committed by Henrique Moody
parent 767fcaaccf
commit f08a1fa9fc
11 changed files with 180 additions and 0 deletions

19
docs/ArrayType.md Normal file
View 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)

View file

@ -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)

View 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',
],
];
}

View 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);
}
}

View file

@ -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)

View 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--

View 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

View 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

View 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

View 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

View 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()],
];
}
}