1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Robots; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
class RobotsMeta |
8
|
|
|
{ |
9
|
|
|
protected $robotsMetaTagProperties = []; |
10
|
|
|
|
11
|
|
|
public static function readFrom(string $source): self |
12
|
|
|
{ |
13
|
|
|
$content = @file_get_contents($source); |
14
|
|
|
|
15
|
|
|
if ($content === false) { |
16
|
|
|
throw new InvalidArgumentException("Could not read from source `{$source}`"); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
return new self($content); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public static function create(string $source): self |
23
|
|
|
{ |
24
|
|
|
return new self($source); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function __construct(string $html) |
28
|
|
|
{ |
29
|
|
|
$this->robotsMetaTagProperties = $this->findRobotsMetaTagProperties($html); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function mayIndex(): bool |
33
|
|
|
{ |
34
|
|
|
return ! $this->noindex(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function mayFollow(): bool |
38
|
|
|
{ |
39
|
|
|
return ! $this->nofollow(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function noindex(): bool |
43
|
|
|
{ |
44
|
|
|
return $this->robotsMetaTagProperties['noindex'] ?? false; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function nofollow(): bool |
48
|
|
|
{ |
49
|
|
|
return $this->robotsMetaTagProperties['nofollow'] ?? false; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function findRobotsMetaTagProperties(string $html): array |
53
|
|
|
{ |
54
|
|
|
$metaTagLine = $this->findRobotsMetaTagLine($html); |
55
|
|
|
|
56
|
|
|
return [ |
57
|
|
|
'noindex' => $metaTagLine |
58
|
|
|
? strpos(strtolower($metaTagLine), 'noindex') !== false |
59
|
|
|
: false, |
60
|
|
|
|
61
|
|
|
'nofollow' => $metaTagLine |
62
|
|
|
? strpos(strtolower($metaTagLine), 'nofollow') !== false |
63
|
|
|
: false, |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function findRobotsMetaTagLine(string $html): ?string |
68
|
|
|
{ |
69
|
|
|
if (preg_match('/\<meta name="robots".*?\>/mis', $html, $matches)) { |
70
|
|
|
return $matches[0]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return null; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|