1
|
|
|
<?php |
2
|
|
|
namespace vipnytt\RobotsTxtParser\Directives; |
3
|
|
|
|
4
|
|
|
use vipnytt\RobotsTxtParser\ObjectTools; |
5
|
|
|
use vipnytt\RobotsTxtParser\RobotsTxtInterface; |
6
|
|
|
use vipnytt\RobotsTxtParser\UrlToolbox; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Allow |
10
|
|
|
* |
11
|
|
|
* @package vipnytt\RobotsTxtParser\Directives |
12
|
|
|
*/ |
13
|
|
View Code Duplication |
class Allow implements DirectiveInterface, RobotsTxtInterface |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
use UrlToolbox; |
16
|
|
|
use ObjectTools; |
17
|
|
|
|
18
|
|
|
const SUB_DIRECTIVES = [ |
19
|
|
|
self::DIRECTIVE_CLEAN_PARAM, |
20
|
|
|
self::DIRECTIVE_HOST, |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Directive |
25
|
|
|
*/ |
26
|
|
|
const DIRECTIVE = 'Allow'; |
27
|
|
|
|
28
|
|
|
protected $array = []; |
29
|
|
|
protected $parent; |
30
|
|
|
|
31
|
|
|
protected $cleanParam; |
32
|
|
|
protected $host; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
public function __construct($array, $parent = null) |
36
|
|
|
{ |
37
|
|
|
$this->array = $array; |
|
|
|
|
38
|
|
|
$this->cleanParam = new CleanParam([], self::DIRECTIVE); |
39
|
|
|
$this->host = new Host([], self::DIRECTIVE); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Add |
44
|
|
|
* |
45
|
|
|
* @param string $line |
46
|
|
|
* @return bool |
47
|
|
|
*/ |
48
|
|
|
public function add($line) |
49
|
|
|
{ |
50
|
|
|
$pair = $this->generateRulePair($line, self::SUB_DIRECTIVES); |
51
|
|
|
switch ($pair['directive']) { |
52
|
|
|
case self::DIRECTIVE_CLEAN_PARAM: |
53
|
|
|
return $this->cleanParam->add($pair['value']); |
54
|
|
|
case self::DIRECTIVE_HOST: |
55
|
|
|
return $this->host->add($pair['value']); |
56
|
|
|
} |
57
|
|
|
return $this->addPath($line); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function addPath($rule) |
61
|
|
|
{ |
62
|
|
|
// Return an array of paths |
63
|
|
|
if (isset($this->array['path']) && in_array($rule, $this->array['path'])) { |
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
$this->array['path'][] = $rule; |
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Check |
72
|
|
|
* |
73
|
|
|
* @param string $url |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
public function check($url) |
77
|
|
|
{ |
78
|
|
|
$path = $this->getPath($url); |
79
|
|
|
return ( |
80
|
|
|
$this->checkPath($path, isset($this->array['path']) ? $this->array['path'] : []) || |
81
|
|
|
$this->cleanParam->check($path) || |
82
|
|
|
$this->host->check($url) |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function export() |
87
|
|
|
{ |
88
|
|
|
$result = $this->array |
89
|
|
|
+ $this->cleanParam->export() |
90
|
|
|
+ $this->host->export(); |
91
|
|
|
return empty($result) ? [] : [self::DIRECTIVE => $result]; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
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.