1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bee4\RobotsTxt; |
4
|
|
|
|
5
|
|
|
use Bee4\RobotsTxt\Exception\DuplicateRuleException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Rules |
9
|
|
|
* Represent a collection of Rules |
10
|
|
|
* |
11
|
|
|
* @copyright Bee4 2015 |
12
|
|
|
* @author Stephane HULARD <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class Rules implements \Countable |
15
|
|
|
{ |
16
|
|
|
const DEFAULT_UA = '*'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The collection of rules |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $collection = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Default rule used if robots.txt is empty |
26
|
|
|
* @var Rule |
27
|
|
|
*/ |
28
|
|
|
private $defaultRule; |
29
|
|
|
|
30
|
|
|
public function __construct() |
31
|
|
|
{ |
32
|
1 |
|
$this->defaultRule = new Rule(self::DEFAULT_UA); |
33
|
1 |
|
$this->add($this->defaultRule); |
34
|
1 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Add a new rule to the collection |
38
|
|
|
* @param string $userAgent |
|
|
|
|
39
|
|
|
* @param Rule $rule |
40
|
|
|
* @return Rules |
41
|
|
|
*/ |
42
|
|
|
public function add(Rule $rule) |
43
|
|
|
{ |
44
|
1 |
|
$userAgent = $this->handleUa($rule->getUserAgent()); |
45
|
1 |
|
if (isset($this->collection[$userAgent]) && |
46
|
1 |
|
$this->collection[$userAgent] !== $this->defaultRule ) { |
47
|
1 |
|
throw (new DuplicateRuleException( |
48
|
|
|
'You can\'t add 2 rules for the same UserAgent' |
49
|
1 |
|
)) |
50
|
1 |
|
->setRule($rule); |
51
|
|
|
} |
52
|
1 |
|
$this->collection[$userAgent] = $rule; |
53
|
|
|
|
54
|
1 |
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Check if the URL match for the given UA or not |
59
|
|
|
* @param string $userAgent |
60
|
|
|
* @param string $url |
61
|
|
|
* @return boolean |
62
|
|
|
*/ |
63
|
|
|
public function match($userAgent, $url) |
64
|
|
|
{ |
65
|
1 |
|
return $this->get($userAgent)->match($url); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Retrieve rules for a given UA |
70
|
|
|
* @param string $userAgent |
71
|
|
|
* @return null|Rule |
72
|
|
|
*/ |
73
|
|
|
public function get($userAgent) |
74
|
|
|
{ |
75
|
1 |
|
$item = null; |
76
|
1 |
|
$iterator = new \ArrayIterator($this->collection); |
77
|
1 |
|
iterator_apply( |
78
|
1 |
|
$iterator, |
79
|
1 |
|
function (\ArrayIterator $iterator, $userAgent) use (&$item) { |
80
|
1 |
|
if ($iterator->key() != Rules::DEFAULT_UA && |
81
|
1 |
|
preg_match($iterator->key(), $userAgent) === 1 ) { |
82
|
1 |
|
$item = $iterator->current(); |
83
|
1 |
|
return false; |
84
|
|
|
} |
85
|
1 |
|
return true; |
86
|
1 |
|
}, |
87
|
1 |
|
[$iterator, $userAgent] |
88
|
1 |
|
); |
89
|
|
|
|
90
|
1 |
|
return $item!==null? |
91
|
1 |
|
$item: |
92
|
1 |
|
$this->collection[self::DEFAULT_UA]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Update the UA to make a valid regexp |
97
|
|
|
* @param string $userAgent |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
private function handleUa($userAgent) |
101
|
|
|
{ |
102
|
1 |
|
if ($userAgent == self::DEFAULT_UA) { |
103
|
1 |
|
return $userAgent; |
104
|
|
|
} |
105
|
1 |
|
return '/^'.preg_quote($userAgent).'.*/i'; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Return the number of rules |
110
|
|
|
* @return integer |
111
|
|
|
*/ |
112
|
|
|
public function count() |
113
|
|
|
{ |
114
|
1 |
|
return count($this->collection); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.