Passed
Pull Request — master (#364)
by
unknown
02:47
created

Url::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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\SerializableRuleInterface;
14
use Yiisoft\Validator\SkipOnEmptyInterface;
15
use Yiisoft\Validator\SkipOnErrorInterface;
16
use Yiisoft\Validator\ValidationContext;
17
use Yiisoft\Validator\WhenInterface;
18
19
use function function_exists;
20
21
/**
22
 * Validates that the value is a valid HTTP or HTTPS URL.
23
 *
24
 * Note that this rule only checks if the URL scheme and host part are correct.
25
 * It does not check the remaining parts of a URL.
26
 */
27
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
28
final class Url implements SerializableRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface
29
{
30
    use SkipOnEmptyTrait;
31
    use SkipOnErrorTrait;
32
    use WhenTrait;
33
34 3
    public function __construct(
35
        /**
36
         * @var string the regular expression used to validate the value.
37
         * The pattern may contain a `{schemes}` token that will be replaced
38
         * by a regular expression which represents the {@see $schemes}.
39
         *
40
         * Note that if you want to reuse the pattern in HTML5 input it should have ^ and $, should not have any
41
         * modifiers and should not be case-insensitive.
42
         */
43
        private string $pattern = '/^{schemes}:\/\/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+)(?::\d{1,5})?([?\/#].*$|$)/',
44
        /**
45
         * @var string[] list of URI schemes which should be considered valid. By default, http and https
46
         * are considered to be valid schemes.
47
         */
48
        private array $validSchemes = ['http', 'https'],
49
        /**
50
         * @var bool whether validation process should take into account IDN (internationalized
51
         * domain names). Defaults to false meaning that validation of URLs containing IDN will always
52
         * fail. Note that in order to use IDN validation you have to install and enable `intl` PHP
53
         * extension, otherwise an exception would be thrown.
54
         */
55
        private bool $enableIDN = false,
56
        private string $message = 'This value is not a valid URL.',
57
58
        /**
59
         * @var bool|callable|null
60
         */
61
        private $skipOnEmpty = null,
62
        private bool $skipOnError = false,
63
        /**
64
         * @var Closure(mixed, ValidationContext):bool|null
65
         */
66
        private ?Closure $when = null,
67
    ) {
68 3
        if ($enableIDN && !function_exists('idn_to_ascii')) {
69
            throw new RuntimeException('In order to use IDN validation intl extension must be installed and enabled.');
70
        }
71
    }
72
73 1
    public function getName(): string
74
    {
75 1
        return 'url';
76
    }
77
78 42
    public function getPattern(): string
79
    {
80 42
        return strtr($this->pattern, ['{schemes}' => '((?i)' . implode('|', $this->validSchemes) . ')']);
81
    }
82
83
    /**
84
     * @return array|string[]
85
     */
86
    public function getValidSchemes(): array
87
    {
88
        return $this->validSchemes;
89
    }
90
91 38
    public function isEnableIDN(): bool
92
    {
93 38
        return $this->enableIDN;
94
    }
95
96 18
    public function getMessage(): string
97
    {
98 18
        return $this->message;
99
    }
100
101 4
    public function getOptions(): array
102
    {
103
        return [
104 4
            'pattern' => $this->getPattern(),
105 4
            'validSchemes' => $this->validSchemes,
106 4
            'enableIDN' => $this->enableIDN,
107
            'message' => [
108 4
                'message' => $this->message,
109
            ],
110 4
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
111 4
            'skipOnError' => $this->skipOnError,
112
        ];
113
    }
114
115 41
    public function getHandlerClassName(): string
116
    {
117 41
        return UrlHandler::class;
118
    }
119
}
120