Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00%
0 / 1
66.67%
4 / 6
CRAP
84.78%
39 / 46
AbstractDomParser
0.00%
0 / 1
66.67%
4 / 6
16.90
84.78%
39 / 46
 createDocumentFromXML($content)
100.00%
1 / 1
1
100.00%
6 / 6
 createDocumentFromHTML($content)
0.00%
0 / 1
2
0.00%
0 / 6
 getNodeValueByTagName(DOMElement $node, $tagName)
100.00%
1 / 1
3
100.00%
7 / 7
 getNodeValueByTagNameNS(DOMElement $node, $namespace, $tagName)
0.00%
0 / 1
3.03
85.71%
6 / 7
 getNodeValuesByTagName(DOMElement $node, $tagName)
100.00%
1 / 1
4
100.00%
10 / 10
 getNodePropertyByTagName(\DOMElement $node, $tagName, $propertyName)
100.00%
1 / 1
4
100.00%
10 / 10
<?php
/**
* This file is part of the FastFeed package.
*
* (c) Daniel González <daniel@desarrolla2.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FastFeed\Parser;
use DOMElement;
use DOMDocument;
/**
* AbstractDomParser
*/
abstract class AbstractDomParser
{
/**
* @param $content
*
* @return DOMDocument
* @throws \FastFeed\Exception\RuntimeException
*/
protected function createDocumentFromXML($content)
{
$previousValue = libxml_use_internal_errors(true);
$document = new DOMDocument();
$document->strictErrorChecking = false;
$document->loadXML(trim($content));
libxml_use_internal_errors($previousValue);
return $document;
}
/**
* @param $content
*
* @return DOMDocument
* @throws \FastFeed\Exception\RuntimeException
*/
protected function createDocumentFromHTML($content)
{
$previousValue = libxml_use_internal_errors(true);
$document = new DOMDocument();
$document->strictErrorChecking = false;
$document->loadHTML(trim($content));
libxml_use_internal_errors($previousValue);
return $document;
}
/**
* @param DOMElement $node
* @param $tagName
*
* @return bool|string
* @throws \FastFeed\Exception\RuntimeException
*/
protected function getNodeValueByTagName(DOMElement $node, $tagName)
{
$results = $node->getElementsByTagName($tagName);
for ($i = 0; $i < $results->length; $i++) {
$result = $results->item($i);
if (!$result->nodeValue) {
continue;
}
return $result->nodeValue;
}
return false;
}
/**
* @param DOMElement $node
* @param $namespace
* @param $tagName
*
* @return bool|string
* @throws \FastFeed\Exception\RuntimeException
*/
protected function getNodeValueByTagNameNS(DOMElement $node, $namespace, $tagName)
{
$results = $node->getElementsByTagNameNS($namespace, $tagName);
for ($i = 0; $i < $results->length; $i++) {
$result = $results->item($i);
if (!$result->nodeValue) {
continue;
}
return $result->nodeValue;
}
return false;
}
/**
* @param DOMElement $node
* @param $tagName
*
* @return array
* @throws \FastFeed\Exception\RuntimeException
*/
protected function getNodeValuesByTagName(DOMElement $node, $tagName)
{
$values = array();
$results = $node->getElementsByTagName($tagName);
if ($results->length) {
foreach ($results as $result) {
if ($result->nodeValue) {
$values[] = $result->nodeValue;
}
}
}
return $values;
}
/**
* @param DOMElement $node
* @param $tagName
* @param $propertyName
*
* @return array
* @throws \FastFeed\Exception\RuntimeException
*/
protected function getNodePropertyByTagName(\DOMElement $node, $tagName, $propertyName)
{
$values = array();
$results = $node->getElementsByTagName($tagName);
if ($results->length) {
foreach ($results as $result) {
if ($result->getAttribute($propertyName)) {
$values[] = $result->getAttribute($propertyName);
}
}
}
return $values;
}
}