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

This commit is contained in:
shibafu 2018-06-13 01:00:11 +09:00
parent a0e4063c47
commit 9bd22d9f77
1 changed files with 17 additions and 23 deletions

View File

@ -23,30 +23,24 @@ class OGPResolver implements Resolver
$metadata = new Metadata();
$titleNode = $xpath->query('//meta[@*="og:title"]');
foreach ($titleNode as $node) {
if (!empty($node->getAttribute('content'))) {
$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;
}
}
$metadata->title = $this->findContent($xpath, '//meta[@*="og:title"]', '//meta[@*="twitter:title"]');
$metadata->description = $this->findContent($xpath, '//meta[@*="og:description"]', '//meta[@*="twitter:description"]');
$metadata->image = $this->findContent($xpath, '//meta[@*="og:image"]', '//meta[@*="twitter:image"]');
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 '';
}
}