Skip to content

Commit 27919d3

Browse files
committed
improved code
1 parent d6e477b commit 27919d3

File tree

9 files changed

+95
-54
lines changed

9 files changed

+95
-54
lines changed

src/Detectors/AuthorName.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ public function detect(): ?string
1111
$document = $this->extractor->getDocument();
1212

1313
return $oembed->str('author_name')
14-
?: $document->meta('article:author')
15-
?: $document->meta('book:author')
16-
?: $document->meta('sailthru.author')
17-
?: $document->meta('lp.article:author')
18-
?: $document->meta('twitter:creator');
14+
?: $document->meta(
15+
'article:author',
16+
'book:author',
17+
'sailthru.author',
18+
'lp.article:author',
19+
'twitter:creator'
20+
);
1921
}
2022
}

src/Detectors/Code.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ private function detectFromOpenGraph(): ?EmbedCode
3535
{
3636
$document = $this->extractor->getDocument();
3737

38-
$url = $document->meta('og:video:secure_url')
39-
?: $document->meta('og:video:url')
40-
?: $document->meta('og:video');
38+
$url = $document->meta('og:video:secure_url', 'og:video:url', 'og:video');
4139

4240
if (!$url) {
4341
return null;

src/Detectors/Description.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ public function detect(): ?string
1212
$ld = $this->extractor->getLinkedData();
1313

1414
return $oembed->str('description')
15-
?: $document->meta('og:description')
16-
?: $document->meta('twitter:description')
17-
?: $document->meta('lp:description')
18-
?: $document->meta('description')
15+
?: $document->meta('og:description', 'twitter:description', 'lp:description', 'description')
1916
?: $ld->str('description');
2017
}
2118
}

src/Detectors/Image.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,7 @@ public function detect(): ?string
1414
return $oembed->url('image')
1515
?: $oembed->url('thumbnail')
1616
?: $oembed->url('thumbnail_url')
17-
?: $document->meta('og:image')
18-
?: $document->meta('og:image:url')
19-
?: $document->meta('og:image:secure_url')
20-
?: $document->meta('twitter:image')
21-
?: $document->meta('twitter:image:src')
22-
?: $document->meta('lp:image')
17+
?: $document->meta('og:image', 'og:image:url', 'og:image:secure_url', 'twitter:image', 'twitter:image:src', 'lp:image')
2318
?: $document->link('image_src')
2419
?: $ld->url('image', 'url');
2520
}

src/Detectors/Keywords.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ public function detect(): ?array
1111
$document = $this->extractor->getDocument();
1212

1313
$metas = [
14-
['name' => 'keywords'],
15-
['property' => 'og:video:tag'],
16-
['property' => 'og:article:tag'],
17-
['property' => 'og:video:tag'],
18-
['property' => 'og:book:tag'],
19-
['property' => 'lp.article:section'],
14+
'keywords',
15+
'og:video:tag',
16+
'og:article:tag',
17+
'og:video:tag',
18+
'og:book:tag',
19+
'lp.article:section',
2020
];
2121

