Refactoring by extracting a method

This commit is contained in:
Yusuf Kandemir 2018-12-16 12:23:09 +03:00
parent 1ce3f5fd17
commit e26f84fd0c

View File

@ -15,17 +15,7 @@ class MicrodataDOMElement extends \DOMElement
$memory = [$this]; $memory = [$this];
$pending = $this->getChildElementNodes(); $pending = $this->getChildElementNodes();
if ($this->hasAttribute('itemref')) { $pending = array_merge($pending, $this->getReferenceNodes());
$tokens = $this->tokenizeAttribute('itemref');
foreach ($tokens as $token) {
$references = $this->ownerDocument->xpath->query('//*[@id="'.$token.'"]');
if ($first = $references->item(0)) {
$pending[] = $first;
}
}
}
while ($pending) { while ($pending) {
$current = array_pop($pending); $current = array_pop($pending);
@ -210,4 +200,30 @@ class MicrodataDOMElement extends \DOMElement
{ {
return preg_split('/\s+/', trim($attribute)); return preg_split('/\s+/', trim($attribute));
} }
/**
* Finds the nodes that this node references through the document
*
* @see https://www.w3.org/TR/microdata/#dfn-item-properties 4th step
*
* @return array
*/
protected function getReferenceNodes(): array
{
$referenceNodes = [];
if ($this->hasAttribute('itemref')) {
$tokens = $this->tokenizeAttribute('itemref');
foreach ($tokens as $token) {
$references = $this->ownerDocument->xpath->query('//*[@id="' . $token . '"]');
if ($first = $references->item(0)) {
$referenceNodes[] = $first;
}
}
}
return $referenceNodes;
}
} }