StopOnErrorHandler   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 16
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 16
ccs 0
cts 0
cp 0
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 14 4
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