Skip to content

Commit

Permalink
Create "ArrayType" rule
Browse files Browse the repository at this point in the history
  • Loading branch information
joaotorquato authored and henriquemoody committed Oct 18, 2015
1 parent 767fcaa commit f08a1fa
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 0 deletions.
19 changes: 19 additions & 0 deletions docs/ArrayType.md
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 0 deletions docs/VALIDATORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Types

* [ArrayVal](ArrayVal.md)
* [ArrayType](ArrayType.md)
* [BoolType](BoolType.md)
* [CallableType](CallableType.md)
* [Countable](Countable.md)
Expand Down Expand Up @@ -99,6 +100,7 @@
## Arrays

* [ArrayVal](ArrayVal.md)
* [ArrayType](ArrayType.md)
* [Contains](Contains.md)
* [Each](Each.md)
* [EndsWith](EndsWith.md)
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions library/Exceptions/ArrayTypeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <[email protected]>
*
* 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 changes: 20 additions & 0 deletions library/Rules/ArrayType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <[email protected]>
*
* 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);
}
}
1 change: 1 addition & 0 deletions library/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* @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)
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/arrayType_1.phpt
Original file line number Diff line number Diff line change
@@ -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 changes: 15 additions & 0 deletions tests/integration/arrayType_2.phpt
Original file line number Diff line number Diff line change
@@ -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 changes: 15 additions & 0 deletions tests/integration/arrayType_3.phpt
Original file line number Diff line number Diff line change
@@ -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 changes: 15 additions & 0 deletions tests/integration/arrayType_4.phpt
Original file line number Diff line number Diff line change
@@ -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 changes: 15 additions & 0 deletions tests/integration/arrayType_5.phpt
Original file line number Diff line number Diff line change
@@ -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 changes: 43 additions & 0 deletions tests/unit/Rules/ArrayTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <[email protected]>
*
* 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()],
];
}
}

0 comments on commit f08a1fa

Please sign in to comment.