Passed
Pull Request — master (#395)
by
unknown
02:49
created

InHandler   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 14
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

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;
6
7
use Yiisoft\Arrays\ArrayHelper;
8
use Yiisoft\Validator\Exception\UnexpectedRuleException;
9
use Yiisoft\Validator\Result;
10
use Yiisoft\Validator\RuleHandlerInterface;
11
use Yiisoft\Validator\ValidationContext;
12
13
/**
14
 * A handler for {@see In} rule.
15
 */
16
final class InHandler implements RuleHandlerInterface
17
{
18 39
    public function validate(mixed $value, object $rule, ValidationContext $context): Result
19
    {
20 39
        if (!$rule instanceof In) {
21 1
            throw new UnexpectedRuleException(In::class, $rule);
22
        }
23
24 38
        $result = new Result();
25 38
        if ($rule->isNot() === ArrayHelper::isIn($value, $rule->getValues(), $rule->isStrict())) {
26 22
            $result->addError($rule->getMessage(), ['attribute' => $context->getAttribute()]);
27
        }
28
29 38
        return $result;
30
    }
31
}
32