Passed
Pull Request — master (#222)
by Alexander
02:58
created

Boolean   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 62
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0
wmc 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Attribute;
8
use Closure;
9
use JetBrains\PhpStorm\ArrayShape;
10
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_TRAIT, expecting T_STRING or '{' on line 10 at column 27
Loading history...
11
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait;
12
use Yiisoft\Validator\ParametrizedRuleInterface;
13
14
/**
15
 * Checks if the value is a boolean value or a value corresponding to it.
16
 */
17
#[Attribute(Attribute::TARGET_PROPERTY)]
18
final class Boolean implements ParametrizedRuleInterface
19
{
20
    use HandlerClassNameTrait;
21
    use RuleNameTrait;
22
23 1
    public function __construct(
24
        /**
25
         * @var mixed the value representing true status. Defaults to '1'.
26
         */
27
        private $trueValue = '1',
28
        /**
29
         * @var mixed the value representing false status. Defaults to '0'.
30
         */
31
        private $falseValue = '0',
32
        /**
33
         * @var bool whether the comparison to {@see $trueValue} and {@see $falseValue} is strict.
34
         * When this is `true`, the value and type must both match those of {@see $trueValue} or
35
         * {@see $falseValue}. Defaults to `false`, meaning only the value needs to be matched.
36
         */
37
        private bool $strict = false,
38
        private string $message = 'The value must be either "{true}" or "{false}".',
39
        private bool $skipOnEmpty = false,
40
        private bool $skipOnError = false,
41
        private ?Closure $when = null,
42
    ) {
43
    }
44
45
    /**
46
     * @return mixed
47
     */
48 21
    public function getTrueValue(): mixed
49
    {
50 21
        return $this->trueValue;
51
    }
52
53
    /**
54
     * @return mixed
55
     */
56 12
    public function getFalseValue(): mixed
57
    {
58 12
        return $this->falseValue;
59
    }
60
61
    /**
62
     * @return bool
63
     */
64 21
    public function isStrict(): bool
65
    {
66 21
        return $this->strict;
67
    }
68
69
    /**
70
     * @return string
71
     */
72 8
    public function getMessage(): string
73
    {
74 8
        return $this->message;
75
    }
76
77
    /**
78
     * @return bool
79
     */
80
    public function isSkipOnEmpty(): bool
81
    {
82
        return $this->skipOnEmpty;
83
    }
84
85
    /**
86
     * @return bool
87
     */
88
    public function isSkipOnError(): bool
89
    {
90
        return $this->skipOnError;
91
    }
92
93
    /**
94
     * @return Closure|null
95
     */
96
    public function getWhen(): ?Closure
97
    {
98
        return $this->when;
99
    }
100
101 7
    #[ArrayShape([
102
        'trueValue' => 'string',
103
        'falseValue' => 'string',
104
        'strict' => 'bool',
105
        'message' => 'array',
106
        'skipOnEmpty' => 'bool',
107
        'skipOnError' => 'bool',
108
    ])]
109
    public function getOptions(): array
110
    {
111
        return [
112 7
            'trueValue' => $this->trueValue,
113 7
            'falseValue' => $this->falseValue,
114 7
            'strict' => $this->strict,
115
            'message' => [
116 7
                'message' => $this->message,
117
                'parameters' => [
118
                    // TODO: get reasons to do like this
119
                    //  'true' => $this->trueValue === true ? 'true' : $this->trueValue,
120
                    //  'false' => $this->falseValue === false ? 'false' : $this->falseValue,
121 7
                    'true' => $this->trueValue,
122 7
                    'false' => $this->falseValue,
123
                ],
124
            ],
125 7
            'skipOnEmpty' => $this->skipOnEmpty,
126 7
            'skipOnError' => $this->skipOnError,
127
        ];
128
    }
129
}
130