Refactoring

This commit is contained in:
Yusuf Kandemir 2018-11-11 22:47:37 +03:00
parent faeb1f97a0
commit 4cdd720082
2 changed files with 25 additions and 27 deletions

View File

@ -15,7 +15,7 @@ class MicrodataDOMElement extends \DOMElement
$pending = array_merge($pending, $this->getChildElementNodes()); $pending = array_merge($pending, $this->getChildElementNodes());
if ($this->hasAttribute('itemref')) { if ($this->hasAttribute('itemref')) {
$tokens = preg_split('/\s+/', $this->getAttribute('itemref')); $tokens = $this->tokenizeAttribute('itemref');
foreach ($tokens as $token) { foreach ($tokens as $token) {
// @todo Implement xpath query and get the first item // @todo Implement xpath query and get the first item
@ -42,26 +42,21 @@ class MicrodataDOMElement extends \DOMElement
} }
} }
$results = array_reverse($results); return array_reverse($results);
return $results;
} }
public function getPropertyNames() public function getPropertyNames()
{ {
$itemprop = $this->getAttribute('itemprop'); $tokens = $this->tokenizeAttribute('itemprop');
$tokens = $itemprop ? preg_split('/\s+/', $itemprop) : [];
$properties = []; $properties = [];
foreach ($tokens as $token) { foreach ($tokens as $token) {
if ($this->isAbsoluteUri($token)) { if (!$this->isAbsoluteUri($token) && $this->tokenizeAttribute('itemtype')) {
$properties[] = $token; $token = /*$vocabularyIdentifier . */ $token;
} elseif ($this->isTypedItem()) {
$properties[] = /*$vocabularyIdentifier . */ $token;
} else {
$properties[] = $token;
} }
$properties[] = $token;
} }
$properties = array_unique($properties); $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) protected function isAbsoluteUri(string $uri)
{ {
return preg_match("/^\w+:/", trim($uri)); return preg_match("/^\w+:/", trim($uri));
@ -152,4 +136,19 @@ class MicrodataDOMElement extends \DOMElement
return $childNodes; 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));
}
} }

View File

@ -33,12 +33,11 @@ class MicrodataParser
$memory[] = $item; $memory[] = $item;
$itemtype = $item->getAttribute('itemtype'); $result->type = $this->tokenizeAttribute('itemtype');
$result->type = $itemtype ? preg_split('/\s+/', $itemtype) : [];
// @todo Check if types are valid absolute urls // @todo Check if types are valid absolute urls
if ($itemId = $item->getAttribute('itemid')) { if ($item->hasAttribute('itemid')) {
$result->id = $itemId; $result->id = $item->getAttribute('itemid');
} }
// @todo Check if item ids are valid absolute urls or like isbn:xxx // @todo Check if item ids are valid absolute urls or like isbn:xxx