|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* vipnytt/RobotsTxtParser |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/VIPnytt/RobotsTxtParser |
|
6
|
|
|
* @license https://github.com/VIPnytt/RobotsTxtParser/blob/master/LICENSE The MIT License (MIT) |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace vipnytt\RobotsTxtParser\Parser\Directives; |
|
10
|
|
|
|
|
11
|
|
|
use vipnytt\RobotsTxtParser\Client\Directives\DelayClient; |
|
12
|
|
|
use vipnytt\RobotsTxtParser\Handler\RenderHandler; |
|
13
|
|
|
use vipnytt\RobotsTxtParser\RobotsTxtInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class DelayParser |
|
17
|
|
|
* |
|
18
|
|
|
* @package vipnytt\RobotsTxtParser\Parser\Directives |
|
19
|
|
|
*/ |
|
20
|
|
|
class DelayParser implements ParserInterface, RobotsTxtInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Directive |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private $directive; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Base uri |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
private $base; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Delay |
|
36
|
|
|
* @var float|int |
|
37
|
|
|
*/ |
|
38
|
|
|
private $delay; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* DelayParser constructor. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $base |
|
44
|
|
|
* @param string $directive |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct($base, $directive) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->base = $base; |
|
49
|
|
|
$this->directive = $directive; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Add |
|
54
|
|
|
* |
|
55
|
|
|
* @param float|int|string $line |
|
56
|
|
|
* @return bool |
|
57
|
|
|
*/ |
|
58
|
|
|
public function add($line) |
|
59
|
|
|
{ |
|
60
|
|
|
if (!is_numeric($line) || |
|
61
|
|
|
( |
|
62
|
|
|
isset($this->delay) && |
|
63
|
|
|
$this->delay > 0 |
|
64
|
|
|
) |
|
65
|
|
|
) { |
|
66
|
|
|
return false; |
|
67
|
|
|
} |
|
68
|
|
|
// PHP hack to convert numeric string to float or int |
|
69
|
|
|
// http://stackoverflow.com/questions/16606364/php-cast-string-to-either-int-or-float |
|
70
|
|
|
$this->delay = $line + 0; |
|
71
|
|
|
return true; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Client |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $userAgent |
|
78
|
|
|
* @param float|int $fallbackValue |
|
79
|
|
|
* @return DelayClient |
|
80
|
|
|
*/ |
|
81
|
|
|
public function client($userAgent = self::USER_AGENT, $fallbackValue = 0) |
|
82
|
|
|
{ |
|
83
|
|
|
return new DelayClient($this->base, $userAgent, $this->delay, $fallbackValue); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Render |
|
88
|
|
|
* |
|
89
|
|
|
* @param RenderHandler $handler |
|
90
|
|
|
* @return bool |
|
91
|
|
|
*/ |
|
92
|
|
|
public function render(RenderHandler $handler) |
|
93
|
|
|
{ |
|
94
|
|
|
if (!empty($this->delay)) { |
|
95
|
|
|
$handler->add($this->directive, $this->delay); |
|
96
|
|
|
} |
|
97
|
|
|
return true; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|