OGPResolverでTwitter Cardsの解析も行えるようにした (#9)

This commit is contained in:
shibafu 2018-06-13 01:00:11 +09:00
parent a0e4063c47
commit 9bd22d9f77

View File

@ -23,30 +23,24 @@ class OGPResolver implements Resolver
$metadata = new Metadata(); $metadata = new Metadata();
$titleNode = $xpath->query('//meta[@*="og:title"]'); $metadata->title = $this->findContent($xpath, '//meta[@*="og:title"]', '//meta[@*="twitter:title"]');
foreach ($titleNode as $node) { $metadata->description = $this->findContent($xpath, '//meta[@*="og:description"]', '//meta[@*="twitter:description"]');
if (!empty($node->getAttribute('content'))) { $metadata->image = $this->findContent($xpath, '//meta[@*="og:image"]', '//meta[@*="twitter:image"]');
$metadata->title = $node->getAttribute('content');
break;
}
}
$descriptionNode = $xpath->query('//meta[@*="og:description"]');
foreach ($descriptionNode as $node) {
if (!empty($node->getAttribute('content'))) {
$metadata->description = $node->getAttribute('content');
break;
}
}
$imageNode = $xpath->query('//meta[@*="og:image"]');
foreach ($imageNode as $node) {
if (!empty($node->getAttribute('content'))) {
$metadata->image = $node->getAttribute('content');
break;
}
}
return $metadata; return $metadata;
} }
private function findContent(\DOMXPath $xpath, string ...$expressions)
{
foreach ($expressions as $expression) {
$nodes = $xpath->query($expression);
foreach ($nodes as $node) {
$content = $node->getAttribute('content');
if (!empty($content)) {
return $content;
}
}
}
return '';
}
} }