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());
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));
}
}

View File

@ -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