Passed
Push — master ( 89f940...819311 )
by Alexander
02:27
created

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