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

DisAllowParser::check()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 8.8571
cc 5
eloc 6
nc 6
nop 1
1
<?php
2
namespace vipnytt\RobotsTxtParser\Parser\Directives;
3
4
use vipnytt\RobotsTxtParser\Client\Directives\DisAllowClient;
5
use vipnytt\RobotsTxtParser\Exceptions;
6
use vipnytt\RobotsTxtParser\Parser\UrlParser;
7
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
8
9
/**
10
 * Class DisAllowParser
11
 *
12
 * @package vipnytt\RobotsTxtParser\Parser\Directives
13
 */
14
class DisAllowParser implements ParserInterface, RobotsTxtInterface
15
{
16
    use DirectiveParserCommons;
17
    use UrlParser;
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
     * @var string
38
     */
39
    private $directive;
40
41
    /**
42
     * Base Uri
43
     * @var string
44
     */
45
    private $base;
46
47
    /**
48
     * Rule array
49
     * @var array
50
     */
51
    private $array = [];
52
53
    /**
54
     * Sub-directive Clean-param
55
     * @var CleanParamParser
56
     */
57
    private $cleanParam;
58
59
    /**
60
     * Sub-directive Host
61
     * @var HostParser
62
     */
63
    private $host;
64
65
    /**
66
     * DisAllow constructor
67
     *
68
     * @param string $base
69
     * @param string $directive
70
     */
71
    public function __construct($base, $directive)
72
    {
73
        $this->base = $base;
74
        $this->directive = $this->validateDirective($directive, self::DIRECTIVE);
75
        $this->cleanParam = new CleanParamParser();
76
        $this->host = new HostParser($this->base);
77
    }
78
79
    /**
80
     * Add
81
     *
82
     * @param string $line
83
     * @return bool
84
     */
85
    public function add($line)
86
    {
87
        $pair = $this->generateRulePair($line, self::SUB_DIRECTIVES);
88
        switch ($pair['directive']) {
89
            case self::DIRECTIVE_CLEAN_PARAM:
90
                return $this->cleanParam->add($pair['value']);
91
            case self::DIRECTIVE_HOST:
92
                return $this->host->add($pair['value']);
93
        }
94
        return $this->addPath($line);
95
    }
96
97
    /**
98
     * Add plain path to allow/disallow
99
     *
100
     * @param string $rule
101
     * @return bool
102
     */
103
    private function addPath($rule)
104
    {
105
        if (isset($this->array['path']) && in_array($rule, $this->array['path'])) {
106
            return false;
107
        }
108
        $this->array['path'][] = $rule;
109
        return true;
110
    }
111
112
    /**
113
     * Rule array
114
     *
115
     * @return array
116
     */
117
    public function getRules()
118
    {
119
        $result = array_merge(
120
            $this->array,
121
            $this->cleanParam->getRules(),
122
            $this->host->getRules()
123
        );
124
        return empty($result) ? [] : [$this->directive => $result];
125
    }
126
127
    /**
128
     * Render
129
     *
130
     * @return string[]
131
     */
132
    public function render()
133
    {
134
        $result = [];
135
        $render = array_merge(
136
            $this->array,
137
            $this->cleanParam->render(),
138
            $this->host->render()
139
        );
140
        foreach ($render as $value) {
141
            if (is_array($value)) {
142
                foreach ($value as $path) {
143
                    $result[] = $this->directive . ':' . $path;
144
                }
145
                continue;
146
            }
147
            $result[] = $this->directive . ':' . $value;
148
        }
149
        return $result;
150
    }
151
152
    /**
153
     * Client
154
     *
155
     * @return DisAllowClient
156
     */
157
    public function client()
158
    {
159
        return new DisAllowClient($this->array, $this->cleanParam->client(), $this->host->client());
160
    }
161
}
162