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

TrueValue::getMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
 * A variation of {@see BooleanValue} rule limiting the allowed values to "true" only (not limited to boolean "true"
19
 * type). What value exactly is considered "true" can be configured via {@see $trueValue} setting. There is also an
20
 * option to choose between strict and non-strict mode of comparison (see {@see $strict}).
21
 *
22
 * A typical scope of application is a user agreement. If the purpose is to also check the falsiness, use
23
 * {@see BooleanValue} rule instead.
24
 *
25
 * @see TrueValueHandler 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 TrueValue implements RuleWithOptionsInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface
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 bool $strict Whether the comparison to {@see $trueValue} is strict:
40
     *
41
     * - Strict mode uses `===` operator meaning the type and the value must both match to those set in
42
     * {@see $trueValue}.
43
     * - Non-strict mode uses `==` operator meaning that type juggling is performed first before the comparison. You can
44
     * read more in the PHP docs:
45
     *
46
     * - https://www.php.net/manual/en/language.operators.comparison.php
47
     * - https://www.php.net/manual/en/types.comparisons.php
48
     * - https://www.php.net/manual/en/language.types.type-juggling.php
49
     *
50
     * Defaults to `false` meaning non-strict mode is used.
51
     * @param string $incorrectInputMessage Error message used when validation fails because the type of validated value
52
     * is incorrect. Only scalar values are allowed - either int, float, string or bool. Used for more predictable
53
     * formatting.
54
     *
55
     * You may use the following placeholders in the message:
56
     *
57
     * - `{attribute}`: the label of the attribute being validated.
58
     * - `{true}`: the value set in {@see $trueValue} option.
59
     * - `{type}`: the type of the value being validated.
60
     * @param string $message Error message used when validation fails because the validated value does not match
61
     * neither "true" nor "false" values.
62
     *
63
     * You may use the following placeholders in the message:
64
     *
65
     * - `{attribute}`: the label of the attribute being validated.
66
     * - `{true}`: the value set in {@see $trueValue} option.
67
     * - `{value}`: the value being validated.
68
     * @param bool|callable|null $skipOnEmpty Whether to skip this rule if the validated value is empty / not passed.
69
     * See {@see SkipOnEmptyInterface}.
70
     * @param bool $skipOnError Whether to skip this rule if any of the previous rules gave an error. See
71
     * {@see SkipOnErrorInterface}.
72
     * @param Closure|null $when A callable to define a condition for applying the rule. See {@see WhenInterface}.
73
     * @psalm-param WhenType $when
74
     */
75
    public function __construct(
76
        private int|float|string|bool $trueValue = '1',
77
        private bool $strict = false,
78
        private string $incorrectInputMessage = 'The allowed types are integer, float, string, boolean. {type} given.',
79
        private string $message = 'The value must be "{true}".',
80
        private mixed $skipOnEmpty = null,
81
        private bool $skipOnError = false,
82
        private Closure|null $when = null,
83
    ) {
84
    }
85
86
    public function getName(): string
87
    {
88
        return 'isTrue';
89
    }
90
91
    /**
92
     * Gets the value that is considered to be "true".
93
     *
94
     * @return scalar A scalar value.
95
     *
96
     * @see $trueValue
97
     */
98
    public function getTrueValue(): int|float|string|bool
99
    {
100
        return $this->trueValue;
101
    }
102
103
    /**
104
     * Whether the comparison to {@see $trueValue} is strict.
105
     *
106
     * @return bool `true` - strict, `false` - non-strict.
107
     *
108
     * @see $strict
109
     */
110
    public function isStrict(): bool
111
    {
112
        return $this->strict;
113
    }
114
115
    /**
116
     * Gets error message used when validation fails because the type of validated value is incorrect.
117
     *
118
     * @return string Error message / template.
119
     *
120
     * @see $incorrectInputMessage
121
     */
122
    public function getIncorrectInputMessage(): string
123
    {
124
        return $this->incorrectInputMessage;
125
    }
126
127
    /**
128
     * Gets error message used when validation fails because the validated value does not match neither "true" nor
129
     * "false" values.
130
     *
131
     * @return string Error message / template.
132
     *
133
     * @see $message
134
     */
135
    public function getMessage(): string
136
    {
137
        return $this->message;
138
    }
139
140
    public function getOptions(): array
141
    {
142
        $messageParameters = [
143
            'true' => $this->trueValue === true ? 'true' : $this->trueValue,
144
        ];
145
146
        return [
147
            'trueValue' => $this->trueValue,
148
            'strict' => $this->strict,
149
            'incorrectInputMessage' => [
150
                'template' => $this->incorrectInputMessage,
151
                'parameters' => $messageParameters,
152
            ],
153
            'message' => [
154
                'template' => $this->message,
155
                'parameters' => $messageParameters,
156
            ],
157
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
158
            'skipOnError' => $this->skipOnError,
159
        ];
160
    }
161
162
    public function getHandler(): string
163
    {
164
        return TrueValueHandler::class;
165
    }
166
}
167