Apply contribution guidelines to "ArrayType" rule

This commit is contained in:
Emmerson Siqueira 2018-03-16 11:16:46 +01:00
parent 2069e706b8
commit ad61c49eca
9 changed files with 64 additions and 75 deletions

View file

@ -13,14 +13,21 @@ declare(strict_types=1);
namespace Respect\Validation\Exceptions;
class ArrayTypeException extends ValidationException
/**
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
final class ArrayTypeException extends ValidationException
{
/**
* {@inheritdoc}
*/
public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be of the type array',
self::STANDARD => '{{name}} must be of type array',
],
self::MODE_NEGATIVE => [
self::STANDARD => '{{name}} must not be of the type array',
self::STANDARD => '{{name}} must not be of type array',
],
];
}

View file

@ -13,8 +13,17 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
class ArrayType extends AbstractRule
/**
* Validates whether the type of an input is array.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
final class ArrayType extends AbstractRule
{
/**
* {@inheritdoc}
*/
public function validate($input): bool
{
return is_array($input);

View file

@ -0,0 +1,37 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Exceptions\ArrayTypeException;
use Respect\Validation\Validator as v;
try {
v::arrayType()->check('teste');
} catch (ArrayTypeException $exception) {
echo $exception->getMainMessage().PHP_EOL;
}
try {
v::not(v::arrayType())->check([]);
} catch (ArrayTypeException $exception) {
echo $exception->getMainMessage().PHP_EOL;
}
try {
v::arrayType()->assert(new ArrayObject());
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::not(v::arrayType())->assert([1, 2, 3]);
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
?>
--EXPECTF--
"teste" must be of type array
`{ }` must not be of type array
- `[traversable] (ArrayObject: { })` must be of type array
- `{ 1, 2, 3 }` must not be of type array

View file

@ -1,10 +0,0 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Validator as v;
v::arrayType()->assert([]);
v::arrayType()->check([1, 2, 3]);
?>
--EXPECTF--

View file

@ -1,15 +0,0 @@
--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

@ -1,15 +0,0 @@
--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

@ -1,15 +0,0 @@
--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

@ -1,15 +0,0 @@
--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

@ -19,8 +19,11 @@ use Respect\Validation\Test\RuleTestCase;
* @group rule
* @covers \Respect\Validation\Rules\ArrayType
*/
class ArrayTypeTest extends RuleTestCase
final class ArrayTypeTest extends RuleTestCase
{
/**
* {@inheritdoc}
*/
public function providerForValidInput(): array
{
$rule = new ArrayType();
@ -31,6 +34,9 @@ class ArrayTypeTest extends RuleTestCase
];
}
/**
* {@inheritdoc}
*/
public function providerForInvalidInput(): array
{
$rule = new ArrayType();