1
|
|
|
<?php |
2
|
|
|
namespace vipnytt\RobotsTxtParser; |
3
|
|
|
|
4
|
|
|
use vipnytt\UserAgentParser; |
5
|
|
|
|
6
|
|
|
class Parser extends Core |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* HTTP status code parser |
10
|
|
|
* @var StatusCodeParser |
11
|
|
|
*/ |
12
|
|
|
protected $statusCodeParser; |
13
|
|
|
|
14
|
|
|
protected $origin; |
15
|
|
|
protected $statusCode; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Parser constructor. |
19
|
|
|
* |
20
|
|
|
* @param string $robotsTxtURL |
21
|
|
|
* @param int|null $statusCode |
22
|
|
|
* @param string|null $content |
23
|
|
|
* @param string $encoding |
24
|
|
|
* @param int $byteLimit |
25
|
|
|
*/ |
26
|
|
|
public function __construct($robotsTxtURL, $statusCode, $content, $encoding = self::ENCODING, $byteLimit = self::BYTE_LIMIT) |
27
|
|
|
{ |
28
|
|
|
parent::__construct($content, $encoding, $byteLimit); |
29
|
|
|
$this->origin = $robotsTxtURL; |
30
|
|
|
$this->statusCode = $statusCode; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get sitemaps |
35
|
|
|
* |
36
|
|
|
* @return array |
37
|
|
|
*/ |
38
|
|
|
public function getSitemaps() |
39
|
|
|
{ |
40
|
|
|
return $this->sitemap->export(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get host |
45
|
|
|
* |
46
|
|
|
* @return string|null |
47
|
|
|
*/ |
48
|
|
|
public function getHost() |
49
|
|
|
{ |
50
|
|
|
return $this->host->export(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get Clean-param |
55
|
|
|
* |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
|
|
public function getCleanParam() |
59
|
|
|
{ |
60
|
|
|
return $this->cleanParam->export(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function userAgent($string = self::USER_AGENT) |
64
|
|
|
{ |
65
|
|
|
$uaParser = new UserAgentParser($string); |
66
|
|
|
$userAgent = $uaParser->match($this->userAgent->userAgents, self::USER_AGENT); |
67
|
|
|
return new UserAgentClient([ |
68
|
|
|
self::DIRECTIVE_DISALLOW => $this->userAgent->{self::DIRECTIVE_DISALLOW}[$userAgent], |
69
|
|
|
self::DIRECTIVE_ALLOW => $this->userAgent->{self::DIRECTIVE_ALLOW}[$userAgent], |
70
|
|
|
], $userAgent, $this->origin, $this->statusCode); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.