Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
90.00% covered (success)
90.00%
9 / 10
CRAP
96.55% covered (success)
96.55%
28 / 29
XMLAccess
0.00% covered (danger)
0.00%
0 / 1
90.00% covered (success)
90.00%
9 / 10
18
96.55% covered (success)
96.55%
28 / 29
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 getAttribute
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
 getAttributeAsBool
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getAttributeAsInt
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getXMLChildElementListByName
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
5 / 5
 getDatabaseSpecificAttributeList
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
4 / 4
 getAttributeList
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
4 / 4
 getFirstChildByName
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
4 / 4
 getFirstChildByNameContent
0.00% covered (danger)
0.00%
0 / 1
2.06
75.00% covered (success)
75.00%
3 / 4
 getTextContent
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
<?php
declare(strict_types = 1);
namespace Siesta\XML;
use Siesta\Util\StringUtil;
/**
 * @author Gregor Müller
 */
class XMLAccess
{
    /**
     * @var \DOMElement
     */
    protected $sourceElement;
    /**
     *
     */
    public function __construct(\DOMElement $sourceElement)
    {
        $this->sourceElement = $sourceElement;
    }
    /**
     * @param string $name
     *
     * @return string
     */
    public function getAttribute(string $name)
    {
        if ($this->sourceElement->hasAttribute($name)) {
            return StringUtil::trimToNull($this->sourceElement->getAttribute($name));
        }
        return null;
    }
    /**
     * @param string $name
     *
     * @return bool
     */
    public function getAttributeAsBool(string $name) : bool
    {
        return $this->getAttribute($name) === 'true';
    }
    /**
     * @param $name
     *
     * @return int
     */
    public function getAttributeAsInt(string $name)
    {
        return (int)$this->getAttribute($name);
    }
    /**
     * @param string $tagName
     *
     * @return array
     */
    public function getXMLChildElementListByName(string $tagName) : array
    {
        $result = [];
        foreach ($this->sourceElement->getElementsByTagName($tagName) as $child) {
            if ($child->nodeType === XML_ELEMENT_NODE) {
                $result[] = new XMLAccess($child);
            }
        }
        return $result;
    }
    /**
     * @param string $tagName
     *
     * @return array
     */
    public function getDatabaseSpecificAttributeList(string $tagName)
    {
        $element = $this->getFirstChildByName($tagName);
        if ($element === null) {
            return [];
        }
        return $element->getAttributeList();
    }
    /**
     * @return array
     */
    public function getAttributeList()
    {
        $attributeList = [];
        foreach ($this->sourceElement->attributes as $attribute) {
            $attributeList[$attribute->name] = StringUtil::trimToNull($attribute->value);
        }
        return $attributeList;
    }
    /**
     * @param string $tagName
     *
     * @return XMLAccess|null
     */
    public function getFirstChildByName(string $tagName)
    {
        $xmlList = $this->getXMLChildElementListByName($tagName);
        if (is_null($xmlList) || sizeof($xmlList) === 0) {
            return null;
        }
        return $xmlList[0];
    }
    /**
     * @param string $tagName
     *
     * @return null|string
     */
    public function getFirstChildByNameContent(string $tagName)
    {
        $xmlChild = $this->getFirstChildByName($tagName);
        if ($xmlChild === null) {
            return null;
        }
        return $xmlChild->getTextContent();
    }
    /**
     * @return string
     */
    public function getTextContent()
    {
        return StringUtil::trimToNull($this->sourceElement->textContent);
    }
}