Passed
Push — master ( c53668...8969dd )
by Jean-Christophe
08:33
created

UrlValidator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 20
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Ubiquity\contents\validation\validators\strings;
4
5
/**
6
 * Validates an URL
7
 * Usage @validator("url","constraints"=>["relative"=>false,"protocols"=>["http","https"]])
8
 * Inspired from Bernhard Schussek Symfony UrlValidator
9
 * @author jcheron <[email protected]>
10
 * @version 1.0.0
11
 */
12
class UrlValidator extends RegexValidator {
13
	
14
	protected $protocols=["http","https","ftp"];
15
	protected $relative=false;
16
	
17
	public function __construct(){
18
		$this->message="{value} is not a valid url";
19
		$this->ref='~^
20
            (%s)://                                 # protocol
21
            (([\.\pL\pN-]+:)?([\.\pL\pN-]+)@)?      # basic auth
22
            (
23
                ([\pL\pN\pS\-\.])+(\.?([\pL\pN]|xn\-\-[\pL\pN-]+)+\.?) # a domain name
24
                    |                                                 # or
25
                \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}                    # an IP address
26
                    |                                                 # or
27
                \[
28
                    (?:(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){6})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:::(?:(?:(?:[0-9a-f]{1,4})):){5})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-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})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,5}(?:(?:[0-9a-f]{1,4})))?::)(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,6}(?:(?:[0-9a-f]{1,4})))?::))))
29
                \]  # an IPv6 address
30
            )
31
            (:[0-9]+)?                              # a port (optional)
32
            (?:/ (?:[\pL\pN\-._\~!$&\'()*+,;=:@]|%%[0-9A-Fa-f]{2})* )*      # a path
33
            (?:\? (?:[\pL\pN\-._\~!$&\'()*+,;=:@/?]|%%[0-9A-Fa-f]{2})* )?   # a query (optional)
34
            (?:\# (?:[\pL\pN\-._\~!$&\'()*+,;=:@/?]|%%[0-9A-Fa-f]{2})* )?   # a fragment (optional)
35
        $~ixu';
36
		$this->match=true;
37
	}
38
	/**
39
	 * {@inheritDoc}
40
	 * @see \Ubiquity\contents\validation\validators\Validator::setParams()
41
	 */
42
	protected function setParams(array $params) {
43
		parent::setParams($params);
44
		$ref = $this->relative ? str_replace('(%s)://', '(?:(%s)://)?', $this->ref) : $this->ref;
45
		$this->ref = sprintf($ref, implode('|', $this->protocols));
46
	}
47
	/**
48
	 *
49
	 * {@inheritdoc}
50
	 * @see \Ubiquity\contents\validation\validators\Validator::asUI()
51
	 */
52
	public function asUI(): array {
53
		return ['inputType'=>'url']+\array_merge_recursive(parent::asUI () , ['rules' => [ ['type'=>'url','prompt'=>$this->_getMessage()] ]]);
54
	}
55
	
56
}
57
58