Completed
Push — master ( 1d1c50...dc3dad )
by Jan-Petter
01:55
created

DisAllow::getPath()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
namespace vipnytt\RobotsTxtParser\Directives;
3
4
use vipnytt\RobotsTxtParser\Exceptions;
5
use vipnytt\RobotsTxtParser\ObjectTools;
6
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
7
use vipnytt\RobotsTxtParser\UrlToolbox;
8
9
/**
10
 * Class DisAllow
11
 *
12
 * @package vipnytt\RobotsTxtParser\Directives
13
 */
14
class DisAllow implements DirectiveInterface, RobotsTxtInterface
15
{
16
    use ObjectTools;
17
    use UrlToolbox;
18
19
    /**
20
     * Directive alternatives
21
     */
22
    const DIRECTIVE = [
23
        self::DIRECTIVE_ALLOW,
24
        self::DIRECTIVE_DISALLOW,
25
    ];
26
27
    /**
28
     * Sub directives white list
29
     */
30
    const SUB_DIRECTIVES = [
31
        self::DIRECTIVE_CLEAN_PARAM,
32
        self::DIRECTIVE_HOST,
33
    ];
34
35
    /**
36
     * Directive
37
     */
38
    protected $directive;
39
40
    /**
41
     * Rule array
42
     * @var array
43
     */
44
    protected $array = [];
45
46
    /**
47
     * Sub-directive Clean-param
48
     * @var CleanParam
49
     */
50
    protected $cleanParam;
51
52
    /**
53
     * Sub-directive Host
54
     * @var Host
55
     */
56
    protected $host;
57
58
    /**
59
     * DisAllow constructor
60
     *
61
     * @param string $directive
62
     * @throws Exceptions\ParserException
63
     */
64
    public function __construct($directive)
65
    {
66
        $this->directive = $this->validateDirective($directive, self::DIRECTIVE);
67
        $this->cleanParam = new CleanParam();
68
        $this->host = new Host();
69
    }
70
71
    /**
72
     * Add
73
     *
74
     * @param string $line
75
     * @return bool
76
     */
77
    public function add($line)
78
    {
79
        $pair = $this->generateRulePair($line, self::SUB_DIRECTIVES);
80
        switch ($pair['directive']) {
81
            case self::DIRECTIVE_CLEAN_PARAM:
82
                return $this->cleanParam->add($pair['value']);
83
            case self::DIRECTIVE_HOST:
84
                return $this->host->add($pair['value']);
85
        }
86
        return $this->addPath($line);
87
    }
88
89
    /**
90
     * Add plain path to allow/disallow
91
     *
92
     * @param string $rule
93
     * @return bool
94
     */
95
    protected function addPath($rule)
96
    {
97
        // Return an array of paths
98
        if (isset($this->array['path']) && in_array($rule, $this->array['path'])) {
99
            return false;
100
        }
101
        $this->array['path'][] = $rule;
102
        return true;
103
    }
104
105
    /**
106
     * Check
107
     *
108
     * @param  string $url
109
     * @return bool
110
     */
111
    public function check($url)
112
    {
113
        $path = $this->getPath($url);
114
        return ($path === false) ? false : (
115
            $this->checkPath($path, isset($this->array['path']) ? $this->array['path'] : []) ||
116
            $this->cleanParam->check($path) ||
117
            $this->host->check($url)
118
        );
119
    }
120
121
    /**
122
     * Get path
123
     *
124
     * @param string $url
125
     * @return string
126
     * @throws Exceptions\ClientException
127
     */
128
    protected function getPath($url)
129
    {
130
        $url = $this->urlEncode($url);
131
        if (mb_stripos($url, '/') === 0) {
132
            // URL already is a path
133
            return $url;
134
        }
135
        if (!$this->urlValidate($url)) {
136
            throw new Exceptions\ClientException('Invalid URL');
137
        }
138
        return parse_url($url, PHP_URL_PATH);
139
    }
140
141
    /**
142
     * Export
143
     *
144
     * @return array
145
     */
146
    public function export()
147
    {
148
        $result = $this->array
149
            + $this->cleanParam->export()
150
            + $this->host->export();
151
        return empty($result) ? [] : [$this->directive => $result];
152
    }
153
}
154