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; |
13
|
|
|
|
14
|
|
|
use Bisarca\RobotsTxt\Directive\DirectiveInterface; |
15
|
|
|
use Bisarca\RobotsTxt\Directive\PathDirectiveInterface; |
16
|
|
|
use Generator; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Set of directives. |
20
|
|
|
*/ |
21
|
|
|
class Ruleset extends AbstractSet |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Class constructor with optional initialization data. |
25
|
|
|
* |
26
|
|
|
* @param DirectiveInterface[] $directives |
27
|
|
|
*/ |
28
|
|
|
public function __construct(DirectiveInterface ...$directives) |
29
|
|
|
{ |
30
|
|
|
$this->data = $directives; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Adds a directive. |
35
|
|
|
* |
36
|
|
|
* @param DirectiveInterface $directive |
37
|
|
|
*/ |
38
|
|
|
public function add(DirectiveInterface $directive) |
39
|
|
|
{ |
40
|
|
|
$this->data[] = $directive; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Checks if a directive is contained. |
45
|
|
|
* |
46
|
|
|
* @param DirectiveInterface $directive |
47
|
|
|
* |
48
|
|
|
* @return bool |
49
|
|
|
*/ |
50
|
|
|
public function has(DirectiveInterface $directive): bool |
51
|
|
|
{ |
52
|
|
|
return false !== array_search($directive, $this->data, true); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Remove an element. |
57
|
|
|
* |
58
|
|
|
* @param DirectiveInterface $directive |
59
|
|
|
* |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function remove(DirectiveInterface $directive): bool |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$key = array_search($directive, $this->data, true); |
65
|
|
|
|
66
|
|
|
if (false !== $key) { |
67
|
|
|
unset($this->data[$key]); |
68
|
|
|
$this->data = array_values($this->data); |
69
|
|
|
|
70
|
|
|
return true; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Checks if a user agent is allowed. |
78
|
|
|
* |
79
|
|
|
* @param string $path |
80
|
|
|
* |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
public function isUserAgentAllowed( |
84
|
|
|
string $path = PathDirectiveInterface::DEFAULT_PATH |
85
|
|
|
): bool { |
86
|
|
|
$path = trim($path) ?: PathDirectiveInterface::DEFAULT_PATH; |
87
|
|
|
|
88
|
|
|
foreach ($this->data as $directive) { |
89
|
|
|
if ( |
90
|
|
|
$directive instanceof PathDirectiveInterface && |
91
|
|
|
( |
92
|
|
|
$directive->getValue() == $path || |
93
|
|
|
preg_match('#'.$directive->getRegex().'#', $path) |
94
|
|
|
) |
95
|
|
|
) { |
96
|
|
|
return $directive instanceof Directive\Allow; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return true; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Gets ruleset comments for the developer. |
105
|
|
|
* |
106
|
|
|
* @return Generator |
107
|
|
|
*/ |
108
|
|
|
public function getComments(): Generator |
109
|
|
|
{ |
110
|
|
|
yield from $this->getDirectives(Directive\Comment::class); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Gets directives of a certain type. |
115
|
|
|
* |
116
|
|
|
* @param string|null $class |
117
|
|
|
* |
118
|
|
|
* @return DirectiveInterface[] |
119
|
|
|
*/ |
120
|
|
|
public function getDirectives(string $class = null): array |
121
|
|
|
{ |
122
|
|
|
return array_filter($this->data, function ($element) use ($class) { |
123
|
|
|
return null === $class || $element instanceof $class; |
124
|
|
|
}); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.