Passed
Push — master ( 9562b0...4734f7 )
by Sergei
04:27 queued 02:02
created

Url   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 11
eloc 24
c 5
b 0
f 0
dl 0
loc 105
ccs 24
cts 24
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getMessage() 0 3 1
A getValidSchemes() 0 3 1
A getName() 0 3 1
A isEnableIDN() 0 3 1
A getOptions() 0 16 1
A getPattern() 0 3 1
A getIncorrectInputMessage() 0 3 1
A __construct() 0 39 3
A getHandler() 0 3 1
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
 * Validates that the value is a valid HTTP or HTTPS URL.
22
 *
23
 * Note that this rule only checks if the URL scheme and host part are correct.
24
 * It does not check the remaining parts of a URL.
25
 *
26
 * @psalm-import-type WhenType from WhenInterface
27
 */
28
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
29
final class Url implements RuleWithOptionsInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface
30
{
31
    use SkipOnEmptyTrait;
32
    use SkipOnErrorTrait;
33
    use WhenTrait;
34 4
35
    public function __construct(
36
        /**
37
         * @var string the regular expression used to validate the value.
38
         * The pattern may contain a `{schemes}` token that will be replaced
39
         * by a regular expression which represents the {@see $schemes}.
40
         *
41
         * Note that if you want to reuse the pattern in HTML5 input it should have ^ and $, should not have any
42
         * modifiers and should not be case-insensitive.
43
         */
44
        private string $pattern = '/^{schemes}:\/\/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+)(?::\d{1,5})?([?\/#].*$|$)/',
45
        /**
46
         * @var string[] list of URI schemes which should be considered valid. By default, http and https
47
         * are considered to be valid schemes.
48
         */
49
        private array $validSchemes = ['http', 'https'],
50
        /**
51
         * @var bool whether validation process should take into account IDN (internationalized
52
         * domain names). Defaults to false meaning that validation of URLs containing IDN will always
53
         * fail. Note that in order to use IDN validation you have to install and enable `intl` PHP
54
         * extension, otherwise an exception would be thrown.
55
         */
56
        private bool $enableIDN = false,
57
        private string $incorrectInputMessage = 'The value must have a string type.',
58
        private string $message = 'This value is not a valid URL.',
59
0 ignored issues
show
Coding Style introduced by
Blank lines are not allowed in a multi-line function declaration
Loading history...
60
        /**
61
         * @var bool|callable|null
62
         */
63
        private $skipOnEmpty = null,
64
        private bool $skipOnError = false,
65
        /**
66
         * @var WhenType
67
         */
68
        private Closure|null $when = null,
69 4
    ) {
70
        if ($enableIDN && !function_exists('idn_to_ascii')) {
71
            // Tested via separate CI configuration (see ".github/workflows/build.yml").
72
            // @codeCoverageIgnoreStart
73
            throw new RuntimeException('In order to use IDN validation intl extension must be installed and enabled.');
74
            // @codeCoverageIgnoreEnd
75
        }
76
    }
77 1
78
    public function getName(): string
79 1
    {
80
        return 'url';
81
    }
82 44
83
    public function getPattern(): string
84 44
    {
85
        return str_replace('{schemes}', '((?i)' . implode('|', $this->validSchemes) . ')', $this->pattern);
86
    }
87
88
    /**
89
     * @return string[]
90 2
     */
91
    public function getValidSchemes(): array
92 2
    {
93
        return $this->validSchemes;
94
    }
95 40
96
    public function isEnableIDN(): bool
97 40
    {
98
        return $this->enableIDN;
99
    }
100 6
101
    public function getIncorrectInputMessage(): string
102 6
    {
103
        return $this->incorrectInputMessage;
104
    }
105 19
106
    public function getMessage(): string
107 19
    {
108
        return $this->message;
109
    }
110 4
111
    public function getOptions(): array
112
    {
113 4
        return [
114 4
            'pattern' => $this->getPattern(),
115 4
            'validSchemes' => $this->validSchemes,
116
            'enableIDN' => $this->enableIDN,
117 4
            'incorrectInputMessage' => [
118
                'template' => $this->incorrectInputMessage,
119
                'parameters' => [],
120
            ],
121 4
            'message' => [
122
                'template' => $this->message,
123
                'parameters' => [],
124 4
            ],
125 4
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
126
            'skipOnError' => $this->skipOnError,
127
        ];
128
    }
129 48
130
    public function getHandler(): string
131 48
    {
132
        return UrlHandler::class;
133
    }
134
}
135