Passed
Pull Request — master (#474)
by Alexander
02:46
created

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