Passed
Pull Request — master (#222)
by Alexander
04:47 queued 02:23
created

BooleanValidator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 25
ccs 11
cts 11
cp 1
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 23 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule\Boolean;
6
7
use Yiisoft\Validator\Result;
8
use Yiisoft\Validator\Rule\RuleValidatorInterface;
9
use Yiisoft\Validator\ValidationContext;
10
use Yiisoft\Validator\ValidatorInterface;
11
12
/**
13
 * Checks if the value is a boolean value or a value corresponding to it.
14
 */
15
final class BooleanValidator implements RuleValidatorInterface
16
{
17 21
    public function validate(mixed $value, object $rule, ValidatorInterface $validator, ?ValidationContext $context = null): Result
18
    {
19 21
        if ($rule->strict) {
20 8
            $valid = $value === $rule->trueValue || $value === $rule->falseValue;
21
        } else {
22 13
            $valid = $value == $rule->trueValue || $value == $rule->falseValue;
23
        }
24
25 21
        $result = new Result();
26
27 21
        if ($valid) {
28 13
            return $result;
29
        }
30
31 8
        $result->addError($rule->message, [
32
            // TODO: get reasons to do like this
33
            //            'true' => $config->trueValue === true ? 'true' : $config->trueValue,
34
            //            'false' => $config->falseValue === false ? 'false' : $config->falseValue,
35 8
            'true' => $rule->trueValue,
36 8
            'false' => $rule->falseValue,
37
        ]);
38
39 8
        return $result;
40
    }
41
}
42