InHandler::validate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
nc 3
nop 3
dl 0
loc 12
cc 3
ccs 6
cts 6
cp 1
crap 3
rs 10
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
 * Validates that the value is one of the values provided.
15
 *
16
 * @see In
17
 */
18 39
final class InHandler implements RuleHandlerInterface
19
{
20 39
    public function validate(mixed $value, object $rule, ValidationContext $context): Result
21 1
    {
22
        if (!$rule instanceof In) {
23
            throw new UnexpectedRuleException(In::class, $rule);
24 38
        }
25 38
26 22
        $result = new Result();
27
        if ($rule->isNot() === ArrayHelper::isIn($value, $rule->getValues(), $rule->isStrict())) {
28
            $result->addError($rule->getMessage(), ['attribute' => $context->getTranslatedAttribute()]);
29 38
        }
30
31
        return $result;
32
    }
33
}
34