1
|
|
|
<?php |
2
|
|
|
namespace vipnytt\RobotsTxtParser\Parser\Directives; |
3
|
|
|
|
4
|
|
|
use vipnytt\RobotsTxtParser\Client\Directives\HostClient; |
5
|
|
|
use vipnytt\RobotsTxtParser\Parser\UrlParser; |
6
|
|
|
use vipnytt\RobotsTxtParser\RobotsTxtInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class HostParser |
10
|
|
|
* |
11
|
|
|
* @package vipnytt\RobotsTxtParser\Parser\Directives |
12
|
|
|
*/ |
13
|
|
|
class HostParser implements ParserInterface, RobotsTxtInterface |
14
|
|
|
{ |
15
|
|
|
use UrlParser; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Base Uri |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $base; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Parent directive |
25
|
|
|
* @var string|null |
26
|
|
|
*/ |
27
|
|
|
private $parent; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Host array |
31
|
|
|
* @var string[] |
32
|
|
|
*/ |
33
|
|
|
private $host = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Host constructor. |
37
|
|
|
* |
38
|
|
|
* @param string $base |
39
|
|
|
* @param string|null $parentDirective |
40
|
|
|
*/ |
41
|
|
|
public function __construct($base, $parentDirective = null) |
42
|
|
|
{ |
43
|
|
|
$this->base = $base; |
44
|
|
|
$this->parent = $parentDirective; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Add |
49
|
|
|
* |
50
|
|
|
* @param string $line |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
|
|
public function add($line) |
54
|
|
|
{ |
55
|
|
|
$host = $this->parse($line); |
56
|
|
|
if ( |
57
|
|
|
$host === false || |
58
|
|
|
$line !== $host || |
59
|
|
|
in_array($host, $this->host) |
60
|
|
|
) { |
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
$this->host[] = $line; |
64
|
|
|
return true; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Client |
69
|
|
|
* |
70
|
|
|
* @param string $line |
71
|
|
|
* @return string|false |
72
|
|
|
*/ |
73
|
|
|
private function parse($line) |
74
|
|
|
{ |
75
|
|
|
if ( |
76
|
|
|
!($parts = $this->getParts($line)) || |
77
|
|
|
!$this->urlValidateHost($parts['host']) || |
78
|
|
|
( |
79
|
|
|
!empty($parts['scheme']) && |
80
|
|
|
!$this->urlValidateScheme($parts['scheme']) |
81
|
|
|
) |
82
|
|
|
) { |
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
return $parts['scheme'] . $parts['host'] . $parts['port']; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get URL parts |
90
|
|
|
* |
91
|
|
|
* @param string $url |
92
|
|
|
* @return string[]|false |
93
|
|
|
*/ |
94
|
|
|
private function getParts($url) |
95
|
|
|
{ |
96
|
|
|
return ($parsed = parse_url($this->urlEncode(mb_strtolower($url)))) === false ? false : [ |
97
|
|
|
'scheme' => isset($parsed['scheme']) ? $parsed['scheme'] . '://' : '', |
98
|
|
|
'host' => isset($parsed['host']) ? $parsed['host'] : $parsed['path'], |
99
|
|
|
'port' => isset($parsed['port']) ? ':' . $parsed['port'] : '', |
100
|
|
|
]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Client |
105
|
|
|
* |
106
|
|
|
* @return HostClient |
107
|
|
|
*/ |
108
|
|
|
public function client() |
109
|
|
|
{ |
110
|
|
|
return new HostClient($this->base, $this->host, $this->parent); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Render |
115
|
|
|
* |
116
|
|
|
* @return string[] |
117
|
|
|
*/ |
118
|
|
View Code Duplication |
public function render() |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
$result = []; |
121
|
|
|
foreach ($this->host as $host) { |
122
|
|
|
$result[] = self::DIRECTIVE_HOST . ':' . $host; |
123
|
|
|
} |
124
|
|
|
sort($result); |
125
|
|
|
return $result; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
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.