Passed
Pull Request — master (#493)
by
unknown
02:48
created

Subset::getIterableMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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