Completed
Push — master ( 894553...1b6743 )
by Jan-Petter
02:20
created

Host::export()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
namespace vipnytt\RobotsTxtParser\Parser\Directives;
3
4
use vipnytt\RobotsTxtParser\Parser\RobotsTxtInterface;
5
use vipnytt\RobotsTxtParser\Parser\UrlParser;
6
7
/**
8
 * Class Host
9
 *
10
 * @package vipnytt\RobotsTxtParser\Parser\Directives
11
 */
12
class Host implements DirectiveInterface, RobotsTxtInterface
13
{
14
    use UrlParser;
15
16
    /**
17
     * Directive
18
     */
19
    const DIRECTIVE = self::DIRECTIVE_HOST;
20
21
    /**
22
     * Host array
23
     * @var string[]
24
     */
25
    protected $array = [];
26
27
    /**
28
     * Host constructor.
29
     */
30
    public function __construct()
31
    {
32
    }
33
34
    /**
35
     * Add
36
     *
37
     * @param string $line
38
     * @return bool
39
     */
40
    public function add($line)
41
    {
42
        $host = $this->parse($line);
43
        if (
44
            $host === false ||
45
            $line !== $host ||
46
            in_array($host, $this->array)
47
        ) {
48
            return false;
49
        }
50
        $this->array[] = $line;
51
        return true;
52
    }
53
54
    /**
55
     * Parse
56
     *
57
     * @param string $line
58
     * @return string|false
59
     */
60
    protected function parse($line)
61
    {
62
        if (($parsed = parse_url(($line = $this->urlEncode(mb_strtolower($line))))) === false) {
63
            return false;
64
        }
65
        $line = isset($parsed['host']) ? $parsed['host'] : $parsed['path'];
66
        if (
67
            !$this->urlValidateHost($line) ||
68
            (
69
                isset($parsed['scheme']) &&
70
                !$this->urlValidateScheme($parsed['scheme'])
71
            )
72
        ) {
73
            return false;
74
        }
75
        $scheme = isset($parsed['scheme']) ? $parsed['scheme'] . '://' : '';
76
        $port = isset($parsed['port']) ? ':' . $parsed['port'] : '';
77
        return $scheme . $line . $port;
78
    }
79
80
    /**
81
     * Check
82
     *
83
     * @param string $url
84
     * @return bool
85
     */
86
    public function check($url)
87
    {
88
        if (empty($this->array)) {
89
            return false;
90
        }
91
        $url = mb_strtolower($this->urlEncode($url));
92
        $parts = [
93
            'scheme' => parse_url($url, PHP_URL_SCHEME),
94
            'host' => parse_url($url, PHP_URL_HOST),
95
        ];
96
        $parts['port'] = is_int($port = parse_url($url, PHP_URL_PORT)) ? $port : getservbyname($parts['scheme'], 'tcp');
97
        $cases = [
98
            $parts['host'],
99
            $parts['host'] . ':' . $parts['port'],
100
            $parts['scheme'] . '://' . $parts['host'],
101
            $parts['scheme'] . '://' . $parts['host'] . ':' . $parts['port']
102
        ];
103
        if (in_array($this->array[0], $cases)) {
104
            return true;
105
        }
106
        return false;
107
    }
108
109
    /**
110
     * Export rules
111
     *
112
     * @return string[][]
113
     */
114
    public function export()
115
    {
116
        return empty($this->array) ? [] : [self::DIRECTIVE => $this->array];
117
    }
118
119
    /**
120
     * Render
121
     *
122
     * @return string[]
123
     */
124
    public function render()
125
    {
126
        $result = [];
127
        foreach ($this->array as $value) {
128
            $result[] = self::DIRECTIVE . ':' . $value;
129
        }
130
        return $result;
131
    }
132
}
133