Passed
Push — master ( 779d44...2304a6 )
by
unknown
04:48 queued 02:05
created

Subset::getIncorrectInputMessage()   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
rs 10
ccs 0
cts 0
cp 0
crap 2
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 In} rule allowing to use the set of values instead of single value as an input for checking if
19
 * it's a subset of the set provided in {@see Subset::$values}.
20
 *
21
 * The order of items in the validated set is not important.
22
 *
23
 * Nested arrays are supported in both {@see $values} argument and in the validated value (the order of values in lists
24
 * must match, the order of keys in associative arrays is not important).
25 3
 *
26
 * @see SubsetHandler Corresponding handler performing the actual validation.
27
 *
28
 * @psalm-import-type WhenType from WhenInterface
29
 */
30
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
31
final class Subset implements RuleWithOptionsInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface
32
{
33
    use SkipOnEmptyTrait;
34
    use SkipOnErrorTrait;
35
    use WhenTrait;
36
37
    /**
38
     * @param iterable $values A set of values to check against. Nested arrays are supported too (the order of values in
39
     * lists must match, the order of keys in associative arrays is not important).
40
     * @param bool $strict Whether the comparison for each value in the set is strict:
41
     *
42
     * - Strict mode uses `===` operator meaning the type and the value must both match.
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
     * - {@link https://www.php.net/manual/en/language.operators.comparison.php}
47
     * - {@link https://www.php.net/manual/en/types.comparisons.php}
48
     * - {@link https://www.php.net/manual/en/language.types.type-juggling.php}
49 1
     *
50
     * Defaults to `false` meaning non-strict mode is used.
51 1
     * @param string $incorrectInputMessage Error message used when validation fails because the validated value is not
52
     * an iterable.
53
     *
54
     * You may use the following placeholders in the message:
55
     *
56
     * - `{attribute}`: the translated label of the attribute being validated.
57 12
     * - `{type}`: the type of the value being validated.
58
     * @param string $message Error message used when validation fails because the validated value is not a subset of
59 12
     * the set provided in {@see $values}.
60
     *
61
     * You may use the following placeholders in the message:
62 12
     *
63
     * - `{attribute}`: the translated label of the attribute being validated.
64 12
     * @param bool|callable|null $skipOnEmpty Whether to skip this rule if the validated value is empty / not passed.
65
     * See {@see SkipOnEmptyInterface}.
66
     * @param bool $skipOnError Whether to skip this rule if any of the previous rules gave an error. See
67 4
     * {@see SkipOnErrorInterface}.
68
     * @param Closure|null $when A callable to define a condition for applying the rule. See {@see WhenInterface}.
69 4
     * @psalm-param WhenType $when
70
     */
71
    public function __construct(
72 6
        private iterable $values,
73
        private bool $strict = false,
74 6
        private string $incorrectInputMessage = 'Value must be iterable.',
75
        private string $message = 'This value is not a subset of acceptable values.',
76
        private mixed $skipOnEmpty = null,
77 1
        private bool $skipOnError = false,
78
        private Closure|null $when = null,
79
    ) {
80 1
    }
81 1
82
    public function getName(): string
83 1
    {
84
        return 'subset';
85
    }
86
87 1
    /**
88
     * Gets a set of values to check against.
89
     *
90 1
     * @return iterable A set of values.
91 1
     */
92
    public function getValues(): iterable
93
    {
94
        return $this->values;
95 16
    }
96
97 16
    /**
98
     * Whether the comparison for each value in the set is strict.
99
     *
100
     * @return bool `true` - strict, `false` - non-strict.
101
     *
102
     * @see $strict
103
     */
104
    public function isStrict(): bool
105
    {
106
        return $this->strict;
107
    }
108
109
    /**
110
     * Gets error message used when validation fails because the type of validated value is incorrect.
111
     *
112
     * @return string Error message / template.
113
     *
114
     * @see $incorrectInputMessage
115
     */
116
    public function getIncorrectInputMessage(): string
117
    {
118
        return $this->incorrectInputMessage;
119
    }
120
121
    /**
122
     * Gets error message used when validation fails because the validated value is not a subset of the set provided in
123
     * {@see $values}.
124
     *
125
     * @return string Error message / template.
126
     *
127
     * @see $message
128
     */
129
    public function getMessage(): string
130
    {
131
        return $this->message;
132
    }
133
134
    public function getOptions(): array
135
    {
136
        return [
137
            'values' => $this->values,
138
            'strict' => $this->strict,
139
            'incorrectInputMessage' => [
140
                'template' => $this->incorrectInputMessage,
141
                'parameters' => [],
142
            ],
143
            'message' => [
144
                'template' => $this->message,
145
                'parameters' => [],
146
            ],
147
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
148
            'skipOnError' => $this->skipOnError,
149
        ];
150
    }
151
152
    public function getHandler(): string
153
    {
154
        return SubsetHandler::class;
155
    }
156
}
157