Passed
Pull Request — master (#99)
by Def
02:39
created

Url::getRawOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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