2010-12-06 19:30:56 +01:00
|
|
|
<?php
|
2015-06-08 16:47:14 +02:00
|
|
|
|
2015-06-08 17:09:25 +02:00
|
|
|
/*
|
|
|
|
* This file is part of Respect/Validation.
|
|
|
|
*
|
|
|
|
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
|
|
|
*
|
2019-05-23 16:21:34 +02:00
|
|
|
* For the full copyright and license information, please view the LICENSE file
|
|
|
|
* that was distributed with this source code.
|
2015-06-08 17:09:25 +02:00
|
|
|
*/
|
|
|
|
|
2017-11-19 19:01:31 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2010-12-06 19:30:56 +01:00
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
2019-02-04 00:29:13 +01:00
|
|
|
use Respect\Validation\Exceptions\EachException;
|
2010-12-06 21:35:43 +01:00
|
|
|
use Respect\Validation\Exceptions\ValidationException;
|
2018-06-01 13:56:01 +02:00
|
|
|
use Respect\Validation\Helpers\CanValidateIterable;
|
2015-10-18 03:44:47 +02:00
|
|
|
use Respect\Validation\Validatable;
|
2010-12-06 21:35:43 +01:00
|
|
|
|
2018-06-17 01:20:47 +02:00
|
|
|
/**
|
|
|
|
* Validates whether each value in the input is valid according to another rule.
|
|
|
|
*
|
|
|
|
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
|
|
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
|
|
|
* @author Nick Lombard <github@jigsoft.co.za>
|
|
|
|
* @author William Espindola <oi@williamespindola.com.br>
|
|
|
|
*/
|
|
|
|
final class Each extends AbstractRule
|
2010-12-06 19:30:56 +01:00
|
|
|
{
|
2018-06-01 13:56:01 +02:00
|
|
|
use CanValidateIterable;
|
|
|
|
|
2018-06-17 01:20:47 +02:00
|
|
|
/**
|
|
|
|
* @var Validatable
|
|
|
|
*/
|
|
|
|
private $rule;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the constructor.
|
|
|
|
*/
|
|
|
|
public function __construct(Validatable $rule)
|
2010-12-06 21:35:43 +01:00
|
|
|
{
|
2018-06-17 01:20:47 +02:00
|
|
|
$this->rule = $rule;
|
2010-12-06 21:35:43 +01:00
|
|
|
}
|
|
|
|
|
2018-06-17 01:20:47 +02:00
|
|
|
/**
|
2019-04-05 14:33:00 +02:00
|
|
|
* {@inheritDoc}
|
2018-06-17 01:20:47 +02:00
|
|
|
*/
|
2018-01-28 17:16:53 +01:00
|
|
|
public function assert($input): void
|
2010-12-06 21:35:43 +01:00
|
|
|
{
|
2018-06-01 13:56:01 +02:00
|
|
|
if (!$this->isIterable($input)) {
|
2010-12-06 21:35:43 +01:00
|
|
|
throw $this->reportError($input);
|
2013-01-14 22:55:03 +01:00
|
|
|
}
|
2011-04-28 01:41:12 +02:00
|
|
|
|
2018-06-17 01:20:47 +02:00
|
|
|
$exceptions = [];
|
|
|
|
foreach ($input as $value) {
|
|
|
|
try {
|
2020-05-16 12:06:19 +02:00
|
|
|
$this->rule->assert($value);
|
2018-06-17 01:20:47 +02:00
|
|
|
} catch (ValidationException $exception) {
|
|
|
|
$exceptions[] = $exception;
|
2013-01-14 22:55:03 +01:00
|
|
|
}
|
2011-11-29 22:20:07 +01:00
|
|
|
}
|
2011-02-20 17:59:53 +01:00
|
|
|
|
2013-01-14 22:55:03 +01:00
|
|
|
if (!empty($exceptions)) {
|
2019-02-04 00:29:13 +01:00
|
|
|
/** @var EachException $eachException */
|
|
|
|
$eachException = $this->reportError($input);
|
|
|
|
$eachException->addChildren($exceptions);
|
|
|
|
|
|
|
|
throw $eachException;
|
2013-01-14 22:55:03 +01:00
|
|
|
}
|
2010-12-06 21:35:43 +01:00
|
|
|
}
|
|
|
|
|
2020-05-16 12:06:19 +02:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function check($input): void
|
|
|
|
{
|
|
|
|
if (!$this->isIterable($input)) {
|
|
|
|
throw $this->reportError($input);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($input as $value) {
|
|
|
|
$this->rule->check($value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-17 01:20:47 +02:00
|
|
|
/**
|
2019-04-05 14:33:00 +02:00
|
|
|
* {@inheritDoc}
|
2018-06-17 01:20:47 +02:00
|
|
|
*/
|
2018-01-28 15:41:31 +01:00
|
|
|
public function validate($input): bool
|
2011-02-07 11:52:18 +01:00
|
|
|
{
|
2018-06-17 01:20:47 +02:00
|
|
|
try {
|
2020-05-16 12:06:19 +02:00
|
|
|
$this->check($input);
|
2018-06-17 01:20:47 +02:00
|
|
|
} catch (ValidationException $exception) {
|
2011-10-06 02:07:15 +02:00
|
|
|
return false;
|
2013-01-14 22:55:03 +01:00
|
|
|
}
|
|
|
|
|
2010-12-06 21:35:43 +01:00
|
|
|
return true;
|
|
|
|
}
|
2011-02-10 21:49:48 +01:00
|
|
|
}
|