Passed
Pull Request — master (#620)
by
unknown
03:19
created

Url::preparePattern()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 1
b 0
f 0
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 RuntimeException;
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\RuleWithOptionsInterface;
15
use Yiisoft\Validator\SkipOnEmptyInterface;
16
use Yiisoft\Validator\SkipOnErrorInterface;
17
use Yiisoft\Validator\WhenInterface;
18
19
use function function_exists;
20
21
/**
22
 * Defines validation options for a value that is a valid HTTP or HTTPS URL.
23
 *
24
 * Note that the handler only checks if the URL scheme and host parts are correct.
25
 * It does not check the remaining parts of a URL.
26
 *
27
 * @psalm-import-type WhenType from WhenInterface
28
 *
29
 * @see UrlHandler
30
 */
31
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
32
final class Url implements RuleWithOptionsInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface
33
{
34 4
    use SkipOnEmptyTrait;
35
    use SkipOnErrorTrait;
36
    use WhenTrait;
37
38
    /**
39
     * @var string The regular expression used to validate the value.
40
     * @psalm-var non-empty-string
41
     */
42
    private string $pattern;
43
44
    /**
45
     * @param string $pattern The regular expression used to validate the value.
46
     * The pattern may contain a `{schemes}` token that will be replaced
47
     * by a regular expression which represents the {@see $schemes}.
48
     *
49
     * Note that if you want to reuse the pattern in HTML5 input, it should have `^` and `$`, should not have any
50
     * modifiers and should not be case-insensitive.
51
     * @param string[] $validSchemes List of URI schemes which should be considered valid. By default, http and https
52
     * are considered to be valid schemes.
53
     * @param bool $enableIdn Whether the validation process must take
54
     * {@link https://en.wikipedia.org/wiki/Internationalized_domain_name IDN (internationalized domain names)}
55
     * into account . Defaults to `false` meaning that validation of URLs containing IDN will always
56
     * fail. Note that in order to use IDN validation you have to install and enable `intl` PHP
57
     * extension, otherwise an exception will be thrown.
58
     * @param string $incorrectInputMessage A message used when the input is incorrect.
59
     *
60
     * You may use the following placeholders in the message:
61
     *
62
     * - `{attribute}`: the translated label of the attribute being validated.
63
     * - `{type}`: the value's type.
64
     * @param string $message @var string A message used when the value is not valid.
65
     *
66
     * You may use the following placeholders in the message:
67
     *
68
     * - `{attribute}`: the translated label of the attribute being validated.
69 4
     * - `{value}`: the value of the attribute being validated.
70
     * @param bool|callable|null $skipOnEmpty Whether to skip this rule if the validated value is empty. See {@see SkipOnEmptyInterface}.
71
     * @param bool $skipOnError Whether to skip this rule if any of the previous rules gave an error. See {@see SkipOnErrorInterface}.
72
     * @param Closure|null $when A callable to define a condition for applying the rule. See {@see WhenInterface}.
73
     *
74
     * @psalm-param WhenType $when
75
     *
76
     * @throws RuntimeException If intl extension is not enabled and {@see $enableIdn} is true.
77 1
     */
78
    public function __construct(
79 1
        string $pattern = '/^{schemes}:\/\/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+)(?::\d{1,5})?([?\/#].*$|$)/',
80
        private array $validSchemes = ['http', 'https'],
81
        private bool $enableIdn = false,
82 44
        private string $incorrectInputMessage = 'The value must be a string.',
83
        private string $message = 'This value is not a valid URL.',
84 44
        private mixed $skipOnEmpty = null,
85
        private bool $skipOnError = false,
86
        private Closure|null $when = null,
87
    ) {
88
        $pattern = $this->preparePattern($pattern);
89
        if ($pattern === '') {
90 2
            throw new InvalidArgumentException('Pattern can\'t be empty.');
91
        }
92 2
93
        /**
94
         * @psalm-var non-empty-string $pattern
95 40
         */
96
        $this->pattern = $pattern;
97 40
98
        if ($enableIdn && !function_exists('idn_to_ascii')) {
99
            // Tested via separate CI configuration (see ".github/workflows/build.yml").
100 6
            // @codeCoverageIgnoreStart
101
            throw new RuntimeException('In order to use IDN validation intl extension must be installed and enabled.');
102 6
            // @codeCoverageIgnoreEnd
103
        }
104
    }
105 19
106
    public function getName(): string
107 19
    {
108
        return 'url';
109
    }
110 4
111
    /**
112
     * Get ready to use regular expression pattern applied for URL validation.
113 4
     *
114 4
     * @return string Regular expression pattern applied for URL validation.
115 4
     * @psalm-return non-empty-string
116
     *
117 4
     * @see $pattern
118
     */
119
    public function getPattern(): string
120
    {
121 4
        return $this->pattern;
122
    }
123
124 4
    /**
125 4
     * Get valid URI schemas.
126
     *
127
     * @return string[] List of URI schemes which should be considered valid. By default, http and https
128
     * are considered to be valid schemes.
129 48
     *
130
     * @see $validSchemes
131 48
     */
132
    public function getValidSchemes(): array
133
    {
134
        return $this->validSchemes;
135
    }
136
137
    /**
138
     * Whether the validation process must take
139
     * {@link https://en.wikipedia.org/wiki/Internationalized_domain_name IDN (internationalized domain names)}
140
     * into account. `false` means that validation of URLs containing IDN will always
141
     * fail. Note that in order to use IDN validation you have to install and enable `intl` PHP
142
     * extension, otherwise an exception will be thrown.
143
     *
144
     * @return bool Whether to enable IDN validation.
145
     *
146
     * @see $enableIdn
147
     */
148
    public function isIdnEnabled(): bool
149
    {
150
        return $this->enableIdn;
151
    }
152
153
    /**
154
     * Get a message used when the input is incorrect.
155
     *
156
     * @return string Error message.
157
     *
158
     * @see $incorrectInputMessage
159
     */
160
    public function getIncorrectInputMessage(): string
161
    {
162
        return $this->incorrectInputMessage;
163
    }
164
165
    /**
166
     * Get a message used when the value is not valid.
167
     *
168
     * @return string Error message.
169
     *
170
     * @see $message
171
     */
172
    public function getMessage(): string
173
    {
174
        return $this->message;
175
    }
176
177
    public function getOptions(): array
178
    {
179
        return [
180
            'pattern' => $this->getPattern(),
181
            'validSchemes' => $this->validSchemes,
182
            'enableIdn' => $this->enableIdn,
183
            'incorrectInputMessage' => [
184
                'template' => $this->incorrectInputMessage,
185
                'parameters' => [],
186
            ],
187
            'message' => [
188
                'template' => $this->message,
189
                'parameters' => [],
190
            ],
191
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
192
            'skipOnError' => $this->skipOnError,
193
        ];
194
    }
195
196
    public function getHandler(): string
197
    {
198
        return UrlHandler::class;
199
    }
200
201
    private function preparePattern(string $pattern): string
202
    {
203
        return str_replace('{schemes}', '((?i)' . implode('|', $this->validSchemes) . ')', $pattern);
204
    }
205
}
206