From 4cdd720082bf36d41ffa11e9be06bead921db8d2 Mon Sep 17 00:00:00 2001 From: Yusuf Kandemir Date: Sun, 11 Nov 2018 22:47:37 +0300 Subject: [PATCH] Refactoring --- src/MicrodataDOMElement.php | 45 ++++++++++++++++++------------------- src/MicrodataParser.php | 7 +++--- 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/MicrodataDOMElement.php b/src/MicrodataDOMElement.php index e7ba087..d6a24e2 100644 --- a/src/MicrodataDOMElement.php +++ b/src/MicrodataDOMElement.php @@ -15,7 +15,7 @@ class MicrodataDOMElement extends \DOMElement $pending = array_merge($pending, $this->getChildElementNodes()); if ($this->hasAttribute('itemref')) { - $tokens = preg_split('/\s+/', $this->getAttribute('itemref')); + $tokens = $this->tokenizeAttribute('itemref'); foreach ($tokens as $token) { // @todo Implement xpath query and get the first item @@ -42,26 +42,21 @@ class MicrodataDOMElement extends \DOMElement } } - $results = array_reverse($results); - - return $results; + return array_reverse($results); } public function getPropertyNames() { - $itemprop = $this->getAttribute('itemprop'); - $tokens = $itemprop ? preg_split('/\s+/', $itemprop) : []; + $tokens = $this->tokenizeAttribute('itemprop'); $properties = []; foreach ($tokens as $token) { - if ($this->isAbsoluteUri($token)) { - $properties[] = $token; - } elseif ($this->isTypedItem()) { - $properties[] = /*$vocabularyIdentifier . */ $token; - } else { - $properties[] = $token; + if (!$this->isAbsoluteUri($token) && $this->tokenizeAttribute('itemtype')) { + $token = /*$vocabularyIdentifier . */ $token; } + + $properties[] = $token; } $properties = array_unique($properties); @@ -124,17 +119,6 @@ class MicrodataDOMElement extends \DOMElement } } - public function isTypedItem() - { - $tokens = []; - - if ($this->hasAttribute('itemtype')) { - $tokens = preg_split("/\s+/", $this->getAttribute('itemtype')); - } - - return !empty($tokens); - } - protected function isAbsoluteUri(string $uri) { return preg_match("/^\w+:/", trim($uri)); @@ -152,4 +136,19 @@ class MicrodataDOMElement extends \DOMElement return $childNodes; } + + public function tokenizeAttribute($attributeName) { + $attribute = []; + + if($this->hasAttribute($attributeName)) { + $attribute = $this->tokenize($this->getAttribute($attributeName)); + } + + return $attribute; + } + + protected function tokenize($attribute) + { + return preg_split('/\s+/', trim($attribute)); + } } diff --git a/src/MicrodataParser.php b/src/MicrodataParser.php index f265b40..d1c5486 100644 --- a/src/MicrodataParser.php +++ b/src/MicrodataParser.php @@ -33,12 +33,11 @@ class MicrodataParser $memory[] = $item; - $itemtype = $item->getAttribute('itemtype'); - $result->type = $itemtype ? preg_split('/\s+/', $itemtype) : []; + $result->type = $this->tokenizeAttribute('itemtype'); // @todo Check if types are valid absolute urls - if ($itemId = $item->getAttribute('itemid')) { - $result->id = $itemId; + if ($item->hasAttribute('itemid')) { + $result->id = $item->getAttribute('itemid'); } // @todo Check if item ids are valid absolute urls or like isbn:xxx