| 
<?php
namespace lib\Helper;
 
 /**
 * extends DOMDocumenta with few little helpers to make
 * XML access/creation a little easier
 *
 *  used by:
 *  lib\Helper\PHPInfoParser
 *  lib\StatesInfo
 *  lib\OSMap
 *
 * @package lib\Helper
 * @author Stefanius <[email protected]>
 */
 class ExtDOMDocument extends \DOMDocument
 {
 public function __construct() {
 parent::__construct('1.0', 'utf-8');
 }
 
 /**
 * select nodes specified by XPath
 * @param DOMDocument $this
 * @param DOMElement $oParent
 * @param string $strPath
 * @return DOMNodeList
 */
 public function selectNodes($strPath, \DOMElement $oParent=null)
 {
 $oXPath = new \DOMXPath($this);
 $oNodelist = $oXPath->query($strPath, $oParent);
 if ($oNodelist === false) {
 // PHP generates warning but with no further information about the query - so give little bit more info
 trigger_error('DOMXPath->query: malformed expression or the invalid contextnode (' . $strPath . ')', E_USER_NOTICE);
 }
 return $oNodelist;
 }
 
 /**
 * select first node specified by XPath
 * @param DOMDocument $this
 * @param DOMElement $oParent
 * @param string $strNode
 * @return DOMNode or null if node not found
 */
 public function selectSingleNode($strNode, \DOMElement $oParent=null) {
 $oNode = null;
 $oNodelist = $this->selectNodes($strNode, $oParent);
 if ($oNodelist !== false && $oNodelist->length > 0) {
 $oNode = $oNodelist->item(0);
 }
 return $oNode;
 }
 
 /**
 * @param DOMDocument $this
 * @param DOMElement $oParent
 * @param string $strNode
 * @return string or null if node not found
 */
 public function getNodeValue($strNode, \DOMElement $oParent=null) {
 $strValue = null;
 $oNode = $this->selectSingleNode($strNode, $oParent);
 if ($oNode != null)    {
 $strValue = $oNode->nodeValue;
 }
 return $strValue;
 }
 
 /**
 * get the name of the root element.
 * @return string or null, if no root found
 */
 public function getRootName() {
 $strName = null;
 if ($this->documentElement) {
 $strName = $this->documentElement->nodeName;
 }
 return $strName;
 }
 
 /**
 * create new DOMElement and append it to given parent
 * @param DOMDocument $this
 * @param DOMElement $oParent
 * @param string $strName
 * @param string $strValue
 * @return DOMElement
 */
 public function addChild($strName, $strValue=null, \DOMElement $oParent=null) {
 $oChild = $this->createElement($strName, $strValue);
 $oParent ? $oParent->appendChild($oChild) : $this->appendChild($oChild);
 
 return $oChild;
 }
 }
 
 |