Passed
Pull Request — master (#41)
by Alexander
12:06 queued 10:41
created

Url   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 86.67%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 30
dl 0
loc 84
ccs 26
cts 30
cp 0.8667
rs 10
c 2
b 0
f 0
wmc 14

7 Methods

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