From e26f84fd0c31c71538effedb29661a357317c762 Mon Sep 17 00:00:00 2001 From: Yusuf Kandemir Date: Sun, 16 Dec 2018 12:23:09 +0300 Subject: [PATCH] Refactoring by extracting a method --- src/MicrodataDOMElement.php | 38 ++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/src/MicrodataDOMElement.php b/src/MicrodataDOMElement.php index 6ea8035..541cfe6 100644 --- a/src/MicrodataDOMElement.php +++ b/src/MicrodataDOMElement.php @@ -15,17 +15,7 @@ class MicrodataDOMElement extends \DOMElement $memory = [$this]; $pending = $this->getChildElementNodes(); - if ($this->hasAttribute('itemref')) { - $tokens = $this->tokenizeAttribute('itemref'); - - foreach ($tokens as $token) { - $references = $this->ownerDocument->xpath->query('//*[@id="'.$token.'"]'); - - if ($first = $references->item(0)) { - $pending[] = $first; - } - } - } + $pending = array_merge($pending, $this->getReferenceNodes()); while ($pending) { $current = array_pop($pending); @@ -210,4 +200,30 @@ class MicrodataDOMElement extends \DOMElement { 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; + } }