-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTitleCrawler.php
More file actions
54 lines (44 loc) · 1.14 KB
/
Copy pathTitleCrawler.php
File metadata and controls
54 lines (44 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
/**
* Bit&Black Document Crawler.
*
* @author Tobias Köngeter
* @copyright Copyright © Bit&Black
* @link https://www.bitandblack.com
* @license MIT
*/
namespace BitAndBlack\DocumentCrawler\Crawler;
use BitAndBlack\DocumentCrawler\ResourceHandler\ResourceHandlerInterface;
use Symfony\Component\DomCrawler\Crawler;
/**
* Crawl and extract the title of a document, that has been declared with `<title>...</title>`.
*/
class TitleCrawler implements CrawlerInterface
{
private ?string $title = null;
public function __construct(
private readonly Crawler $crawler,
) {
}
public function crawlContent(): void
{
$titleMissing = '__TITLE_MISSING__';
$title = $this->crawler
->filter('head > title')
->first()
->text($titleMissing)
;
if ($titleMissing !== $title) {
$this->title = $title;
}
}
public function getTitle(): ?string
{
return $this->title;
}
public function setResourceHandler(ResourceHandlerInterface $resourceHandler): self
{
// Not needed here.
return $this;
}
}