/** * Overrides the Exception default constructor to automatically add informations about the concerned node (path and line number) * * @param string $message * @param type $code * @param type $previous * @param DOMNode $node [Optionnal] DOMNode causing the DOMFormatException */ public function __construct($message, $code, $previous, DOMNode $node = null) { if ($node !== null) { $message .= ' (' . MFDocument::GetItopNodePath($node) . ' at line ' . $node->getLineNo() . ')'; } parent::__construct($message, $code, $previous); }
/** * Merge the current node into the given container * * @param DOMNode $oContainer An element or a document * @param string $sSearchId The id to consider (could be blank) * @param bool $bMustExist Throw an exception if the node must already be found (and not marked as deleted!) */ public function MergeInto($oContainer, $sSearchId, $bMustExist) { $oTargetNode = $oContainer->_FindChildNode($this, $sSearchId); if ($oTargetNode) { if ($oTargetNode->getAttribute('_alteration') == 'removed') { if ($bMustExist) { throw new Exception(MFDocument::GetItopNodePath($this) . ' at line ' . $this->getLineNo() . ": could not be found (marked as deleted)"); } // Beware: ImportNode(xxx, false) DOES NOT copy the node's attribute on *some* PHP versions (<5.2.17) // So use this workaround to import a node and its attributes on *any* PHP version $oTargetNode = $oContainer->ownerDocument->ImportNode($this->cloneNode(false), true); $oContainer->AddChildNode($oTargetNode); } } else { if ($bMustExist) { echo "Dumping parent node<br/>\n"; $oContainer->Dump(); throw new Exception(MFDocument::GetItopNodePath($this) . ' at line ' . $this->getLineNo() . ": could not be found"); } // Beware: ImportNode(xxx, false) DOES NOT copy the node's attribute on *some* PHP versions (<5.2.17) // So use this workaround to import a node and its attributes on *any* PHP version $oTargetNode = $oContainer->ownerDocument->ImportNode($this->cloneNode(false), true); $oContainer->AddChildNode($oTargetNode); } return $oTargetNode; }
/** * Assumes the current node to be either a text or * <items> * <item [key]="..."]>value<item> * <item [key]="..."]>value<item> * </items> * where value can be the either a text or an array of items... recursively * Returns a PHP array */ public function GetNodeAsArrayOfItems($sElementName = 'items') { $oItems = $this->GetOptionalElement($sElementName); if ($oItems) { $res = array(); $aRanks = array(); foreach ($oItems->childNodes as $oItem) { if ($oItem instanceof DOMElement) { // When an attribute is missing if ($oItem->hasAttribute('id')) { $key = $oItem->getAttribute('id'); if (array_key_exists($key, $res)) { // Houston! throw new DOMFormatException("Tag " . MFDocument::GetItopNodePath($oItem) . ", id '{$key}' already used!!!"); } $res[$key] = $oItem->GetNodeAsArrayOfItems(); } else { $res[] = $oItem->GetNodeAsArrayOfItems(); } $sRank = $oItem->GetChildText('rank'); if ($sRank != '') { $aRanks[] = (double) $sRank; } else { $aRanks[] = count($aRanks) > 0 ? max($aRanks) + 1 : 0; } array_multisort($aRanks, $res); } } } else { $res = $this->GetText(); } return $res; }
/** * For debugging purposes */ public function Dump($bReturnRes = false) { $oMFDoc = new MFDocument(); $oClone = $oMFDoc->importNode($this->cloneNode(true), true); $oMFDoc->appendChild($oClone); $sXml = $oMFDoc->saveXML($oClone); if ($bReturnRes) { return $sXml; } else { echo "<pre>\n"; echo htmlentities($sXml); echo "</pre>\n"; } }