Passed
Pull Request — master (#491)
by
unknown
02:41
created

BooleanValue::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 8
dl 0
loc 10
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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 TrueValue} rule instead.
24
 *
25
 * @see BooleanValueHandler Corresponding handler performing the actual validation.
26
 *
27
 * @psalm-import-type WhenType from WhenInterface
28
 */
29
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
30
final class BooleanValue implements RuleWithOptionsInterface, SkipOnEmptyInterface, SkipOnErrorInterface, WhenInterface
31
{
32
    use SkipOnEmptyTrait;
33
    use SkipOnErrorTrait;
34
    use WhenTrait;
35
36
    /**
37
     * @param scalar $trueValue The value that is considered to be "true". Only scalar values (either int, float, string
38
     * or bool) are allowed. Defaults to `1` string.
39
     * @param scalar $falseValue The value that is considered to be "false". Only scalar values (either int, float,
40
     * string or bool) are allowed. Defaults to `0` string.
41
     * @param bool $strict Whether the comparison to {@see $trueValue} and {@see $falseValue} is strict:
42
     *
43
     * - Strict mode uses `===` operator meaning the type and the value must both match to those set in
44
     * {@see $trueValue} or {@see $falseValue}.
45
     * - Non-strict mode uses `==` operator meaning that type juggling is performed first before the comparison. You can
46
     * read more in the PHP docs:
47
     *
48
     * - {@link https://www.php.net/manual/en/language.operators.comparison.php}
49
     * - {@link https://www.php.net/manual/en/types.comparisons.php}
50
     * - {@link https://www.php.net/manual/en/language.types.type-juggling.php}
51
     *
52
     * Defaults to `false` meaning non-strict mode is used.
53
     * @param string $incorrectInputMessage Error message used when validation fails because the type of validated value
54
     * is incorrect. Only scalar values are allowed - either int, float, string or bool. Used for more predictable
55
     * formatting.
56
     *
57
     * You may use the following placeholders in the message:
58
     *
59
     * - `{attribute}`: the label of the attribute being validated.
60
     * - `{true}`: the value set in {@see $trueValue} option.
61
     * - `{false}`: the value set in {@see $falseValue} option.
62
     * - `{type}`: the type of the value being validated.
63
     * @param string $message Error message used when validation fails because the validated value does not match
64
     * neither "true" nor "false" values.
65
     *
66
     * You may use the following placeholders in the message:
67
     *
68
     * - `{attribute}`: the label of the attribute being validated.
69
     * - `{true}`: the value set in {@see $trueValue} option.
70
     * - `{false}`: the value set in {@see $falseValue} option.
71
     * - `{value}`: the value being validated.
72
     * @param bool|callable|null $skipOnEmpty Whether to skip this rule if the validated value is empty / not passed.
73
     * See {@see SkipOnEmptyInterface}.
74
     * @param bool $skipOnError Whether to skip this rule if any of the previous rules gave an error. See
75
     * {@see SkipOnErrorInterface}.
76
     * @param Closure|null $when A callable to define a condition for applying the rule. See {@see WhenInterface}.
77
     * @psalm-param WhenType $when
78
     */
79
    public function __construct(
80
        private int|float|string|bool $trueValue = '1',
81
        private int|float|string|bool $falseValue = '0',
82
        private bool $strict = false,
83
        private string $incorrectInputMessage = 'The allowed types are integer, float, string, boolean. {type} given.',
84
        private string $message = 'Value must be either "{true}" or "{false}".',
85
        private mixed $skipOnEmpty = null,
86
        private bool $skipOnError = false,
87
        private Closure|null $when = null,
88
    ) {
89
    }
90
91
    public function getName(): string
92
    {
93
        return 'boolean';
94
    }
95
96
    /**
97
     * Gets the value that is considered to be "true".
98
     *
99
     * @return scalar A scalar value.
100
     *
101
     * @see $trueValue
102
     */
103
    public function getTrueValue(): int|float|string|bool
104
    {
105
        return $this->trueValue;
106
    }
107
108
    /**
109
     * Gets the value that is considered to be "false".
110
     *
111
     * @return scalar A scalar value.
112
     *
113
     * @see $falseValue
114
     */
115
    public function getFalseValue(): int|float|string|bool
116
    {
117
        return $this->falseValue;
118
    }
119
120
    /**
121
     * Whether the comparison to {@see $trueValue} and {@see $falseValue} is strict.
122
     *
123
     * @return bool `true` - strict, `false` - non-strict.
124
     *
125
     * @see $strict
126
     */
127
    public function isStrict(): bool
128
    {
129
        return $this->strict;
130
    }
131
132
    /**
133
     * Gets error message used when validation fails because the type of validated value is incorrect.
134
     *
135
     * @return string Error message / template.
136
     *
137
     * @see $incorrectInputMessage
138
     */
139
    public function getIncorrectInputMessage(): string
140
    {
141
        return $this->incorrectInputMessage;
142
    }
143
144
    /**
145
     * Error message used when validation fails because the validated value does not match neither "true" nor "false"
146
     * values.
147
     *
148
     * @return string Error message / template.
149
     *
150
     * @see $message
151
     */
152
    public function getMessage(): string
153
    {
154
        return $this->message;
155
    }
156
157
    public function getOptions(): array
158
    {
159
        $messageParameters = [
160
            'true' => $this->trueValue === true ? 'true' : $this->trueValue,
161
            'false' => $this->falseValue === false ? 'false' : $this->falseValue,
162
        ];
163
164
        return [
165
            'trueValue' => $this->trueValue,
166
            'falseValue' => $this->falseValue,
167
            'strict' => $this->strict,
168
            'incorrectInputMessage' => [
169
                'template' => $this->incorrectInputMessage,
170
                'parameters' => $messageParameters,
171
            ],
172
            'message' => [
173
                'template' => $this->message,
174
                'parameters' => $messageParameters,
175
            ],
176
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
177
            'skipOnError' => $this->skipOnError,
178
        ];
179
    }
180
181
    public function getHandler(): string
182
    {
183
        return BooleanValueHandler::class;
184
    }
185
}
186