In::isNot()   A
last analyzed

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
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
cc 1
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\DumpedRuleInterface;
13
use Yiisoft\Validator\SkipOnEmptyInterface;
14
use Yiisoft\Validator\SkipOnErrorInterface;
15
use Yiisoft\Validator\WhenInterface;
16
17
/**
18
 * Defines validation options to check that the value is one of the values provided in {@see $values}.
19
 * If the {@see In::$not} is set, the validation logic is inverted and the rule will ensure that the value
20
 * is NOT one of them.
21
 *
22
 * In case of the validated value being a list, the order of values is important.
23
 *
24
 * Nested arrays are supported too in both {@see values} argument and in the validated value (the order of values in
25
 * lists must match, the order of keys in associative arrays is not important).
26
 *
27
 * If the validated value is a set, use {@see Subset} instead.
28
 *
29
 * @see InHandler
30 3
 *
31
 * @psalm-import-type WhenType from WhenInterface
32
 */
33
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
34
final class In implements DumpedRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface
35
{
36
    use SkipOnEmptyTrait;
37
    use SkipOnErrorTrait;
38
    use WhenTrait;
39
40
    /**
41
     * @param iterable $values A set of values to check against. Nested arrays are supported too (the order of values in
42
     * lists must match, the order of keys in associative arrays is not important).
43
     * @param bool $strict Whether the comparison to each value in the set is strict:
44
     *
45
     * - Strict mode uses `===` operator meaning the type and the value must both match.
46
     * - Non-strict mode uses `==` operator meaning that type juggling is performed first before the comparison. You can
47
     * read more in the PHP docs:
48
     *
49
     * - {@link https://www.php.net/manual/en/language.operators.comparison.php}
50
     * - {@link https://www.php.net/manual/en/types.comparisons.php}
51
     * - {@link https://www.php.net/manual/en/language.types.type-juggling.php}
52
     *
53
     * Defaults to `false` meaning non-strict mode is used.
54
     * @param bool $not Whether to invert the validation logic. Defaults to `false`. If set to `true`, the value must NOT
55
     * be among the list of {@see $values}.
56
     * @param string $message Error message when the value is not in a set of value.
57
     *
58 1
     * You may use the following placeholders in the message:
59
     *
60 1
     * - `{attribute}`: the name of the attribute.
61
     * @param bool|callable|null $skipOnEmpty Whether to skip this rule if the value validated is empty.
62
     * See {@see SkipOnEmptyInterface}.
63 38
     * @param bool $skipOnError Whether to skip this rule if any of the previous rules gave an error.
64
     * See {@see SkipOnErrorInterface}.
65 38
     * @param Closure|null $when A callable to define a condition for applying the rule.
66
     * See {@see WhenInterface}.
67
     *
68 38
     * @psalm-param WhenType $when
69
     */
70 38
    public function __construct(
71
        private iterable $values,
72
        private bool $strict = false,
73 38
        private bool $not = false,
74
        private string $message = 'This value is not in the list of acceptable values.',
75 38
        private mixed $skipOnEmpty = null,
76
        private bool $skipOnError = false,
77
        private Closure|null $when = null,
78 22
    ) {
79
    }
80 22
81
    public function getName(): string
82
    {
83 3
        return self::class;
84
    }
85
86 3
    /**
87 3
     * Get a set of values to check against.
88 3
     *
89
     * @return iterable A set of values.
90 3
     */
91
    public function getValues(): iterable
92
    {
93 3
        return $this->values;
94 3
    }
95
96
    /**
97
     * Whether the comparison is strict (both type and value must be the same).
98 38
     *
99
     * @return bool Whether the comparison is strict.
100 38
     */
101
    public function isStrict(): bool
102
    {
103
        return $this->strict;
104
    }
105
106
    /**
107
     * Whether to invert the validation logic. Defaults to `false`. If set to `true`, the value must NOT
108
     * be among the list of {@see $values}.
109
     *
110
     * @return bool Whether to invert the validation logic.
111
     */
112
    public function isNot(): bool
113
    {
114
        return $this->not;
115
    }
116
117
    /**
118
     * Get error message when the value is not in a set of {@see $values}.
119
     *
120
     * @return string Error message.
121
     */
122
    public function getMessage(): string
123
    {
124
        return $this->message;
125
    }
126
127
    public function getOptions(): array
128
    {
129
        return [
130
            'values' => $this->values,
131
            'strict' => $this->strict,
132
            'not' => $this->not,
133
            'message' => [
134
                'template' => $this->message,
135
                'parameters' => [],
136
            ],
137
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
138
            'skipOnError' => $this->skipOnError,
139
        ];
140
    }
141
142
    public function getHandler(): string
143
    {
144
        return InHandler::class;
145
    }
146
}
147