Passed
Pull Request — master (#222)
by Dmitriy
02:36
created

GroupRuleValidator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 14
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 7
c 1
b 0
f 0
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 12 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule\GroupRule;
6
7
use Yiisoft\Validator\Result;
8
use Yiisoft\Validator\Rule\AtLeast\AtLeast;
9
use Yiisoft\Validator\Rule\RuleValidatorInterface;
10
use Yiisoft\Validator\ValidationContext;
11
use Yiisoft\Validator\ValidatorInterface;
12
use Yiisoft\Validator\Exception\UnexpectedRuleException;
13
14
/**
15
 * Validates a single value for a set of custom rules.
16
 */
17
class GroupRuleValidator implements RuleValidatorInterface
18
{
19 6
    public function validate(mixed $value, object $rule, ValidatorInterface $validator, ?ValidationContext $context = null): Result
20
    {
21 6
        if (!$rule instanceof GroupRule) {
22 1
            throw new UnexpectedRuleException(GroupRule::class, $rule);
23
        }
24
25 5
        $result = new Result();
26 5
        if (!$validator->validate($value, $rule->getRuleSet())->isValid()) {
27 4
            $result->addError($rule->message);
28
        }
29
30 5
        return $result;
31
    }
32
}
33