respect-validation/library/Rules/Each.php
Henrique Moody df9ae14100
Apply contribution guidelines to "IterableType" rule
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-06-01 14:23:41 +02:00

100 lines
2.5 KiB
PHP

<?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\Rules;
use Respect\Validation\Exceptions\ValidationException;
use Respect\Validation\Helpers\CanValidateIterable;
use Respect\Validation\Validatable;
class Each extends AbstractRule
{
use CanValidateIterable;
public $itemValidator;
public $keyValidator;
public function __construct(Validatable $itemValidator = null, Validatable $keyValidator = null)
{
$this->itemValidator = $itemValidator;
$this->keyValidator = $keyValidator;
}
public function assert($input): void
{
$exceptions = [];
if (!$this->isIterable($input)) {
throw $this->reportError($input);
}
foreach ($input as $key => $item) {
if (isset($this->itemValidator)) {
try {
$this->itemValidator->assert($item);
} catch (ValidationException $e) {
$exceptions[] = $e;
}
}
if (isset($this->keyValidator)) {
try {
$this->keyValidator->assert($key);
} catch (ValidationException $e) {
$exceptions[] = $e;
}
}
}
if (!empty($exceptions)) {
throw $this->reportError($input)->setRelated($exceptions);
}
}
public function check($input): void
{
if (!$this->isIterable($input)) {
throw $this->reportError($input);
}
foreach ($input as $key => $item) {
if (isset($this->itemValidator)) {
$this->itemValidator->check($item);
}
if (isset($this->keyValidator)) {
$this->keyValidator->check($key);
}
}
}
public function validate($input): bool
{
if (!$this->isIterable($input)) {
return false;
}
foreach ($input as $key => $item) {
if (isset($this->itemValidator) && !$this->itemValidator->validate($item)) {
return false;
}
if (isset($this->keyValidator) && !$this->keyValidator->validate($key)) {
return false;
}
}
return true;
}
}