respect-validation/library/Rules/AnyOf.php
2017-02-05 12:28:08 +01:00

63 lines
1.4 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.
*/
namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\ValidationException;
class AnyOf extends AbstractComposite
{
public function assert($input)
{
$validators = $this->getRules();
$exceptions = $this->validateRules($input);
$numRules = count($validators);
$numExceptions = count($exceptions);
if ($numExceptions === $numRules) {
throw $this->reportError($input)->setRelated($exceptions);
}
return true;
}
public function validate($input)
{
foreach ($this->getRules() as $v) {
if ($v->validate($input)) {
return true;
}
}
return false;
}
public function check($input)
{
foreach ($this->getRules() as $v) {
try {
if ($v->check($input)) {
return true;
}
} catch (ValidationException $e) {
if (!isset($firstException)) {
$firstException = $e;
}
}
}
if (isset($firstException)) {
throw $firstException;
}
return false;
}
}