respect-validation/library/Rules/AllOf.php
Henrique Moody c80524b457
Method assert() should not have a return value
One this method should throw an exception when the input is not valid,
returning `TRUE` when it succeeds is not really consistent.
2018-01-28 17:38:40 +01:00

51 lines
1.2 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;
class AllOf extends AbstractComposite
{
public function assert($input): void
{
$exceptions = $this->validateRules($input);
$numRules = count($this->rules);
$numExceptions = count($exceptions);
$summary = [
'total' => $numRules,
'failed' => $numExceptions,
'passed' => $numRules - $numExceptions,
];
if (!empty($exceptions)) {
throw $this->reportError($input, $summary)->setRelated($exceptions);
}
}
public function check($input): void
{
foreach ($this->getRules() as $rule) {
$rule->check($input);
}
}
public function validate($input): bool
{
foreach ($this->getRules() as $rule) {
if (!$rule->validate($input)) {
return false;
}
}
return true;
}
}