1
|
|
|
<?php |
2
|
|
|
namespace vipnytt\RobotsTxtParser\Directives; |
3
|
|
|
|
4
|
|
|
use vipnytt\RobotsTxtParser\Exceptions\ParserException; |
5
|
|
|
use vipnytt\RobotsTxtParser\ObjectTools; |
6
|
|
|
use vipnytt\RobotsTxtParser\RobotsTxtInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class DisAllow |
10
|
|
|
* |
11
|
|
|
* @package vipnytt\RobotsTxtParser\Directives |
12
|
|
|
*/ |
13
|
|
|
class DisAllow implements DirectiveInterface, RobotsTxtInterface |
14
|
|
|
{ |
15
|
|
|
use ObjectTools; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Directive alternatives |
19
|
|
|
*/ |
20
|
|
|
const DIRECTIVE = [ |
21
|
|
|
self::DIRECTIVE_ALLOW, |
22
|
|
|
self::DIRECTIVE_DISALLOW, |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Sub directives white list |
27
|
|
|
*/ |
28
|
|
|
const SUB_DIRECTIVES = [ |
29
|
|
|
self::DIRECTIVE_CLEAN_PARAM, |
30
|
|
|
self::DIRECTIVE_HOST, |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Directive |
35
|
|
|
*/ |
36
|
|
|
protected $directive; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Rule array |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $array = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Sub-directive Clean-param |
46
|
|
|
* @var CleanParam |
47
|
|
|
*/ |
48
|
|
|
protected $cleanParam; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Sub-directive Host |
52
|
|
|
* @var Host |
53
|
|
|
*/ |
54
|
|
|
protected $host; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* DisAllow constructor |
58
|
|
|
* |
59
|
|
|
* @param string $directive |
60
|
|
|
* @throws ParserException |
61
|
|
|
*/ |
62
|
|
|
public function __construct($directive) |
63
|
|
|
{ |
64
|
|
View Code Duplication |
if (!in_array($directive, self::DIRECTIVE, true)) { |
|
|
|
|
65
|
|
|
throw new ParserException('Directive not allowed here, has to be `' . self::DIRECTIVE_ALLOW . '` or `' . self::DIRECTIVE_DISALLOW . '`'); |
66
|
|
|
} |
67
|
|
|
$this->directive = mb_strtolower($directive); |
68
|
|
|
$this->cleanParam = new CleanParam(); |
69
|
|
|
$this->host = new Host(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Add |
74
|
|
|
* |
75
|
|
|
* @param string $line |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
public function add($line) |
79
|
|
|
{ |
80
|
|
|
$pair = $this->generateRulePair($line, self::SUB_DIRECTIVES); |
81
|
|
|
switch ($pair['directive']) { |
82
|
|
|
case self::DIRECTIVE_CLEAN_PARAM: |
83
|
|
|
return $this->cleanParam->add($pair['value']); |
84
|
|
|
case self::DIRECTIVE_HOST: |
85
|
|
|
return $this->host->add($pair['value']); |
86
|
|
|
} |
87
|
|
|
return $this->addPath($line); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Add plain path to allow/disallow |
92
|
|
|
* |
93
|
|
|
* @param string $rule |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
|
|
protected function addPath($rule) |
97
|
|
|
{ |
98
|
|
|
// Return an array of paths |
99
|
|
|
if (isset($this->array['path']) && in_array($rule, $this->array['path'])) { |
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
$this->array['path'][] = $rule; |
103
|
|
|
return true; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Check |
108
|
|
|
* |
109
|
|
|
* @param string $url |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
|
|
public function check($url) |
113
|
|
|
{ |
114
|
|
|
$path = $this->getPath($url); |
115
|
|
|
return ( |
116
|
|
|
$this->checkPath($path, isset($this->array['path']) ? $this->array['path'] : []) || |
|
|
|
|
117
|
|
|
$this->cleanParam->check($path) || |
|
|
|
|
118
|
|
|
$this->host->check($url) |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Export |
124
|
|
|
* |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
public function export() |
128
|
|
|
{ |
129
|
|
|
$result = $this->array |
130
|
|
|
+ $this->cleanParam->export() |
131
|
|
|
+ $this->host->export(); |
132
|
|
|
return empty($result) ? [] : [$this->directive => $result]; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.