|
1
|
|
|
<?php |
|
2
|
|
|
namespace vipnytt\RobotsTxtParser\Directives; |
|
3
|
|
|
|
|
4
|
|
|
use vipnytt\RobotsTxtParser\ObjectTools; |
|
5
|
|
|
use vipnytt\RobotsTxtParser\RobotsTxtInterface; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class UserAgent |
|
9
|
|
|
* |
|
10
|
|
|
* @package vipnytt\RobotsTxtParser\Directives |
|
11
|
|
|
*/ |
|
12
|
|
|
class UserAgent implements DirectiveInterface, RobotsTxtInterface |
|
13
|
|
|
{ |
|
14
|
|
|
use ObjectTools; |
|
15
|
|
|
|
|
16
|
|
|
const SUB_DIRECTIVES = [ |
|
17
|
|
|
self::DIRECTIVE_ALLOW, |
|
18
|
|
|
self::DIRECTIVE_CACHE_DELAY, |
|
19
|
|
|
self::DIRECTIVE_CRAWL_DELAY, |
|
20
|
|
|
self::DIRECTIVE_DISALLOW, |
|
21
|
|
|
]; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Directive |
|
25
|
|
|
*/ |
|
26
|
|
|
const DIRECTIVE = 'User-agent'; |
|
27
|
|
|
|
|
28
|
|
|
protected $parent; |
|
29
|
|
|
protected $array = []; |
|
30
|
|
|
|
|
31
|
|
|
protected $allow; |
|
32
|
|
|
protected $cacheDelay; |
|
33
|
|
|
protected $crawlDelay; |
|
34
|
|
|
protected $disallow; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct($array, $parent = null) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->array = $array; |
|
|
|
|
|
|
39
|
|
|
$this->allow = new Allow([], self::DIRECTIVE); |
|
|
|
|
|
|
40
|
|
|
$this->cacheDelay = new CacheDelay([], self::DIRECTIVE); |
|
|
|
|
|
|
41
|
|
|
$this->crawlDelay = new CrawlDelay([], self::DIRECTIVE); |
|
|
|
|
|
|
42
|
|
|
$this->disallow = new Disallow([], self::DIRECTIVE); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Add |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $line |
|
49
|
|
|
* @return bool |
|
50
|
|
|
*/ |
|
51
|
|
|
public function add($line) |
|
52
|
|
|
{ |
|
53
|
|
|
$pair = $this->generateRulePair($line, self::SUB_DIRECTIVES); |
|
54
|
|
|
switch ($pair['directive']) { |
|
55
|
|
|
case self::DIRECTIVE_ALLOW: |
|
56
|
|
|
return $this->allow->add($pair['value']); |
|
57
|
|
|
case self::DIRECTIVE_CACHE_DELAY: |
|
58
|
|
|
return $this->cacheDelay->add($pair['value']); |
|
59
|
|
|
case self::DIRECTIVE_CRAWL_DELAY: |
|
60
|
|
|
return $this->crawlDelay->add($pair['value']); |
|
61
|
|
|
case self::DIRECTIVE_DISALLOW: |
|
62
|
|
|
return $this->disallow->add($pair['value']); |
|
63
|
|
|
} |
|
64
|
|
|
return false; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Check rules |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $url - URL to check |
|
71
|
|
|
* @param string $type - directive to check |
|
72
|
|
|
* @return bool |
|
73
|
|
|
*/ |
|
74
|
|
|
public function check($url, $type) |
|
75
|
|
|
{ |
|
76
|
|
|
$result = ($type === self::DIRECTIVE_ALLOW); |
|
77
|
|
|
foreach ([self::DIRECTIVE_DISALLOW, self::DIRECTIVE_ALLOW] as $directive) { |
|
78
|
|
|
if ($this->$directive->check($url)) { |
|
79
|
|
|
$result = ($type === $directive); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
return $result; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function export() |
|
86
|
|
|
{ |
|
87
|
|
|
$result = $this->array |
|
88
|
|
|
+ $this->allow->export() |
|
89
|
|
|
+ $this->cacheDelay->export() |
|
90
|
|
|
+ $this->crawlDelay->export() |
|
91
|
|
|
+ $this->disallow->export(); |
|
92
|
|
|
return empty($result) ? [] : [self::DIRECTIVE => $result]; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..