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

InRangeValidator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 15
ccs 7
cts 7
cp 1
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 13 4
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