/
UrlValidator.php
70 lines (59 loc) · 1.83 KB
/
UrlValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\validators;
use Craft;
use craft\helpers\App;
use yii\validators\UrlValidator as YiiUrlValidator;
/**
* Class UrlValidator.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0.0
*/
class UrlValidator extends YiiUrlValidator
{
/**
* @since 3.6.0
*/
public const URL_PATTERN = '^(?:(?:{schemes}:)?\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)?|\/)[^\s]*$';
/**
* @var bool Whether the value can begin with an alias
* @deprecated
*/
public bool $allowAlias = false;
/**
* @inheritdoc
*/
public function __construct(array $config = [])
{
// Override the $pattern regex so that a TLD is not required, and the protocol may be relative.
if (!isset($config['pattern'])) {
$config['pattern'] = '/' . self::URL_PATTERN . '/i';
}
// Enable support for validating international domain names if the server supports IDNA ASCII strings
if (!isset($config['enableIDN']) && App::supportsIdn()) {
$config['enableIDN'] = true;
}
parent::__construct($config);
}
/**
* @inheritdoc
*/
public function validateValue($value): ?array
{
if ($this->allowAlias && strncmp($value, '@', 1) === 0) {
$value = Craft::getAlias($value);
// Prevent validateAttribute() from prepending a default scheme if the alias is missing one
$this->defaultScheme = null;
}
// Add support for protocol-relative URLs
if (isset($this->defaultScheme) && str_starts_with($value, '/')) {
$this->defaultScheme = null;
}
return parent::validateValue($value);
}
}