mirror of
https://github.com/Respect/Validation.git
synced 2024-06-08 00:32:16 +02:00
Apply contribution guidelines to "Countable" rule
Co-Authored-By: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
4976fbaf48
commit
8f47cc37d6
|
@ -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',
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
37
tests/integration/rules/countable.phpt
Normal file
37
tests/integration/rules/countable.phpt
Normal 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
|
|
@ -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--
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -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();
|
||||
|
|
Loading…
Reference in a new issue