Removed comments that only shows steps
This steps was only helpful while implementing the algorithm. And could be useful while comparing with the W3C specification, but they take up too much space.
This commit is contained in:
parent
793d596541
commit
65f35e747b
@ -6,15 +6,12 @@ class MicrodataDOMElement extends \DOMElement
|
|||||||
{
|
{
|
||||||
public function getProperties()
|
public function getProperties()
|
||||||
{
|
{
|
||||||
// Step 1
|
|
||||||
$results = [];
|
$results = [];
|
||||||
$memory = [];
|
$memory = [];
|
||||||
$pending = [];
|
$pending = [];
|
||||||
|
|
||||||
// Step 2
|
|
||||||
$memory[] = $this;
|
$memory[] = $this;
|
||||||
|
|
||||||
// Step 3
|
|
||||||
if ($this->hasChildNodes()) {
|
if ($this->hasChildNodes()) {
|
||||||
$childNodes = iterator_to_array($this->childNodes);
|
$childNodes = iterator_to_array($this->childNodes);
|
||||||
|
|
||||||
@ -25,7 +22,6 @@ class MicrodataDOMElement extends \DOMElement
|
|||||||
$pending = array_merge($pending, $childNodes);
|
$pending = array_merge($pending, $childNodes);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 4
|
|
||||||
if ($this->hasAttribute('itemref')) {
|
if ($this->hasAttribute('itemref')) {
|
||||||
$tokens = preg_split('/\s+/', $this->getAttribute('itemref'));
|
$tokens = preg_split('/\s+/', $this->getAttribute('itemref'));
|
||||||
|
|
||||||
@ -34,17 +30,9 @@ class MicrodataDOMElement extends \DOMElement
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 5
|
|
||||||
while ($pending) {
|
while ($pending) {
|
||||||
// Step 6
|
|
||||||
$current = array_pop($pending);
|
$current = array_pop($pending);
|
||||||
|
|
||||||
// Step 7
|
|
||||||
// in_array can't compare objects
|
|
||||||
/*if (in_array($current, $memory)) {
|
|
||||||
// There is MicrodataError
|
|
||||||
continue;
|
|
||||||
}*/
|
|
||||||
$error = false;
|
$error = false;
|
||||||
|
|
||||||
foreach ($memory as $memory_item) {
|
foreach ($memory as $memory_item) {
|
||||||
@ -59,10 +47,8 @@ class MicrodataDOMElement extends \DOMElement
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 8
|
|
||||||
$memory[] = $current;
|
$memory[] = $current;
|
||||||
|
|
||||||
// Step 9
|
|
||||||
if (! $current->hasAttribute('itemscope')) {
|
if (! $current->hasAttribute('itemscope')) {
|
||||||
if ($current->hasChildNodes()) {
|
if ($current->hasChildNodes()) {
|
||||||
$childNodes = iterator_to_array($current->childNodes);
|
$childNodes = iterator_to_array($current->childNodes);
|
||||||
@ -75,32 +61,23 @@ class MicrodataDOMElement extends \DOMElement
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 10
|
|
||||||
if ($current->hasAttribute('itemprop') && /* hasPropertyNames */ $current->getPropertyNames()) {
|
if ($current->hasAttribute('itemprop') && /* hasPropertyNames */ $current->getPropertyNames()) {
|
||||||
$results[] = $current;
|
$results[] = $current;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 11: Return to loop
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 12: End of loop. Sort results in tree order.
|
|
||||||
|
|
||||||
$results = array_reverse($results);
|
$results = array_reverse($results);
|
||||||
|
|
||||||
// Step 13
|
|
||||||
return $results;
|
return $results;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getPropertyNames()
|
public function getPropertyNames()
|
||||||
{
|
{
|
||||||
// Step 1
|
|
||||||
$itemprop = $this->getAttribute('itemprop');
|
$itemprop = $this->getAttribute('itemprop');
|
||||||
$tokens = $itemprop ? preg_split('/\s+/', $itemprop) : [];
|
$tokens = $itemprop ? preg_split('/\s+/', $itemprop) : [];
|
||||||
|
|
||||||
// Step 2
|
|
||||||
$properties = [];
|
$properties = [];
|
||||||
|
|
||||||
// Step 3
|
|
||||||
foreach ($tokens as $token) {
|
foreach ($tokens as $token) {
|
||||||
if ($this->isAbsoluteUri($token)) {
|
if ($this->isAbsoluteUri($token)) {
|
||||||
$properties[] = $token;
|
$properties[] = $token;
|
||||||
|
@ -16,50 +16,36 @@ class MicrodataParser
|
|||||||
|
|
||||||
public function extractMicrodata()
|
public function extractMicrodata()
|
||||||
{
|
{
|
||||||
// Step 1
|
|
||||||
$result = new \stdClass;
|
$result = new \stdClass;
|
||||||
|
|
||||||
// Step 2
|
|
||||||
$items = [];
|
$items = [];
|
||||||
|
|
||||||
// Step 3
|
|
||||||
foreach ($this->topLevelItems as $topLevelItem) {
|
foreach ($this->topLevelItems as $topLevelItem) {
|
||||||
$items[] = $this->getObject($topLevelItem);
|
$items[] = $this->getObject($topLevelItem);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 4
|
|
||||||
$result->items = $items;
|
$result->items = $items;
|
||||||
|
|
||||||
// Step 5
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getObject(\DOMElement $item, $memory = [])
|
protected function getObject(\DOMElement $item, $memory = [])
|
||||||
{
|
{
|
||||||
// Step 1
|
|
||||||
$result = new \stdClass;
|
$result = new \stdClass;
|
||||||
|
|
||||||
// Step 2 in 2nd parameter of this function
|
|
||||||
// $memory = [];
|
|
||||||
|
|
||||||
// Step 3
|
|
||||||
$memory[] = $item;
|
$memory[] = $item;
|
||||||
|
|
||||||
// Step 4
|
|
||||||
$itemtype = $item->getAttribute('itemtype');
|
$itemtype = $item->getAttribute('itemtype');
|
||||||
$result->type = $itemtype ? preg_split('/\s+/', $itemtype) : [];
|
$result->type = $itemtype ? preg_split('/\s+/', $itemtype) : [];
|
||||||
// @todo Check if types are valid absolute urls
|
// @todo Check if types are valid absolute urls
|
||||||
|
|
||||||
// Step 5
|
|
||||||
if ($itemId = $item->getAttribute('itemid')) {
|
if ($itemId = $item->getAttribute('itemid')) {
|
||||||
$result->id = $itemId;
|
$result->id = $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
|
||||||
|
|
||||||
// Step 6
|
|
||||||
$properties = new \stdClass;
|
$properties = new \stdClass;
|
||||||
|
|
||||||
// Step 7
|
|
||||||
foreach ($item->getProperties() as $element) {
|
foreach ($item->getProperties() as $element) {
|
||||||
$value = $element->getPropertyValue();
|
$value = $element->getPropertyValue();
|
||||||
|
|
||||||
@ -80,10 +66,8 @@ class MicrodataParser
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 8
|
|
||||||
$result->properties = $properties;
|
$result->properties = $properties;
|
||||||
|
|
||||||
// Step 9
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user