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

IsTrue::getHandler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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