Apply contribution guidelines to "Countable" rule

Co-Authored-By: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
William Espindola 2018-06-01 11:23:37 -03:00 committed by Henrique Moody
parent 4976fbaf48
commit 8f47cc37d6
No known key found for this signature in database
GPG key ID: 221E9281655813A6
8 changed files with 74 additions and 63 deletions

View file

@ -13,8 +13,16 @@ declare(strict_types=1);
namespace Respect\Validation\Exceptions;
class CountableException extends ValidationException
/**
* @author Henrique Moody <henriquemoody@gmail.com>
* @author João Torquato <joao.otl@gmail.com>
* @author William Espindola <oi@williamespindola.com.br>
*/
final class CountableException extends ValidationException
{
/**
* {@inheritdoc}
*/
public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be countable',

View file

@ -13,10 +13,23 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
class Countable extends AbstractRule
use Countable as CountableInterface;
use function is_array;
/**
* Validates if the input is countable.
*
* @author Henrique Moody <henriquemoody@gmail.com>
* @author João Torquato <joao.otl@gmail.com>
* @author William Espindola <oi@williamespindola.com.br>
*/
final class Countable extends AbstractRule
{
/**
* {@inheritdoc}
*/
public function validate($input): bool
{
return is_array($input) || $input instanceof \Countable;
return is_array($input) || $input instanceof CountableInterface;
}
}

View file

@ -0,0 +1,37 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\CountableException;
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Validator as v;
try {
v::countable()->check(1.0);
} catch (CountableException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::not(v::countable())->check([]);
} catch (CountableException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::countable()->assert('Not countable!');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::not(v::countable())->assert(new ArrayObject());
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
?>
--EXPECTF--
1.0 must be countable
`{ }` must not be countable
- "Not countable!" must be countable
- `[traversable] (ArrayObject: { })` must not be countable

View file

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

View file

@ -1,16 +0,0 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\AllOfException;
use Respect\Validation\Validator as v;
try {
v::countable()->assert('Not countable!');
} catch (AllOfException $e) {
echo $e->getFullMessage();
}
?>
--EXPECTF--
- "Not countable!" must be countable

View file

@ -1,16 +0,0 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\CountableException;
use Respect\Validation\Validator as v;
try {
v::not(v::countable())->check([]);
} catch (CountableException $e) {
echo $e->getMessage();
}
?>
--EXPECTF--
`{ }` must not be countable

View file

@ -1,16 +0,0 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\AllOfException;
use Respect\Validation\Validator as v;
try {
v::not(v::countable())->assert(new ArrayObject());
} catch (AllOfException $e) {
echo $e->getFullMessage();
}
?>
--EXPECTF--
- `[traversable] (ArrayObject: { })` must not be countable

View file

@ -16,11 +16,19 @@ namespace Respect\Validation\Rules;
use Respect\Validation\Test\RuleTestCase;
/**
* @group rule
* @group rule
*
* @covers \Respect\Validation\Rules\Countable
*
* @author Henrique Moody <henriquemoody@gmail.com>
* @author João Torquato <joao.otl@gmail.com>
* @author William Espindola <oi@williamespindola.com.br>
*/
class CountableTest extends RuleTestCase
final class CountableTest extends RuleTestCase
{
/**
* {@inheritdoc}
*/
public function providerForValidInput(): array
{
$rule = new Countable();
@ -32,6 +40,9 @@ class CountableTest extends RuleTestCase
];
}
/**
* {@inheritdoc}
*/
public function providerForInvalidInput(): array
{
$rule = new Countable();