URLParser   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 11
c 3
b 1
f 0
lcom 1
cbo 0
dl 0
loc 91
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isValid() 0 10 4
B encode() 0 25 1
A isHostValid() 0 8 4
A isSchemeValid() 0 7 1
1
<?php
2
/**
3
 * URL parser
4
 *
5
 * @author Jan-Petter Gundersen ([email protected])
6
 */
7
8
namespace vipnytt\CleanParamFilter;
9
10
final class URLParser
11
{
12
    private $url;
13
14
    /**
15
     * Constructor
16
     *
17
     * @param string $url
18
     */
19
    public function __construct($url)
20
    {
21
        $this->url = $url;
22
    }
23
24
    /**
25
     * Validate URL
26
     *
27
     * @return bool
28
     */
29
    public function isValid()
30
    {
31
        $this->encode();
32
        return (
33
            filter_var($this->url, FILTER_VALIDATE_URL)
34
            && parse_url($this->url) !== false
35
            && $this->isHostValid()
36
            && $this->isSchemeValid()
37
        );
38
    }
39
40
    /**
41
     * URL encoder according to RFC 3986
42
     * Returns a string containing the encoded URL with disallowed characters converted to their percentage encodings.
43
     * @link http://publicmind.in/blog/url-encoding/
44
     *
45
     * @return string string
46
     */
47
    public function encode()
48
    {
49
        $reserved = [
50
            ":" => '!%3A!ui',
51
            "/" => '!%2F!ui',
52
            "?" => '!%3F!ui',
53
            "#" => '!%23!ui',
54
            "[" => '!%5B!ui',
55
            "]" => '!%5D!ui',
56
            "@" => '!%40!ui',
57
            "!" => '!%21!ui',
58
            "$" => '!%24!ui',
59
            "&" => '!%26!ui',
60
            "'" => '!%27!ui',
61
            "(" => '!%28!ui',
62
            ")" => '!%29!ui',
63
            "*" => '!%2A!ui',
64
            "+" => '!%2B!ui',
65
            "," => '!%2C!ui',
66
            ";" => '!%3B!ui',
67
            "=" => '!%3D!ui',
68
            "%" => '!%25!ui'
69
        ];
70
        return $this->url = preg_replace(array_values($reserved), array_keys($reserved), rawurlencode($this->url));
71
    }
72
73
    /**
74
     * Validate host name
75
     *
76
     * @link http://stackoverflow.com/questions/1755144/how-to-validate-domain-name-in-php
77
     * @return bool
78
     */
79
    public function  isHostValid()
80
    {
81
        $host = parse_url($this->url, PHP_URL_HOST);
82
        return (preg_match("/^([a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*$/i", $host) //valid chars check
83
            && preg_match("/^.{1,253}$/", $host) //overall length check
84
            && preg_match("/^[^\.]{1,63}(\.[^\.]{1,63})*$/", $host) //length of each label
85
            && !filter_var($host, FILTER_VALIDATE_IP)); //is not an IP address
86
    }
87
88
    /**
89
     * Validate URL scheme
90
     *
91
     * @return bool
92
     */
93
    public function isSchemeValid()
94
    {
95
        return in_array(parse_url($this->url, PHP_URL_SCHEME), [
96
            'http', 'https',
97
            'ftp', 'sftp'
98
        ]);
99
    }
100
}
101