StopOnErrorHandler::validate()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 14
ccs 0
cts 0
cp 0
crap 20
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Yiisoft\Validator\Exception\UnexpectedRuleException;
8
use Yiisoft\Validator\Result;
9
use Yiisoft\Validator\RuleHandlerInterface;
10
use Yiisoft\Validator\ValidationContext;
11
12
/**
13
 * A handler for {@see StopOnError} rule. Validates a set of rules consecutively and stops at the rule where validation
14
 * has failed.
15
 */
16
final class StopOnErrorHandler implements RuleHandlerInterface
17
{
18
    public function validate(mixed $value, object $rule, ValidationContext $context): Result
19
    {
20
        if (!$rule instanceof StopOnError) {
21
            throw new UnexpectedRuleException(StopOnError::class, $rule);
22
        }
23
24
        foreach ($rule->getRules() as $relatedRule) {
25
            $result = $context->validate($value, $relatedRule);
26
            if (!$result->isValid()) {
27
                return $result;
28
            }
29
        }
30
31
        return new Result();
32
    }
33
}
34