Apply contribution guidelines to "IterableType" rule

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Henrique Moody 2018-06-01 13:56:01 +02:00
parent 2dc8e72844
commit df9ae14100
No known key found for this signature in database
GPG key ID: 221E9281655813A6
12 changed files with 117 additions and 90 deletions

View file

@ -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

View file

@ -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',

View 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;
}
}

View file

@ -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;
}

View file

@ -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);
}
}

View 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

View file

@ -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--

View file

@ -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

View file

@ -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

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::iterableType()->assert('String');
} catch (AllOfException $exception) {
echo $exception->getFullMessage();
}
?>
--EXPECTF--
- "String" must be iterable

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::iterableType())->assert(new stdClass());
} catch (AllOfException $exception) {
echo $exception->getFullMessage();
}
?>
--EXPECTF--
- `[object] (stdClass: { })` must not be iterable

View file

@ -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()],
];
}