Test Failed
Pull Request — master (#222)
by Rustam
02:30
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 39
27
    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 39
        if ($enableIDN && !function_exists('idn_to_ascii')) {
55
            throw new RuntimeException('In order to use IDN validation intl extension must be installed and enabled.');
56 39
        }
57 1
    }
58
59
    public function getPattern(): string
60
    {
61 42
        if (str_contains($this->pattern, '{schemes}')) {
62
            return str_replace('{schemes}', '((?i)' . implode('|', $this->validSchemes) . ')', $this->pattern);
63 42
        }
64
65
        return $this->pattern;
66 42
    }
67 40
68 8
    /**
69
     * @return array|string[]
70
     */
71 40
    public function getValidSchemes(): array
72 24
    {
73
        return $this->validSchemes;
74
    }
75
76 18
    /**
77
     * @return bool
78 18
     */
79
    public function isEnableIDN(): bool
80
    {
81 8
        return $this->enableIDN;
82
    }
83 8
84
    /**
85 8
     * @return string
86
     */
87
    public function getMessage(): string
88 8
    {
89
        return $this->message;
90 8
    }
91 4
92
    /**
93
     * @return bool
94 4
     */
95
    public function isSkipOnEmpty(): bool
96 4
    {
97
        return $this->skipOnEmpty;
98
    }
99
100
    /**
101 40
     * @return bool
102
     */
103 40
    public function isSkipOnError(): bool
104 38
    {
105
        return $this->skipOnError;
106
    }
107 2
108
    /**
109
     * @return Closure|null
110 6
     */
111
    public function getWhen(): ?Closure
112 6
    {
113 6
        return $this->when;
114 6
    }
115 6
116 6
    #[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
            'pattern' => $this->getPattern(),
128
            'validSchemes' => $this->validSchemes,
129
            'enableIDN' => $this->enableIDN,
130
            'message' => [
131
                'message' => $this->message,
132
            ],
133
            'skipOnEmpty' => $this->skipOnEmpty,
134
            'skipOnError' => $this->skipOnError,
135
        ];
136
    }
137
}
138