Test Failed
Pull Request — master (#175)
by
unknown
12:57
created

Boolean   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 47.83%

Importance

Changes 0
Metric Value
eloc 20
c 0
b 0
f 0
dl 0
loc 62
ccs 11
cts 23
cp 0.4783
rs 10
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 23 1
A getFormattedMessage() 0 5 3
A validateValue() 0 18 5
A getOptions() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Yiisoft\Validator\FormatterInterface;
8
use Yiisoft\Validator\Result;
9
use Yiisoft\Validator\Rule;
10
use Yiisoft\Validator\ValidationContext;
11
12
/**
13
 * BooleanValidator checks if the attribute value is a boolean value or a value corresponding to it.
14
 */
15
final class Boolean extends Rule
16
{
17
    public function __construct(
18
        /**
19
         * @var mixed the value representing true status. Defaults to '1'.
20
         */
21
        private $trueValue = '1',
22
        /**
23
         * @var mixed the value representing false status. Defaults to '0'.
24
         */
25
        private $falseValue = '0',
26
        /**
27
         * @var bool whether the comparison to {@see $trueValue} and {@see $falseValue} is strict.
28
         * When this is true, the attribute value and type must both match those of {@see $trueValue} or
29
         * {@see $falseValue}. Defaults to `false`, meaning only the value needs to be matched.
30
         */
31
        private bool $strict = false,
32
        private string $message = 'The value must be either "{true}" or "{false}".',
33
34
        ?FormatterInterface $formatter = null,
35
        bool $skipOnEmpty = false,
36 2
        bool $skipOnError = false,
37
        $when = null
38 2
    ) {
39
        parent::__construct(formatter: $formatter, skipOnEmpty: $skipOnEmpty, skipOnError: $skipOnError, when: $when);
40
    }
41
42
    protected function validateValue($value, ?ValidationContext $context = null): Result
43
    {
44
        if ($this->strict) {
45
            $valid = $value === $this->trueValue || $value === $this->falseValue;
46
        } else {
47
            $valid = $value == $this->trueValue || $value == $this->falseValue;
48
        }
49
50
        $result = new Result();
51
52
        if ($valid) {
53
            return $result;
54
        }
55
56
        $message = $this->getFormattedMessage();
57
        $result->addError($message);
58
59
        return $result;
60
    }
61
62 20
    private function getFormattedMessage(): string
63
    {
64 20
        return $this->formatMessage( $this->message, [
65 8
            'true' => $this->trueValue === true ? 'true' : $this->trueValue,
66
            'false' => $this->falseValue === false ? 'false' : $this->falseValue,
67 12
        ]);
68
    }
69
70 20
    public function getOptions(): array
71
    {
72 20
        return array_merge(parent::getOptions(), [
73 7
            'trueValue' => $this->trueValue,
74 7
            'falseValue' => $this->falseValue,
75 7
            'strict' => $this->strict,
76
            'message' => $this->getFormattedMessage(),
77 7
        ]);
78 7
    }
79
}
80