Passed
Pull Request — master (#485)
by
unknown
02:33
created

Boolean::getNonScalarMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Attribute;
8
use Closure;
9
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
10
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
11
use Yiisoft\Validator\Rule\Trait\WhenTrait;
12
use Yiisoft\Validator\RuleWithOptionsInterface;
13
use Yiisoft\Validator\SkipOnEmptyInterface;
14
use Yiisoft\Validator\SkipOnErrorInterface;
15
use Yiisoft\Validator\WhenInterface;
16
17
/**
18
 * Contains a set of options to determine if the value is "true" or "false", not limited to boolean type only. What
19
 * values exactly are considered "true" and "false" can be configured via {@see $trueValue} and {@see $falseValue}
20
 * settings accordingly. There is also an option to choose between strict and non-strict mode of comparison
21
 * (see {@see $strict}).
22
 *
23
 * If the purpose is to check the truthiness only, use {@see IsTrue} rule instead.
24
 *
25
 * @see BooleanHandler Corresponding handler performing the actual validation.
26
 *
27
 * @psalm-import-type WhenType from WhenInterface
28 11
 */
29
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
30
final class Boolean implements RuleWithOptionsInterface, SkipOnEmptyInterface, SkipOnErrorInterface, WhenInterface
31
{
32
    use SkipOnEmptyTrait;
33
    use SkipOnErrorTrait;
34
    use WhenTrait;
35
36
    /**
37
     * @const Default message used for all cases.
38
     */
39
    private const DEFAULT_MESSAGE = 'Value must be either "{true}" or "{false}".';
40
41
    /**
42
     * @param scalar $trueValue The value that is considered to be "true". Only scalar values (either int, float, string
43
     * or bool) are allowed. Defaults to `1` string.
44
     * @param scalar $falseValue The value that is considered to be "false". Only scalar values (either int, float,
45
     * string or bool) are allowed. Defaults to `0` string.
46
     * @param bool $strict Whether the comparison to {@see $trueValue} and {@see $falseValue} is strict:
47
     *
48
     * - Strict mode uses `===` operator meaning the type and the value must both match to those set in
49
     * {@see $trueValue} or {@see $falseValue}.
50
     * - Non-strict mode uses `==` operator meaning that type juggling is performed first before the comparison. You can
51
     * read more in the PHP docs:
52
     *
53
     * - {@link https://www.php.net/manual/en/language.operators.comparison.php}
54
     * - {@link https://www.php.net/manual/en/types.comparisons.php}
55
     * - {@link https://www.php.net/manual/en/language.types.type-juggling.php}
56
     *
57
     * Defaults to `false` meaning non-strict mode is used.
58 2
     * @param string $messageWithType Error message used when validation fails and neither non-scalar value (int,
59
     * float, string, bool) nor null was provided as input. The type is used instead of value here for more predictable
60 2
     * formatting.
61
     *
62
     * You may use the following placeholders in the message:
63 31
     *
64
     * - `{attribute}`: the label of the attribute being validated.
65 31
     * - `{true}`: the value set in {@see $trueValue} option.
66
     * - `{false}`: the value set in {@see $falseValue} option.
67
     * - `{type}`: the type of the value being validated.
68 21
     * @param string $messageWithValue Error message used when validation fails and either scalar value (int, float,
69
     * string, bool) or null was provided as input.
70 21
     *
71
     * You may use the following placeholders in the message:
72
     *
73 22
     * - `{attribute}`: the label of the attribute being validated.
74
     * - `{true}`: the value set in {@see $trueValue} option.
75 22
     * - `{false}`: the value set in {@see $falseValue} option.
76
     * - `{value}`: the value being validated.
77
     * @param bool|callable|null $skipOnEmpty Whether to skip this rule if the validated value is empty / not passed.
78 9
     * See {@see SkipOnEmptyInterface}.
79
     * @param bool $skipOnError Whether to skip this rule if any of the previous rules gave an error. See
80 9
     * {@see SkipOnErrorInterface}.
81
     * @param Closure|null $when A callable to define a condition for applying the rule. See {@see WhenInterface}.
82
     * @psalm-param WhenType $when
83 8
     */
84
    public function __construct(
85 8
        private int|float|string|bool $trueValue = '1',
86
        private int|float|string|bool $falseValue = '0',
87
        private bool $strict = false,
88 4
        private string $messageWithType = self::DEFAULT_MESSAGE,
89
        private string $messageWithValue = self::DEFAULT_MESSAGE,
90 4
        private mixed $skipOnEmpty = null,
91 4
        private bool $skipOnError = false,
92 4
        private Closure|null $when = null,
93
    ) {
94
    }
95
96 4
    public function getName(): string
97 4
    {
98 4
        return 'boolean';
99
    }
100 4
101
    /**
102
     * A getter for {@see $trueValue} property.
103
     *
104 4
     * @return scalar The value that is considered to be "true".
105
     */
106
    public function getTrueValue(): int|float|string|bool
107 4
    {
108 4
        return $this->trueValue;
109
    }
110
111
    /**
112 31
     * A getter for {@see $falseValue} property.
113
     *
114 31
     * @return scalar The value that is considered to be "true".
115
     */
116
    public function getFalseValue(): int|float|string|bool
117
    {
118
        return $this->falseValue;
119
    }
120
121
    /**
122
     * A getter for {@see $strict} property.
123
     *
124
     * @return bool Whether the comparison to {@see $trueValue} and {@see $falseValue} is strict:
125
     */
126
    public function isStrict(): bool
127
    {
128
        return $this->strict;
129
    }
130
131
    /**
132
     * A getter for {@see $messageWithType}.
133
     *
134
     * @return string Error message.
135
     */
136
    public function getMessageWithType(): string
137
    {
138
        return $this->messageWithType;
139
    }
140
141
    /**
142
     * A getter for {@see $messageWithValue}.
143
     *
144
     * @return string Error message.
145
     */
146
    public function getMessageWithValue(): string
147
    {
148
        return $this->messageWithValue;
149
    }
150
151
    public function getOptions(): array
152
    {
153
        $messageParameters = [
154
            'true' => $this->trueValue === true ? 'true' : $this->trueValue,
155
            'false' => $this->falseValue === false ? 'false' : $this->falseValue,
156
        ];
157
158
        return [
159
            'trueValue' => $this->trueValue,
160
            'falseValue' => $this->falseValue,
161
            'strict' => $this->strict,
162
            'messageWithType' => [
163
                'template' => $this->messageWithType,
164
                'parameters' => $messageParameters,
165
            ],
166
            'messageWithValue' => [
167
                'template' => $this->messageWithValue,
168
                'parameters' => $messageParameters,
169
            ],
170
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
171
            'skipOnError' => $this->skipOnError,
172
        ];
173
    }
174
175
    public function getHandler(): string
176
    {
177
        return BooleanHandler::class;
178
    }
179
}
180