mirror of
https://github.com/Respect/Validation.git
synced 2024-06-04 14:52:17 +02:00
Apply contribution guidelines to "IterableType" rule
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
2dc8e72844
commit
df9ae14100
|
@ -2,8 +2,9 @@
|
|||
|
||||
- `IterableType()`
|
||||
|
||||
Validates if the input is iterable, in other words, if you're able to iterate
|
||||
over it with [foreach](http://php.net/foreach) language construct.
|
||||
Validates whether the pseudo-type of the input is iterable or not, in other words,
|
||||
if you're able to iterate over it with [foreach](http://php.net/foreach) language
|
||||
construct.
|
||||
|
||||
```php
|
||||
v::iterableType()->validate([]); // true
|
||||
|
|
|
@ -13,8 +13,14 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class IterableTypeException extends ValidationException
|
||||
/**
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
final class IterableTypeException extends ValidationException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} must be iterable',
|
||||
|
|
38
library/Helpers/CanValidateIterable.php
Normal file
38
library/Helpers/CanValidateIterable.php
Normal file
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Respect/Validation.
|
||||
*
|
||||
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
*
|
||||
* For the full copyright and license information, please view the "LICENSE.md"
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Respect\Validation\Helpers;
|
||||
|
||||
use stdClass;
|
||||
use Traversable;
|
||||
use function is_array;
|
||||
|
||||
/**
|
||||
* Helper to handle iterable values.
|
||||
*
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
trait CanValidateIterable
|
||||
{
|
||||
/**
|
||||
* Returns whether the value is iterable or not.
|
||||
*
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isIterable($value): bool
|
||||
{
|
||||
return is_array($value) || $value instanceof stdClass || $value instanceof Traversable;
|
||||
}
|
||||
}
|
|
@ -14,10 +14,13 @@ declare(strict_types=1);
|
|||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Exceptions\ValidationException;
|
||||
use Respect\Validation\Helpers\CanValidateIterable;
|
||||
use Respect\Validation\Validatable;
|
||||
|
||||
class Each extends IterableType
|
||||
class Each extends AbstractRule
|
||||
{
|
||||
use CanValidateIterable;
|
||||
|
||||
public $itemValidator;
|
||||
public $keyValidator;
|
||||
|
||||
|
@ -31,7 +34,7 @@ class Each extends IterableType
|
|||
{
|
||||
$exceptions = [];
|
||||
|
||||
if (!parent::validate($input)) {
|
||||
if (!$this->isIterable($input)) {
|
||||
throw $this->reportError($input);
|
||||
}
|
||||
|
||||
|
@ -60,7 +63,7 @@ class Each extends IterableType
|
|||
|
||||
public function check($input): void
|
||||
{
|
||||
if (!parent::validate($input)) {
|
||||
if (!$this->isIterable($input)) {
|
||||
throw $this->reportError($input);
|
||||
}
|
||||
|
||||
|
@ -77,7 +80,7 @@ class Each extends IterableType
|
|||
|
||||
public function validate($input): bool
|
||||
{
|
||||
if (!parent::validate($input)) {
|
||||
if (!$this->isIterable($input)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -13,12 +13,22 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class IterableType extends AbstractRule
|
||||
use Respect\Validation\Helpers\CanValidateIterable;
|
||||
|
||||
/**
|
||||
* Validates whether the pseudo-type of the input is iterable or not.
|
||||
*
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
final class IterableType extends AbstractRule
|
||||
{
|
||||
use CanValidateIterable;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($input): bool
|
||||
{
|
||||
return is_array($input) ||
|
||||
$input instanceof \stdClass ||
|
||||
$input instanceof \Traversable;
|
||||
return $this->isIterable($input);
|
||||
}
|
||||
}
|
||||
|
|
38
tests/integration/rules/iterableType.phpt
Normal file
38
tests/integration/rules/iterableType.phpt
Normal file
|
@ -0,0 +1,38 @@
|
|||
--FILE--
|
||||
<?php
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\IterableTypeException;
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::iterableType()->check(3);
|
||||
} catch (IterableTypeException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::iterableType())->check([2, 3]);
|
||||
} catch (IterableTypeException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::iterableType()->assert('String');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::iterableType())->assert(new stdClass());
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
3 must be iterable
|
||||
`{ 2, 3 }` must not be iterable
|
||||
- "String" must be iterable
|
||||
- `[object] (stdClass: { })` must not be iterable
|
|
@ -1,11 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
v::iterableType()->assert([1, 2, 3]);
|
||||
v::iterableType()->check(new ArrayObject());
|
||||
?>
|
||||
--EXPECTF--
|
|
@ -1,16 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\IterableTypeException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::iterableType()->check(3);
|
||||
} catch (IterableTypeException $exception) {
|
||||
echo $exception->getMessage();
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
3 must be iterable
|
|
@ -1,16 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\IterableTypeException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::not(v::iterableType())->check([2, 3]);
|
||||
} catch (IterableTypeException $exception) {
|
||||
echo $exception->getMessage();
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
`{ 2, 3 }` must not be iterable
|
|
@ -1,16 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\AllOfException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::iterableType()->assert('String');
|
||||
} catch (AllOfException $exception) {
|
||||
echo $exception->getFullMessage();
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
- "String" must be iterable
|
|
@ -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::iterableType())->assert(new stdClass());
|
||||
} catch (AllOfException $exception) {
|
||||
echo $exception->getFullMessage();
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
- `[object] (stdClass: { })` must not be iterable
|
|
@ -13,13 +13,19 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use ArrayIterator;
|
||||
use Respect\Validation\Test\RuleTestCase;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
* @group rule
|
||||
*
|
||||
* @covers \Respect\Validation\Rules\IterableType
|
||||
*
|
||||
* @author Guilherme Siani <guilherme@siani.com.br>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
class IterableTypeTest extends RuleTestCase
|
||||
final class IterableTypeTest extends RuleTestCase
|
||||
{
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
|
@ -27,8 +33,8 @@ class IterableTypeTest extends RuleTestCase
|
|||
|
||||
return [
|
||||
[$rule, [1, 2, 3]],
|
||||
[$rule, new \stdClass()],
|
||||
[$rule, new \ArrayIterator()],
|
||||
[$rule, new stdClass()],
|
||||
[$rule, new ArrayIterator()],
|
||||
];
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue