microdata-parser/src/MicrodataParser.php

98 lines
2.4 KiB
PHP
Raw Normal View History

2018-11-10 12:05:55 +00:00
<?php
namespace YusufKandemir\MicrodataParser;
class MicrodataParser
{
protected $topLevelItems;
public function __construct(\DOMDocument $dom)
{
2018-11-11 17:49:54 +00:00
$dom->registerNodeClass(\DOMElement::class, MicrodataDOMElement::class);
2018-11-10 12:05:55 +00:00
$xpath = new \DOMXPath($dom);
$this->topLevelItems = $xpath->query('//*[@itemscope and not(@itemprop)]');
}
public function extractMicrodata()
{
// Step 1
$result = new \stdClass;
// Step 2
$items = [];
// Step 3
// items = map("top-level microdata items", item => getObject(item))
$items = array_map([$this, 'getObject'], iterator_to_array($this->topLevelItems));
/*foreach ($topLevelItems as $topLevelItem) {
$items[] = getObject($topLevelItem);
}*/
// Step 4
$result->items = $items;
// Step 5
return $result;
}
protected function getObject(\DOMElement $item, $memory = [])
{
// Step 1
$result = new \stdClass;
// Step 2 in 2nd parameter of this function
// $memory = [];
// Step 3
$memory[] = $item;
// Step 4
$itemtype = $item->getAttribute('itemtype');
$result->type = $itemtype ? preg_split('/\s+/', $itemtype) : [];
// @todo Check if types are valid absolute urls
// Step 5
if ($itemId = $item->getAttribute('itemid')) {
$result->id = $itemId;
}
// @todo Check if item ids are valid absolute urls or like isbn:xxx
// Step 6
$properties = new \stdClass;
// Step 7
2018-11-11 17:49:54 +00:00
foreach ($item->getProperties() as $element) {
$value = $element->getPropertyValue();
2018-11-10 12:05:55 +00:00
if ($this->isItem($value)) {
foreach ($memory as $memory_item) {
if ($element->isSameNode($memory_item)) {
$value = 'ERROR';
}
}
if ($value != 'ERROR') {
$value = $this->getObject($value, $memory);
}
}
2018-11-11 17:49:54 +00:00
foreach ($element->getPropertyNames() as $name) {
2018-11-10 12:05:55 +00:00
$properties->{$name}[] = $value;
}
}
// Step 8
$result->properties = $properties;
// Step 9
return $result;
}
protected function isItem($element)
{
return $element instanceof \DOMElement && $element->hasAttribute('itemscope');
}
}