Regex::getMessage()   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 InvalidArgumentException;
10
use JetBrains\PhpStorm\Language;
11
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
12
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
13
use Yiisoft\Validator\Rule\Trait\WhenTrait;
14
use Yiisoft\Validator\DumpedRuleInterface;
15
use Yiisoft\Validator\SkipOnEmptyInterface;
16
use Yiisoft\Validator\SkipOnErrorInterface;
17
use Yiisoft\Validator\WhenInterface;
18
19
/**
20
 * Defines validation options to check that the value matches the pattern specified in constructor.
21
 *
22
 * If the {@see Regex::$not} is used, the rule will ensure the value do NOT match the pattern.
23
 *
24
 * @see RegexHandler
25
 *
26
 * @psalm-import-type WhenType from WhenInterface
27
 */
28
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
29
final class Regex implements DumpedRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface
30
{
31 5
    use SkipOnEmptyTrait;
32
    use SkipOnErrorTrait;
33
    use WhenTrait;
34
35
    /**
36
     * @var string The regular expression to be matched with.
37
     * @psalm-var non-empty-string
38
     */
39
    private string $pattern;
40
41
    /**
42
     * @param string $pattern The regular expression to be matched with.
43
     * @param bool $not Whether to invert the validation logic. Defaults to `false`. If set to `true`, the regular
44
     * expression defined via {@see $pattern} should NOT match the value.
45
     * @param string $incorrectInputMessage A message used when the input is incorrect.
46
     *
47
     * You may use the following placeholders in the message:
48
     *
49
     * - `{attribute}`: the translated label of the attribute being validated.
50
     * - `{type}`: the type of the value being validated.
51
     * @param string $message A message used when the value does not match regular expression.
52
     *
53
     * You may use the following placeholders in the message:
54
     *
55
     * - `{attribute}`: the translated label of the attribute being validated.
56
     * - `{value}`: the value of the attribute being validated.
57 1
     * @param bool|callable|null $skipOnEmpty Whether to skip this rule if the value validated is empty.
58
     * See {@see SkipOnEmptyInterface}.
59 1
     * @param bool $skipOnError Whether to skip this rule if any of the previous rules gave an error.
60
     * See {@see SkipOnErrorInterface}.
61
     * @param Closure|null $when A callable to define a condition for applying the rule.
62 9
     * See {@see WhenInterface}.
63
     *
64 9
     * @psalm-param WhenType $when
65
     */
66
    public function __construct(
67 9
        #[Language('RegExp')]
68
        string $pattern,
69 9
        private bool $not = false,
70
        private string $incorrectInputMessage = 'The value must be a string.',
71
        private string $message = 'Value is invalid.',
72 9
        private mixed $skipOnEmpty = null,
73
        private bool $skipOnError = false,
74 9
        private Closure|null $when = null,
75
    ) {
76
        if ($pattern === '') {
77 6
            throw new InvalidArgumentException('Pattern can\'t be empty.');
78
        }
79 6
80
        $this->pattern = $pattern;
81
    }
82 2
83
    public function getName(): string
84
    {
85 2
        return self::class;
86 2
    }
87
88 2
    /**
89
     * Get the regular expression to be matched with.
90
     *
91
     * @return string The regular expression.
92 2
     * @psalm-return non-empty-string
93
     *
94
     * @see $pattern
95 2
     */
96 2
    public function getPattern(): string
97
    {
98
        return $this->pattern;
99
    }
100 18
101
    /**
102 18
     * Get whether to invert the validation logic.
103
     *
104
     * @return bool Whether to invert the validation logic.
105
     *
106
     * @see $not
107
     */
108
    public function isNot(): bool
109
    {
110
        return $this->not;
111
    }
112
113
    /**
114
     * Get a message used when the input is incorrect.
115
     *
116
     * @return string Error message.
117
     *
118
     * @see $incorrectInputMessage
119
     */
120
    public function getIncorrectInputMessage(): string
121
    {
122
        return $this->incorrectInputMessage;
123
    }
124
125
    /**
126
     * Get a message used when the value does not match regular expression.
127
     *
128
     * @return string Error message.
129
     *
130
     * @see $message
131
     */
132
    public function getMessage(): string
133
    {
134
        return $this->message;
135
    }
136
137
    public function getOptions(): array
138
    {
139
        return [
140
            'pattern' => $this->pattern,
141
            'not' => $this->not,
142
            'incorrectInputMessage' => [
143
                'template' => $this->incorrectInputMessage,
144
                'parameters' => [],
145
            ],
146
            'message' => [
147
                'template' => $this->message,
148
                'parameters' => [],
149
            ],
150
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
151
            'skipOnError' => $this->skipOnError,
152
        ];
153
    }
154
155
    public function getHandler(): string
156
    {
157
        return RegexHandler::class;
158
    }
159
}
160