Completed
Push — master ( fda467...a7ec5f )
by Jan-Petter
02:10
created

UrlToolbox   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

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

4 Methods

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