Passed
Pull Request — master (#222)
by Dmitriy
12:29
created

GroupRuleHandler::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 4
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Yiisoft\Validator\Result;
8
use Yiisoft\Validator\Rule\RuleHandlerInterface;
9
use Yiisoft\Validator\ValidationContext;
10
use Yiisoft\Validator\ValidatorInterface;
11
use Yiisoft\Validator\Exception\UnexpectedRuleException;
12
13
/**
14
 * Validates a single value for a set of custom rules.
15
 */
16
class GroupRuleHandler implements RuleHandlerInterface
17
{
18 6
    public function validate(mixed $value, object $rule, ValidatorInterface $validator, ?ValidationContext $context = null): Result
19
    {
20 6
        if (!$rule instanceof GroupRule) {
21 1
            throw new UnexpectedRuleException(GroupRule::class, $rule);
22
        }
23
24 5
        $result = new Result();
25 5
        if (!$validator->validate($value, $rule->getRuleSet())->isValid()) {
26 4
            $result->addError($rule->message);
27
        }
28
29 5
        return $result;
30
    }
31
}
32