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

InRangeValidator::validate()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
c 1
b 0
f 0
nc 3
nop 4
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule\InRange;
6
7
use Yiisoft\Arrays\ArrayHelper;
8
use Yiisoft\Validator\Result;
9
use Yiisoft\Validator\Rule\RuleValidatorInterface;
10
use Yiisoft\Validator\ValidationContext;
11
use Yiisoft\Validator\ValidatorInterface;
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 InRangeValidator implements RuleValidatorInterface
20
{
21 31
    public function validate(mixed $value, object $rule, ValidatorInterface $validator, ?ValidationContext $context = null): Result
22
    {
23 31
        $result = new Result();
24
25 31
        if ($rule->skipOnEmpty && $value === null) {
26 1
            return $result;
27
        }
28
29 30
        if ($rule->not === ArrayHelper::isIn($value, $rule->range, $rule->strict)) {
30 18
            $result->addError($rule->message);
31
        }
32
33 30
        return $result;
34
    }
35
}
36