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

InHandler::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 3
crap 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