Passed
Pull Request — master (#222)
by Alexander
02:58
created

Url   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 27
c 4
b 0
f 0
dl 0
loc 94
ccs 31
cts 31
cp 1
rs 10
wmc 15
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Attribute;
8
use Closure;
9
use JetBrains\PhpStorm\ArrayShape;
10
use RuntimeException;
11
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_TRAIT, expecting T_STRING or '{' on line 11 at column 27
Loading history...
12
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait;
13
use Yiisoft\Validator\ParametrizedRuleInterface;
14
15
/**
16
 * Validates that the value is a valid HTTP or HTTPS URL.
17
 *
18
 * Note that this rule only checks if the URL scheme and host part are correct.
19
 * It does not check the remaining parts of a URL.
20
 */
21
#[Attribute(Attribute::TARGET_PROPERTY)]
22
final class Url implements ParametrizedRuleInterface
23
{
24
    use HandlerClassNameTrait;
25
    use RuleNameTrait;
26
27 7
    public function __construct(
28
        /**
29
         * @var string the regular expression used to validate the value.
30
         * The pattern may contain a `{schemes}` token that will be replaced
31
         * by a regular expression which represents the {@see $schemes}.
32
         *
33
         * Note that if you want to reuse the pattern in HTML5 input it should have ^ and $, should not have any
34
         * modifiers and should not be case-insensitive.
35
         */
36
        private string $pattern = '/^{schemes}:\/\/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+)(?::\d{1,5})?([?\/#].*$|$)/',
37
        /**
38
         * @var array list of URI schemes which should be considered valid. By default, http and https
39
         * are considered to be valid schemes.
40
         */
41
        private array $validSchemes = ['http', 'https'],
42
        /**
43
         * @var bool whether validation process should take into account IDN (internationalized
44
         * domain names). Defaults to false meaning that validation of URLs containing IDN will always
45
         * fail. Note that in order to use IDN validation you have to install and enable `intl` PHP
46
         * extension, otherwise an exception would be thrown.
47
         */
48
        private bool $enableIDN = false,
49
        private string $message = 'This value is not a valid URL.',
50
        private bool $skipOnEmpty = false,
51
        private bool $skipOnError = false,
52
        private ?Closure $when = null,
53
    ) {
54 7
        if ($enableIDN && !function_exists('idn_to_ascii')) {
55 1
            throw new RuntimeException('In order to use IDN validation intl extension must be installed and enabled.');
56
        }
57
    }
58
59 46
    public function getPattern(): string
60
    {
61 46
        if (str_contains($this->pattern, '{schemes}')) {
62 43
            return str_replace('{schemes}', '((?i)' . implode('|', $this->validSchemes) . ')', $this->pattern);
63
        }
64
65 3
        return $this->pattern;
66
    }
67
68
    /**
69
     * @return array|string[]
70
     */
71
    public function getValidSchemes(): array
72
    {
73
        return $this->validSchemes;
74
    }
75
76
    /**
77
     * @return bool
78
     */
79 41
    public function isEnableIDN(): bool
80
    {
81 41
        return $this->enableIDN;
82
    }
83
84
    /**
85
     * @return string
86
     */
87 19
    public function getMessage(): string
88
    {
89 19
        return $this->message;
90
    }
91
92
    /**
93
     * @return bool
94
     */
95
    public function isSkipOnEmpty(): bool
96
    {
97
        return $this->skipOnEmpty;
98
    }
99
100
    /**
101
     * @return bool
102
     */
103
    public function isSkipOnError(): bool
104
    {
105
        return $this->skipOnError;
106
    }
107
108
    /**
109
     * @return Closure|null
110
     */
111
    public function getWhen(): ?Closure
112
    {
113
        return $this->when;
114
    }
115
116 5
    #[ArrayShape([
117
        'pattern' => 'string',
118
        'validSchemes' => 'array|string[]',
119
        'enableIDN' => 'bool',
120
        'message' => 'string[]',
121
        'skipOnEmpty' => 'bool',
122
        'skipOnError' => 'bool',
123
    ])]
124
    public function getOptions(): array
125
    {
126
        return [
127 5
            'pattern' => $this->getPattern(),
128 5
            'validSchemes' => $this->validSchemes,
129 5
            'enableIDN' => $this->enableIDN,
130
            'message' => [
131 5
                'message' => $this->message,
132
            ],
133 5
            'skipOnEmpty' => $this->skipOnEmpty,
134 5
            'skipOnError' => $this->skipOnError,
135
        ];
136
    }
137
}
138