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

BooleanValidator::validate()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
c 1
b 0
f 0
nc 8
nop 4
dl 0
loc 23
ccs 11
cts 11
cp 1
crap 5
rs 9.6111
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