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;
if ($this->hasChildNodes()) {
$childNodes = iterator_to_array($this->childNodes);
$childNodes = array_filter($childNodes, function ($node) {
return $node instanceof \DOMElement;
}); // Get only DOMElements
$pending = array_merge($pending, $childNodes);
}
$pending = array_merge($pending, $this->getChildElementNodes());
if ($this->hasAttribute('itemref')) {
$tokens = preg_split('/\s+/', $this->getAttribute('itemref'));
@ -33,32 +25,16 @@ class MicrodataDOMElement extends \DOMElement
while ($pending) {
$current = array_pop($pending);
$error = false;
foreach ($memory as $memory_item) {
if ($current->isSameNode($memory_item)) {
// There is MicrodataError
$error = true;
break;
continue 2; // Skip next part and continue while loop if memory contains $current
}
}
if ($error) {
continue;
}
$memory[] = $current;
if (! $current->hasAttribute('itemscope')) {
if ($current->hasChildNodes()) {
$childNodes = iterator_to_array($current->childNodes);
$childNodes = array_filter($childNodes, function ($node) {
return $node instanceof \DOMElement;
});
$pending = array_merge($pending, $childNodes);
}
$pending = array_merge($pending, $current->getChildElementNodes());
}
if ($current->hasAttribute('itemprop') && /* hasPropertyNames */ $current->getPropertyNames()) {
@ -163,4 +139,17 @@ class MicrodataDOMElement extends \DOMElement
{
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;
$items = [];
$result->items = [];
foreach ($this->topLevelItems as $topLevelItem) {
$items[] = $this->getObject($topLevelItem);
$result->items[] = $this->getObject($topLevelItem);
}
$result->items = $items;
return $result;
}
@ -53,6 +51,7 @@ class MicrodataParser
foreach ($memory as $memory_item) {
if ($element->isSameNode($memory_item)) {
$value = 'ERROR';
break;
}
}