Some refactoring

This commit is contained in:
Yusuf Kandemir 2018-11-11 21:53:08 +03:00
parent 65f35e747b
commit faeb1f97a0
2 changed files with 19 additions and 31 deletions

View File

@ -12,15 +12,7 @@ class MicrodataDOMElement extends \DOMElement
$memory[] = $this; $memory[] = $this;
if ($this->hasChildNodes()) { $pending = array_merge($pending, $this->getChildElementNodes());
$childNodes = iterator_to_array($this->childNodes);
$childNodes = array_filter($childNodes, function ($node) {
return $node instanceof \DOMElement;
}); // Get only DOMElements
$pending = array_merge($pending, $childNodes);
}
if ($this->hasAttribute('itemref')) { if ($this->hasAttribute('itemref')) {
$tokens = preg_split('/\s+/', $this->getAttribute('itemref')); $tokens = preg_split('/\s+/', $this->getAttribute('itemref'));
@ -33,32 +25,16 @@ class MicrodataDOMElement extends \DOMElement
while ($pending) { while ($pending) {
$current = array_pop($pending); $current = array_pop($pending);
$error = false;
foreach ($memory as $memory_item) { foreach ($memory as $memory_item) {
if ($current->isSameNode($memory_item)) { if ($current->isSameNode($memory_item)) {
// There is MicrodataError continue 2; // Skip next part and continue while loop if memory contains $current
$error = true;
break;
} }
} }
if ($error) {
continue;
}
$memory[] = $current; $memory[] = $current;
if (! $current->hasAttribute('itemscope')) { if (! $current->hasAttribute('itemscope')) {
if ($current->hasChildNodes()) { $pending = array_merge($pending, $current->getChildElementNodes());
$childNodes = iterator_to_array($current->childNodes);
$childNodes = array_filter($childNodes, function ($node) {
return $node instanceof \DOMElement;
});
$pending = array_merge($pending, $childNodes);
}
} }
if ($current->hasAttribute('itemprop') && /* hasPropertyNames */ $current->getPropertyNames()) { if ($current->hasAttribute('itemprop') && /* hasPropertyNames */ $current->getPropertyNames()) {
@ -163,4 +139,17 @@ class MicrodataDOMElement extends \DOMElement
{ {
return preg_match("/^\w+:/", trim($uri)); return preg_match("/^\w+:/", trim($uri));
} }
protected function getChildElementNodes()
{
$childNodes = [];
foreach ($this->childNodes as $childNode) {
if ($childNode->nodeType == XML_ELEMENT_NODE) {
$childNodes[] = $childNode;
}
}
return $childNodes;
}
} }

View File

@ -18,14 +18,12 @@ class MicrodataParser
{ {
$result = new \stdClass; $result = new \stdClass;
$items = []; $result->items = [];
foreach ($this->topLevelItems as $topLevelItem) { foreach ($this->topLevelItems as $topLevelItem) {
$items[] = $this->getObject($topLevelItem); $result->items[] = $this->getObject($topLevelItem);
} }
$result->items = $items;
return $result; return $result;
} }
@ -53,6 +51,7 @@ class MicrodataParser
foreach ($memory as $memory_item) { foreach ($memory as $memory_item) {
if ($element->isSameNode($memory_item)) { if ($element->isSameNode($memory_item)) {
$value = 'ERROR'; $value = 'ERROR';
break;
} }
} }