Completed
Push — master ( a7ec5f...7812c9 )
by Jan-Petter
02:03
created

Host::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
namespace vipnytt\RobotsTxtParser\Directives;
3
4
use vipnytt\RobotsTxtParser\UrlToolbox;
5
6
/**
7
 * Class Host
8
 *
9
 * @package vipnytt\RobotsTxtParser\Directives
10
 */
11
class Host implements DirectiveInterface
12
{
13
    use UrlToolbox;
14
15
    /**
16
     * Directive
17
     */
18
    const DIRECTIVE = 'Host';
19
20
    protected $array = [];
21
    protected $parent;
22
23
24
    public function __construct($array = [], $parent = null)
25
    {
26
        $this->array = $array;
27
        $this->parent = $parent;
28
    }
29
30
    /**
31
     * Add
32
     *
33
     * @param string $line
34
     * @return bool
35
     */
36
    public function add($line)
37
    {
38
        if (($parsed = parse_url(($line = $this->urlEncode(mb_strtolower($line))))) === false) {
39
            return false;
40
        }
41
        $line = isset($parsed['host']) ? $parsed['host'] : $parsed['path'];
42
        if (
43
            !$this->urlValidateHost($line) ||
44
            (
45
                isset($parsed['scheme']) &&
46
                !$this->urlValidateScheme($parsed['scheme'])
47
            )
48
        ) {
49
            return false;
50
        }
51
        $scheme = isset($parsed['scheme']) ? $parsed['scheme'] . '://' : '';
52
        $port = isset($parsed['port']) ? ':' . $parsed['port'] : '';
53
54
        $host = $scheme . $line . $port;
55
        if (in_array($host, $this->array)) {
56
            return false;
57
        }
58
        if ($this->parent !== null) {
59
            $this->array[] = $host;
60
            return true;
61
        }
62
        if (!empty($this->array)) {
63
            return false;
64
        }
65
        $this->array = [$host];
66
        return true;
67
    }
68
69
    /**
70
     * Check Host rule
71
     *
72
     * @param string $url
73
     * @return bool
74
     */
75
    public function check($url)
76
    {
77
        if (empty($this->array)) {
78
            return false;
79
        }
80
        $url = mb_strtolower($this->urlEncode($url));
81
        $parts = [
82
            'scheme' => parse_url($url, PHP_URL_SCHEME),
83
            'host' => parse_url($url, PHP_URL_HOST),
84
        ];
85
        $parts['port'] = is_int($port = parse_url($url, PHP_URL_PORT)) ? $port : getservbyname($parts['scheme'], 'tcp');
86
        $cases = [
87
            $parts['host'],
88
            $parts['host'] . ':' . $parts['port'],
89
            $parts['scheme'] . '://' . $parts['host'],
90
            $parts['scheme'] . '://' . $parts['host'] . ':' . $parts['port']
91
        ];
92
        if (in_array($this->array[0], $cases)) {
93
            return true;
94
        }
95
        return false;
96
    }
97
98
    public function export()
99
    {
100
        return empty($this->array) ? [] : [self::DIRECTIVE => $this->array];
101
    }
102
}
103