Skip to content

Commit f08a1fa

Browse files
joaotorquatohenriquemoody
authored andcommitted
Create "ArrayType" rule
1 parent 767fcaa commit f08a1fa

File tree

11 files changed

+180
-0
lines changed

11 files changed

+180
-0
lines changed

docs/ArrayType.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# ArrayType
2+
3+
- `v::arrayType()`
4+
5+
Validates whether the type of an input is array.
6+
7+
```php
8+
v::arrayType()->validate([]); // true
9+
v::arrayType()->validate([1, 2, 3]); // true
10+
v::arrayType()->validate(new ArrayObject()); // false
11+
```
12+
13+
***
14+
See also:
15+
16+
* [ArrayVal](ArrayVal.md)
17+
* [Countable](Countable.md)
18+
* [Iterable](Iterable.md)
19+
* [Iterable](Iterable.md)

docs/VALIDATORS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Types
44

55
* [ArrayVal](ArrayVal.md)
6+
* [ArrayType](ArrayType.md)
67
* [BoolType](BoolType.md)
78
* [CallableType](CallableType.md)
89
* [Countable](Countable.md)
@@ -99,6 +100,7 @@
99100
## Arrays
100101

101102
* [ArrayVal](ArrayVal.md)
103+
* [ArrayType](ArrayType.md)
102104
* [Contains](Contains.md)
103105
* [Each](Each.md)
104106
* [EndsWith](EndsWith.md)
@@ -195,6 +197,7 @@
195197
* [AlwaysInvalid](AlwaysInvalid.md)
196198
* [AlwaysValid](AlwaysValid.md)
197199
* [ArrayVal](ArrayVal.md)
200+
* [ArrayType](ArrayType.md)
198201
* [Attribute](Attribute.md)
199202
* [Bank](Bank.md)
200203
* [BankAccount](BankAccount.md)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Respect/Validation.
5+
*
6+
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
7+
*
8+
* For the full copyright and license information, please view the "LICENSE.md"
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Respect\Validation\Exceptions;
13+
14+
class ArrayTypeException extends ValidationException
15+
{
16+
public static $defaultTemplates = [
17+
self::MODE_DEFAULT => [
18+
self::STANDARD => '{{name}} must be of the type array',
19+
],
20+
self::MODE_NEGATIVE => [
21+
self::STANDARD => '{{name}} must not be of the type array',
22+
],
23+
];
24+
}

library/Rules/ArrayType.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Respect/Validation.
5+
*
6+
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
7+
*
8+
* For the full copyright and license information, please view the "LICENSE.md"
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Respect\Validation\Rules;
13+
14+
class ArrayType extends AbstractRule
15+
{
16+
public function validate($input)
17+
{
18+
return is_array($input);
19+
}
20+
}

library/Validator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* @method static Validator alwaysInvalid()
2626
* @method static Validator alwaysValid()
2727
* @method static Validator arrayVal()
28+
* @method static Validator arrayType()
2829
* @method static Validator attribute(string $reference, Validatable $validator = null, bool $mandatory = true)
2930
* @method static Validator bank(string $countryCode)
3031
* @method static Validator bankAccount(string $countryCode)

tests/integration/arrayType_1.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--FILE--
2+
<?php
3+
require 'vendor/autoload.php';
4+
5+
use Respect\Validation\Validator as v;
6+
7+
v::arrayType()->assert([]);
8+
v::arrayType()->check([1, 2, 3]);
9+
?>
10+
--EXPECTF--

tests/integration/arrayType_2.phpt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--FILE--
2+
<?php
3+
require 'vendor/autoload.php';
4+
5+
use Respect\Validation\Exceptions\ArrayTypeException;
6+
use Respect\Validation\Validator as v;
7+
8+
try {
9+
v::arrayType()->check('teste');
10+
} catch (ArrayTypeException $exception) {
11+
echo $exception->getMainMessage();
12+
}
13+
?>
14+
--EXPECTF--
15+
"teste" must be of the type array

tests/integration/arrayType_3.phpt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--FILE--
2+
<?php
3+
require 'vendor/autoload.php';
4+
5+
use Respect\Validation\Exceptions\AllOfException;
6+
use Respect\Validation\Validator as v;
7+
8+
try {
9+
v::arrayType()->assert(new ArrayObject());
10+
} catch (AllOfException $exception) {
11+
echo $exception->getFullMessage();
12+
}
13+
?>
14+
--EXPECTF--
15+
\-`[traversable] (ArrayObject: { })` must be of the type array

tests/integration/arrayType_4.phpt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--FILE--
2+
<?php
3+
require 'vendor/autoload.php';
4+
5+
use Respect\Validation\Exceptions\ArrayTypeException;
6+
use Respect\Validation\Validator as v;
7+
8+
try {
9+
v::not(v::arrayType())->check([]);
10+
} catch (ArrayTypeException $exception) {
11+
echo $exception->getMainMessage();
12+
}
13+
?>
14+
--EXPECTF--
15+
{ } must not be of the type array

tests/integration/arrayType_5.phpt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--FILE--
2+
<?php
3+
require 'vendor/autoload.php';
4+
5+
use Respect\Validation\Exceptions\AllOfException;
6+
use Respect\Validation\Validator as v;
7+
8+
try {
9+
v::not(v::arrayType())->assert([1, 2, 3]);
10+
} catch (AllOfException $exception) {
11+
echo $exception->getFullMessage();
12+
}
13+
?>
14+
--EXPECTF--
15+
\-{ 1, 2, 3 } must not be of the type array

0 commit comments

Comments
 (0)