|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the bisarca/robots-txt package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Emanuele Minotto <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Bisarca\RobotsTxt\Directive; |
|
13
|
|
|
|
|
14
|
|
|
use Bisarca\RobotsTxt\Exception\InvalidDirectiveException; |
|
15
|
|
|
use Pdp\Parser; |
|
16
|
|
|
use Pdp\PublicSuffixListManager; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* "Host" directive element. |
|
20
|
|
|
*/ |
|
21
|
|
|
class Host implements NonGroupInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* PHP Domain Parser (if available). |
|
25
|
|
|
* |
|
26
|
|
|
* @var Parser |
|
27
|
|
|
*/ |
|
28
|
|
|
private static $domainParser; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Directive value. |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
private $value = ''; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritdoc} |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(string $raw) |
|
41
|
|
|
{ |
|
42
|
|
|
if ( |
|
43
|
|
|
class_exists(Parser::class) && |
|
44
|
|
|
class_exists(PublicSuffixListManager::class) && |
|
45
|
|
|
null === self::$domainParser |
|
46
|
|
|
) { |
|
47
|
|
|
$pslManager = new PublicSuffixListManager(); |
|
48
|
|
|
self::setDomainParser(new Parser($pslManager->getList())); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if (!preg_match('/^host:\s*([^# ]+).*/i', $raw, $matches)) { |
|
52
|
|
|
throw InvalidDirectiveException::create($raw); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$host = trim($matches[1]); |
|
56
|
|
|
|
|
57
|
|
|
$this->validateHost($host, $raw); |
|
58
|
|
|
$this->value = $host; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritdoc} |
|
63
|
|
|
*/ |
|
64
|
|
|
public static function getField(): string |
|
65
|
|
|
{ |
|
66
|
|
|
return 'host'; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getValue(): string |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->value; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* {@inheritdoc} |
|
79
|
|
|
*/ |
|
80
|
|
|
public function __toString(): string |
|
81
|
|
|
{ |
|
82
|
|
|
return sprintf('Host: %s', $this->value); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Validates host (if PHP Domain Parser is available). |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $host |
|
89
|
|
|
* @param string $raw |
|
90
|
|
|
*/ |
|
91
|
|
|
private function validateHost(string $host, string $raw) |
|
92
|
|
|
{ |
|
93
|
|
|
if ( |
|
94
|
|
|
null !== self::$domainParser && |
|
95
|
|
|
!self::$domainParser->isSuffixValid($host) |
|
96
|
|
|
) { |
|
97
|
|
|
throw InvalidDirectiveException::create($raw); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Sets the domain parser. |
|
103
|
|
|
* |
|
104
|
|
|
* @param Parser $domainParser |
|
105
|
|
|
*/ |
|
106
|
|
|
public static function setDomainParser(Parser $domainParser) |
|
107
|
|
|
{ |
|
108
|
|
|
self::$domainParser = $domainParser; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|