1
|
|
|
<?php |
2
|
|
|
namespace vipnytt\RobotsTxtParser\Client\Directives; |
3
|
|
|
|
4
|
|
|
use vipnytt\RobotsTxtParser\Exceptions\ClientException; |
5
|
|
|
use vipnytt\RobotsTxtParser\Parser\UriParser; |
6
|
|
|
use vipnytt\RobotsTxtParser\RobotsTxtInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class AllowClient |
10
|
|
|
* |
11
|
|
|
* @see https://github.com/VIPnytt/RobotsTxtParser/blob/master/docs/methods/AllowClient.md for documentation |
12
|
|
|
* @package vipnytt\RobotsTxtParser\Client\Directives |
13
|
|
|
*/ |
14
|
|
|
class AllowClient implements ClientInterface, RobotsTxtInterface |
15
|
|
|
{ |
16
|
|
|
use DirectiveClientCommons; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Paths |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $paths; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Host |
26
|
|
|
* @var InlineHostClient |
27
|
|
|
*/ |
28
|
|
|
private $host; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Clean-param |
32
|
|
|
* @var InlineCleanParamClient |
33
|
|
|
*/ |
34
|
|
|
private $cleanParam; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* AllowClient constructor. |
38
|
|
|
* |
39
|
|
|
* @param array $paths |
40
|
|
|
* @param InlineHostClient $host |
41
|
|
|
* @param InlineCleanParamClient $cleanParam |
42
|
|
|
*/ |
43
|
|
|
public function __construct(array $paths, InlineHostClient $host, InlineCleanParamClient $cleanParam) |
44
|
|
|
{ |
45
|
|
|
$this->host = $host; |
46
|
|
|
$this->paths = $paths; |
47
|
|
|
$this->cleanParam = $cleanParam; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Inline Clean-param directive |
52
|
|
|
* |
53
|
|
|
* @return InlineCleanParamClient |
54
|
|
|
*/ |
55
|
|
|
public function cleanParam() |
56
|
|
|
{ |
57
|
|
|
return $this->cleanParam; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Inline Host directive |
62
|
|
|
* |
63
|
|
|
* @return InlineHostClient |
64
|
|
|
*/ |
65
|
|
|
public function host() |
66
|
|
|
{ |
67
|
|
|
return $this->host; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Check |
72
|
|
|
* |
73
|
|
|
* @param string $uri |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
public function isListed($uri) |
77
|
|
|
{ |
78
|
|
|
$path = $this->getPath($uri); |
79
|
|
|
return ( |
80
|
|
|
$this->checkPaths($path, $this->paths) || |
81
|
|
|
$this->host->isListed($uri) || |
82
|
|
|
!empty($this->cleanParam->detect($path)) |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get path and query |
88
|
|
|
* |
89
|
|
|
* @param string $uri |
90
|
|
|
* @return string |
91
|
|
|
* @throws ClientException |
92
|
|
|
*/ |
93
|
|
|
private function getPath($uri) |
94
|
|
|
{ |
95
|
|
|
$uriParser = new UriParser($uri); |
96
|
|
|
// Encode |
97
|
|
|
$uri = explode('#', $uriParser->encode(), 2)[0]; |
98
|
|
|
if (mb_strpos($uri, '/') === 0) { |
99
|
|
|
// URI is already an path |
100
|
|
|
return $uri; |
101
|
|
|
} |
102
|
|
|
if (!$uriParser->validate()) { |
103
|
|
|
throw new ClientException('Invalid URI'); |
104
|
|
|
} |
105
|
|
|
$path = (($path = parse_url($uri, PHP_URL_PATH)) === null) ? '/' : $path; |
106
|
|
|
$query = (($query = parse_url($uri, PHP_URL_QUERY)) === null) ? '' : '?' . $query; |
107
|
|
|
return $path . $query; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Export |
112
|
|
|
* |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
|
|
public function export() |
116
|
|
|
{ |
117
|
|
|
return [ |
118
|
|
|
'host' => $this->host->export(), |
119
|
|
|
'path' => $this->paths, |
120
|
|
|
'clean-param' => $this->cleanParam->export(), |
121
|
|
|
]; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|