Passed
Pull Request — master (#300)
by Alexander
06:13 queued 03:06
created

Url   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 86.36%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 10
eloc 20
c 5
b 0
f 0
dl 0
loc 96
ccs 19
cts 22
cp 0.8636
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getHandlerClassName() 0 3 1
A getMessage() 0 3 1
A __construct() 0 36 3
A getValidSchemes() 0 3 1
A isEnableIDN() 0 3 1
A getOptions() 0 11 1
A getPattern() 0 7 2
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\BeforeValidationInterface;
11
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
12
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
13
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
14
use Yiisoft\Validator\SerializableRuleInterface;
15
use Yiisoft\Validator\SkipOnEmptyInterface;
16
use Yiisoft\Validator\ValidationContext;
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
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
27
final class Url implements SerializableRuleInterface, BeforeValidationInterface, SkipOnEmptyInterface
28
{
29
    use BeforeValidationTrait;
30
    use RuleNameTrait;
31
    use SkipOnEmptyTrait;
32
33 2
    public function __construct(
34
        /**
35
         * @var string the regular expression used to validate the value.
36
         * The pattern may contain a `{schemes}` token that will be replaced
37
         * by a regular expression which represents the {@see $schemes}.
38
         *
39
         * Note that if you want to reuse the pattern in HTML5 input it should have ^ and $, should not have any
40
         * modifiers and should not be case-insensitive.
41
         */
42
        private string $pattern = '/^{schemes}:\/\/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+)(?::\d{1,5})?([?\/#].*$|$)/',
43
        /**
44
         * @var array list of URI schemes which should be considered valid. By default, http and https
45
         * are considered to be valid schemes.
46
         */
47
        private array $validSchemes = ['http', 'https'],
48
        /**
49
         * @var bool whether validation process should take into account IDN (internationalized
50
         * domain names). Defaults to false meaning that validation of URLs containing IDN will always
51
         * fail. Note that in order to use IDN validation you have to install and enable `intl` PHP
52
         * extension, otherwise an exception would be thrown.
53
         */
54
        private bool $enableIDN = false,
55
        private string $message = 'This value is not a valid URL.',
56
57
        /**
58
         * @var bool|callable|null
59
         */
60
        private $skipOnEmpty = null,
61
        private bool $skipOnError = false,
62
        /**
63
         * @var Closure(mixed, ValidationContext):bool|null
64
         */
65
        private ?Closure $when = null,
66
    ) {
67 2
        if ($enableIDN && !function_exists('idn_to_ascii')) {
68
            throw new RuntimeException('In order to use IDN validation intl extension must be installed and enabled.');
69
        }
70
    }
71
72 41
    public function getPattern(): string
73
    {
74 41
        if (str_contains($this->pattern, '{schemes}')) {
75 38
            return str_replace('{schemes}', '((?i)' . implode('|', $this->validSchemes) . ')', $this->pattern);
76
        }
77
78 3
        return $this->pattern;
79
    }
80
81
    /**
82
     * @return array|string[]
83
     */
84
    public function getValidSchemes(): array
85
    {
86
        return $this->validSchemes;
87
    }
88
89
    /**
90
     * @return bool
91
     */
92 37
    public function isEnableIDN(): bool
93
    {
94 37
        return $this->enableIDN;
95
    }
96
97
    /**
98
     * @return string
99
     */
100 16
    public function getMessage(): string
101
    {
102 16
        return $this->message;
103
    }
104
105 4
    public function getOptions(): array
106
    {
107
        return [
108 4
            'pattern' => $this->getPattern(),
109 4
            'validSchemes' => $this->validSchemes,
110 4
            'enableIDN' => $this->enableIDN,
111
            'message' => [
112 4
                'message' => $this->message,
113
            ],
114 4
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
115 4
            'skipOnError' => $this->skipOnError,
116
        ];
117
    }
118
119 1
    public function getHandlerClassName(): string
120
    {
121 1
        return UrlHandler::class;
122
    }
123
}
124