Passed
Pull Request — master (#81)
by Def
04:12 queued 02:54
created

Url::getOptions()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 1
nop 0
dl 0
loc 8
ccs 7
cts 7
cp 1
crap 4
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 Yiisoft\Validator\HasValidationErrorMessage;
8
use Yiisoft\Validator\Rule;
9
use Yiisoft\Validator\Result;
10
use Yiisoft\Validator\DataSetInterface;
11
12
/**
13
 * UrlValidator validates that the attribute value is a valid http or https URL.
14
 *
15
 * Note that this validator only checks if the URL scheme and host part are correct.
16
 * It does not check the remaining parts of a URL.
17
 */
18
class Url extends Rule
19
{
20
    use HasValidationErrorMessage;
21
22
    /**
23
     * @var string the regular expression used to validateValue the attribute value.
24
     * The pattern may contain a `{schemes}` token that will be replaced
25
     * by a regular expression which represents the [[validSchemes]].
26
     */
27
    private string $pattern = '/^{schemes}:\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(?::\d{1,5})?(?:$|[?\/#])/i';
28
    /**
29
     * @var bool set to true if not default pattern
30
     */
31
    private bool $patternIsChanged = false;
32
    /**
33
     * @var array list of URI schemes which should be considered valid. By default, http and https
34
     * are considered to be valid schemes.
35
     */
36
    private array $validSchemes = ['http', 'https'];
37
    /**
38
     * @var bool set to true if not default schemes
39
     */
40
    private bool $schemesIsChanged = false;
41
    /**
42
     * @var bool whether validation process should take into account IDN (internationalized
43
     * domain names). Defaults to false meaning that validation of URLs containing IDN will always
44
     * fail. Note that in order to use IDN validation you have to install and enable `intl` PHP
45
     * extension, otherwise an exception would be thrown.
46
     */
47
    private bool $enableIDN = false;
48
49
    private string $message = 'This value is not a valid URL.';
50
51 10
    public function __construct()
52
    {
53 10
        if ($this->enableIDN && !function_exists('idn_to_ascii')) {
54
            throw new \RuntimeException('In order to use IDN validation intl extension must be installed and enabled.');
55
        }
56
    }
57
58 9
    protected function validateValue($value, DataSetInterface $dataSet = null): Result
59
    {
60 9
        $result = new Result();
61
62
        // make sure the length is limited to avoid DOS attacks
63 9
        if (is_string($value) && strlen($value) < 2000) {
64 8
            if ($this->enableIDN) {
65 5
                $value = $this->convertIdn($value);
66
            }
67
68 8
            if (preg_match($this->getPattern(), $value)) {
69 6
                return $result;
70
            }
71
        }
72
73 5
        $result->addError($this->translateMessage($this->message));
74
75 5
        return $result;
76
    }
77
78 5
    private function idnToAscii($idn)
79
    {
80 5
        return idn_to_ascii($idn, 0, INTL_IDNA_VARIANT_UTS46);
81
    }
82
83 5
    private function convertIdn($value): string
84
    {
85 5
        if (!str_contains($value, '://')) {
86 3
            return $this->idnToAscii($value);
87
        }
88
89 2
        return preg_replace_callback(
90 2
            '/:\/\/([^\/]+)/',
91 2
            fn ($matches) => '://' . $this->idnToAscii($matches[1]),
92
            $value
93
        );
94
    }
95
96 8
    private function getPattern(): string
97
    {
98 8
        if (str_contains($this->pattern, '{schemes}')) {
99 6
            return str_replace('{schemes}', '(' . implode('|', $this->validSchemes) . ')', $this->pattern);
100
        }
101
102 2
        return $this->pattern;
103
    }
104
105 2
    public function pattern(string $pattern): self
106
    {
107 2
        $new = clone $this;
108 2
        $new->pattern = $pattern;
109 2
        $new->patternIsChanged = true;
110 2
        return $new;
111
    }
112
113 5
    public function enableIDN(): self
114
    {
115 5
        $new = clone $this;
116 5
        $new->enableIDN = true;
117 5
        return $new;
118
    }
119
120 1
    public function schemes(array $schemes): self
121
    {
122 1
        $new = clone $this;
123 1
        $new->validSchemes = $schemes;
124 1
        $new->schemesIsChanged = true;
125 1
        return $new;
126
    }
127
128
    /**
129
     * @inheritDoc
130
     * @return string
131
     */
132 1
    public function getName(): string
133
    {
134 1
        return 'url';
135
    }
136
137
    /**
138
     * @inheritDoc
139
     * @return array
140
     */
141 4
    public function getOptions(): array
142
    {
143 4
        return array_merge(
144 4
            ['message' => $this->translateMessage($this->message)],
145 4
            $this->enableIDN ? ['enableIDN' => true] : [],
146 4
            $this->schemesIsChanged ? ['validSchemes' => $this->validSchemes] : [],
147 4
            $this->patternIsChanged ? ['pattern' => $this->pattern] : [],
148 4
            parent::getOptions()
149
        );
150
    }
151
}
152