Passed
Push — master ( 5c2907...37dc07 )
by Sergei
24:35 queued 21:56
created

InRangeHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 2
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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 among a list of values.
15
 *
16
 * The range can be specified via constructor.
17
 * If the {@see InRange::$not} is called, the rule will ensure the value is NOT among the specified range.
18
 */
19
final class InRangeHandler implements RuleHandlerInterface
20
{
21 39
    public function validate(mixed $value, object $rule, ValidationContext $context): Result
22
    {
23 39
        if (!$rule instanceof InRange) {
24 1
            throw new UnexpectedRuleException(InRange::class, $rule);
25
        }
26
27 38
        $result = new Result();
28 38
        if ($rule->isNot() === ArrayHelper::isIn($value, $rule->getRange(), $rule->isStrict())) {
29 22
            $result->addError(
30 22
                $rule->getMessage(),
31
                [
32 22
                    'attribute' => $context->getAttribute(),
33
                    'value' => $value,
34
                ],
35
            );
36
        }
37
38 38
        return $result;
39
    }
40
}
41