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
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* "Disallow" directive element. |
18
|
|
|
*/ |
19
|
|
View Code Duplication |
class Disallow implements GroupMemberInterface, PathDirectiveInterface |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Directive value. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $value = ''; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function __construct(string $raw) |
32
|
|
|
{ |
33
|
|
|
if (!preg_match('/^disallow:\s*([^# ]+).*/i', $raw, $matches)) { |
34
|
|
|
throw InvalidDirectiveException::create($raw); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$this->value = trim($matches[1]); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
public static function getField(): string |
44
|
|
|
{ |
45
|
|
|
return 'disallow'; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function getValue(): string |
52
|
|
|
{ |
53
|
|
|
return $this->value; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function __toString(): string |
60
|
|
|
{ |
61
|
|
|
return sprintf('Disallow: %s', $this->value); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
|
public function getRegex(): string |
68
|
|
|
{ |
69
|
|
|
// "*" matches any sequence of characters (zero or more) |
70
|
|
|
$value = str_replace('*', '.*', $this->value); |
71
|
|
|
|
72
|
|
|
// "?" matches any character |
73
|
|
|
$value = str_replace('?', '*', $value); |
74
|
|
|
|
75
|
|
|
// "\" supresses syntactic significance of a special character |
76
|
|
|
$value = str_replace('\*', '\?', $value); |
77
|
|
|
$value = str_replace('\.*', '\*', $value); |
78
|
|
|
|
79
|
|
|
return $value; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
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.