Passed
Pull Request — master (#369)
by
unknown
03:01
created

Url::isEnableIDN()   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
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
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 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 4
    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 $incorrectInputMessage = 'The value must have a string type.',
57
        private string $message = 'This value is not a valid URL.',
58
59
        /**
60
         * @var bool|callable|null
61
         */
62
        private $skipOnEmpty = null,
63
        private bool $skipOnError = false,
64
        /**
65
         * @var Closure(mixed, ValidationContext):bool|null
66
         */
67
        private ?Closure $when = null,
68
    ) {
69 4
        if ($enableIDN && !function_exists('idn_to_ascii')) {
70
            // Tested via separate CI configuration (see ".github/workflows/build.yml").
71
            // @codeCoverageIgnoreStart
72
            throw new RuntimeException('In order to use IDN validation intl extension must be installed and enabled.');
73
            // @codeCoverageIgnoreEnd
74
        }
75
    }
76
77 1
    public function getName(): string
78
    {
79 1
        return 'url';
80
    }
81
82 42
    public function getPattern(): string
83
    {
84 42
        return str_replace('{schemes}', '((?i)' . implode('|', $this->validSchemes) . ')', $this->pattern);
85
    }
86
87
    /**
88
     * @return string[]
89
     */
90 2
    public function getValidSchemes(): array
91
    {
92 2
        return $this->validSchemes;
93
    }
94
95 38
    public function isEnableIDN(): bool
96
    {
97 38
        return $this->enableIDN;
98
    }
99
100 3
    public function getIncorrectInputMessage(): string
101
    {
102 3
        return $this->incorrectInputMessage;
103
    }
104
105 17
    public function getMessage(): string
106
    {
107 17
        return $this->message;
108
    }
109
110 4
    public function getOptions(): array
111
    {
112
        return [
113 4
            'pattern' => $this->getPattern(),
114 4
            'validSchemes' => $this->validSchemes,
115 4
            'enableIDN' => $this->enableIDN,
116
            'incorrectInputMessage' => [
117 4
                'message' => $this->incorrectInputMessage,
118
            ],
119
            'message' => [
120 4
                'message' => $this->message,
121
            ],
122 4
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
123 4
            'skipOnError' => $this->skipOnError,
124
        ];
125
    }
126
127 43
    public function getHandlerClassName(): string
128
    {
129 43
        return UrlHandler::class;
130
    }
131
}
132