Completed
Push — master ( 902e66...0cbd7a )
by Jan-Petter
02:44
created

InlineHostClient::__construct()   A

Complexity

Conditions 1
Paths 1

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 1
eloc 2
nc 1
nop 3
1
<?php
2
namespace vipnytt\RobotsTxtParser\Client\Directives;
3
4
use vipnytt\RobotsTxtParser\Parser\UriParser;
5
6
/**
7
 * Class InlineHostClient
8
 *
9
 * @see https://github.com/VIPnytt/RobotsTxtParser/blob/master/docs/methods/InlineHostClient.md for documentation
10
 * @package vipnytt\RobotsTxtParser\Client\Directives
11
 */
12
class InlineHostClient extends HostClientCore
13
{
14
    /**
15
     * InlineHostClient constructor.
16
     *
17
     * @param string $base
18
     * @param string $effective
19
     * @param string[] $host
20
     */
21
    public function __construct($base, $effective, $host)
22
    {
23
        parent::__construct($base, $effective, $host);
24
    }
25
26
    /**
27
     * Export
28
     *
29
     * @return string[]
30
     */
31
    public function export()
32
    {
33
        return $this->host;
34
    }
35
36
    /**
37
     * Is listed?
38
     *
39
     * @param string $uri
40
     * @return bool
41
     */
42
    public function isListed($uri)
43
    {
44
        $uriParser = new UriParser($uri);
45
        $uri = $uriParser->encode();
46
        $parts = [
47
            'scheme' => parse_url($uri, PHP_URL_SCHEME),
48
            'host' => parse_url($uri, PHP_URL_HOST),
49
        ];
50
        $parts['port'] = is_int($port = parse_url($uri, PHP_URL_PORT)) ? $port : getservbyname($parts['scheme'], 'tcp');
51
        $cases = [
52
            $parts['host'],
53
            $parts['host'] . ':' . $parts['port'],
54
            $parts['scheme'] . '://' . $parts['host'],
55
            $parts['scheme'] . '://' . $parts['host'] . ':' . $parts['port']
56
        ];
57
        foreach ($this->host as $host) {
58
            if (in_array($host, $cases)) {
59
                return true;
60
            }
61
        }
62
        return false;
63
    }
64
}
65