22-
foreach ($metas as $attr) {
23-
$value = $document->select('.//meta', $attr)->strAll('content');
22+
foreach ($metas as $type) {
23+
$value = $document->metas($type);
2424

2525
if ($value) {
2626
$tags = array_merge($tags, self::toArray($value));

src/Detectors/Language.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ public function detect(): ?string
1212

1313
return $document->select('/html')->str('lang')
1414
?: $document->select('/html')->str('xml:lang')
15-
?: $document->meta('language')
16-
?: $document->meta('lang')
17-
?: $document->meta('og:locale')
18-
?: $document->meta('dc:language')
15+
?: $document->meta('language', 'lang', 'og:locale', 'dc:language')
1916
?: $document->select('.//meta', ['http-equiv' => 'content-language'])->str('content')
2017
?: $ld->str('inLanguage');
2118
}

src/Detectors/PublishedTime.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,29 @@ public function detect(): ?string
1212
$ld = $this->extractor->getLinkedData();
1313

1414
return $oembed->str('pubdate')
15-
?: $document->meta('article:published_time')
16-
?: $document->meta('created')
17-
?: $document->meta('date')
18-
?: $document->meta('datepublished')
19-
?: $document->meta('music:release_date')
20-
?: $document->meta('video:release_date')
21-
?: $document->meta('newsrepublic:publish_date')
15+
?: $document->meta(
16+
'article:published_time',
17+
'created',
18+
'date',
19+
'datepublished',
20+
'music:release_date',
21+
'video:release_date',
22+
'newsrepublic:publish_date'
23+
)
2224
?: $ld->str('pagePublished')
2325
?: $this->detectFromPath()
24-
?: $document->meta('pagerender')
25-
?: $document->meta('pub_date')
26-
?: $document->meta('publication-date')
27-
?: $document->meta('lp.article:published_time')
28-
?: $document->meta('lp.article:modified_time')
29-
?: $document->meta('publish-date')
30-
?: $document->meta('rc.datecreation')
31-
?: $document->meta('timestamp')
32-
?: $document->meta('sailthru.date')
33-
?: $document->meta('article:modified_time');
26+
?: $document->meta(
27+
'pagerender',
28+
'pub_date',
29+
'publication-date',
30+
'lp.article:published_time',
31+
'lp.article:modified_time',
32+
'publish-date',
33+
'rc.datecreation',
34+
'timestamp',
35+
'sailthru.date',
36+
'article:modified_time'
37+
);
3438
}
3539

3640
/**

src/Detectors/Title.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ public function detect(): ?string
1111
$document = $this->extractor->getDocument();
1212

1313
return $oembed->str('title')
14-
?: $document->meta('og:title')
15-
?: $document->meta('twitter:title')
16-
?: $document->meta('lp:title')
14+
?: $document->meta('og:title', 'twitter:title', 'lp:title')
1715
?: $document->select('.//head/title')->str();
1816
}
1917
}

src/Document.php

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Document
1313
private Extractor $extractor;
1414
private DOMDocument $document;
1515
private DOMXPath $xpath;
16+
private array $metas;
1617

1718
public function __construct(Extractor $extractor)
1819
{
@@ -77,11 +78,38 @@ public function select(string $query, array $attributes = null, DOMNode $context
7778
return new QueryResult($this->xpath->query($query, $context), $this->extractor);
7879
}
7980

80-
public function meta(string $type): ?string
81+
public function meta(string ...$types): ?string
8182
{
82-
return $this->select('.//meta', ['name' => $type])->str('content')
83-
?: $this->select('.//meta', ['property' => $type])->str('content')
84-
?: $this->select('.//meta', ['itemprop' => $type])->str('content');
83+
$metas = $this->getMetas();
84+
85+
foreach ($types as $type) {
86+
$values = $metas[$type] ?? null;
87+
$value = !empty($values[0]) ? clean($values[0]) : null;
88+
89+
if ($value) {
90+
return $value;
91+
}
92+
}
93+
94+
return null;
95+
}
96+
97+
public function metas(string ...$types): array
98+
{
99+
$metas = $this->getMetas();
100+
101+
foreach ($types as $type) {
102+
$values = $metas[$type] ?? [];
103+
104+
return array_values(
105+
array_filter(
106+
array_map(
107+
fn ($value) => clean($value),
108+
$values
109+
)
110+
)
111+
);
112+
}
85113
}
86114

87115
public function link(string $rel, array $extra = []): ?string
@@ -93,4 +121,26 @@ public function __toString(): string
93121
{
94122
return Parser::stringify($this->getDocument());
95123
}
124+
125+
private function getMetas(): array
126+
{
127+
if (isset($this->metas)) {
128+
return $this->metas;
129+
}
130+
131+
$this->metas = [];
132+
133+
foreach ($this->select('.//meta')->nodes() as $node) {
134+
$type = $node->getAttribute('name') ?: $node->getAttribute('property') ?: $node->getAttribute('itemprop');
135+
$value = $node->getAttribute('content');
136+
137+
if (!empty($value) && !empty($type)) {
138+
$type = strtolower($type);
139+
$this->metas[$type] ??= [];
140+
$this->metas[$type][] = $value;
141+
}
142+
}
143+
144+
return $this->metas;
145+
}
96146
}

0 commit comments

Comments
 (0)