TrueValue   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 137
rs 10
c 0
b 0
f 0
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getName() 0 3 1
A getHandler() 0 3 1
A getOptions() 0 19 2
A getIncorrectInputMessage() 0 3 1
A isStrict() 0 3 1
A getMessage() 0 3 1
A getTrueValue() 0 3 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\DumpedRuleInterface;
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::$trueValue} setting. There is
20
 * also an option to choose between strict and non-strict mode of comparison (see {@see TrueValue::$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 DumpedRuleInterface, 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 translated 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 translated 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
     *
74
     * @psalm-param WhenType $when
75
     */
76
    public function __construct(
77
        private int|float|string|bool $trueValue = '1',
78
        private bool $strict = false,
79
        private string $incorrectInputMessage = 'The allowed types are integer, float, string, boolean. {type} given.',
80
        private string $message = 'The value must be "{true}".',
81
        private mixed $skipOnEmpty = null,
82
        private bool $skipOnError = false,
83
        private Closure|null $when = null,
84
    ) {
85
    }
86
87
    public function getName(): string
88
    {
89
        return self::class;
90
    }
91
92
    /**
93
     * Gets the value that is considered to be "true".
94
     *
95
     * @return scalar A scalar value.
96
     *
97
     * @see $trueValue
98
     */
99
    public function getTrueValue(): int|float|string|bool
100
    {
101
        return $this->trueValue;
102
    }
103
104
    /**
105
     * Whether the comparison to {@see $trueValue} is strict.
106
     *
107
     * @return bool `true` - strict, `false` - non-strict.
108
     *
109
     * @see $strict
110
     */
111
    public function isStrict(): bool
112
    {
113
        return $this->strict;
114
    }
115
116
    /**
117
     * Gets error message used when validation fails because the type of validated value is incorrect.
118
     *
119
     * @return string Error message / template.
120
     *
121
     * @see $incorrectInputMessage
122
     */
123
    public function getIncorrectInputMessage(): string
124
    {
125
        return $this->incorrectInputMessage;
126
    }
127
128
    /**
129
     * Gets error message used when validation fails because the validated value does not match neither "true" nor
130
     * "false" values.
131
     *
132
     * @return string Error message / template.
133
     *
134
     * @see $message
135
     */
136
    public function getMessage(): string
137
    {
138
        return $this->message;
139
    }
140
141
    public function getOptions(): array
142
    {
143
        $messageParameters = [
144
            'true' => $this->trueValue === true ? 'true' : $this->trueValue,
145
        ];
146
147
        return [
148
            'trueValue' => $this->trueValue,
149
            'strict' => $this->strict,
150
            'incorrectInputMessage' => [
151
                'template' => $this->incorrectInputMessage,
152
                'parameters' => $messageParameters,
153
            ],
154
            'message' => [
155
                'template' => $this->message,
156
                'parameters' => $messageParameters,
157
            ],
158
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
159
            'skipOnError' => $this->skipOnError,
160
        ];
161
    }
162
163
    public function getHandler(): string
164
    {
165
        return TrueValueHandler::class;
166
    }
167
}
168