Passed
Push — master ( 9d8a75...f0a095 )
by Alexander
01:39
created

Url::enableIDN()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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