Completed
Branch 2.0-dev (2c252e)
by Jan-Petter
02:43
created

HostClient::isMainHost()   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\Client\Directives;
3
use vipnytt\RobotsTxtParser\Parser\UrlParser;
4
5
/**
6
 * Class HostClient
7
 *
8
 * @package vipnytt\RobotsTxtParser\Client\Directives
9
 */
10
class HostClient
11
{
12
    use UrlParser;
13
14
    /**
15
     * Base Uri
16
     * @var string
17
     */
18
    private $base;
19
20
    /**
21
     * Host
22
     * @var array
23
     */
24
    private $host;
25
26
    /**
27
     * HostClient constructor.
28
     *
29
     * @param string $base
30
     * @param array $host
31
     */
32
    public function __construct($base, array $host)
33
    {
34
        $this->base = $base;
35
        $this->host = $host;
36
    }
37
38
    /**
39
     * Check
40
     *
41
     * @param string $url
42
     * @return bool
43
     */
44
    public function check($url)
45
    {
46
        $url = mb_strtolower($this->urlEncode($url));
47
        $parts = [
48
            'scheme' => parse_url($url, PHP_URL_SCHEME),
49
            'host' => parse_url($url, PHP_URL_HOST),
50
        ];
51
        $parts['port'] = is_int($port = parse_url($url, PHP_URL_PORT)) ? $port : getservbyname($parts['scheme'], 'tcp');
52
        $cases = [
53
            $parts['host'],
54
            $parts['host'] . ':' . $parts['port'],
55
            $parts['scheme'] . '://' . $parts['host'],
56
            $parts['scheme'] . '://' . $parts['host'] . ':' . $parts['port']
57
        ];
58
        if (in_array($this->get(), $cases)) {
59
            return true;
60
        }
61
        return false;
62
    }
63
64
    /**
65
     * Get
66
     *
67
     * @return string|null
68
     */
69
    public function get()
70
    {
71
        return isset($this->host[0]) ? $this->host[0] : null;
72
    }
73
74
    /**
75
     * Preferred host
76
     *
77
     * @return bool
78
     */
79
    public function isMainHost()
80
    {
81
        return empty($this->host) ? true : mb_stripos($this->urlBase($this->urlEncode($this->base)), $this->get()) !== false;
82
    }
83
84
    /**
85
     * Export
86
     *
87
     * @return array
88
     */
89
    public function export()
90
    {
91
        return $this->host;
92
    }
93
}
94