/**
  * Appends (copies) a child $source and all its descendants to $this node
  * @since 1.2.4
  *
  * @param CBSimpleXMLElement  $source
  * @param callback            $callBack to check/transform data or attributes of a node: $destinationData = function ( string|array $sourceData, CBSimpleXMLElement $sourceNode, CBSimpleXMLElement $destinationParentNode );
  */
 function &addChildWithDescendants(&$source, $callBack = null)
 {
     if ($callBack === null) {
         $child = $this->addChildWithAttr($source->name(), $source->data(), null, $source->attributes());
     } else {
         $child = $this->addChildWithAttr($source->name(), call_user_func_array($callBack, array($source->data(), $source, $this)), null, call_user_func_array($callBack, array($source->attributes(), $source, $this)));
     }
     foreach ($source->children() as $sourceChild) {
         $child->addChildWithDescendants($sourceChild, $callBack);
     }
     return $child;
 }
Example #2
0
	/**
	 * Protected internal function that handles missing DOM functionality:
	 * $callBack to the copied attributes and data, as well as copying of children.
	 * @since 1.2.4
	 * @protected
	 *
	 * @param  DOMElement          $newNode         new DOM (incomplete) node just inserted/replaced
	 * @param  CBSimpleXMLElement  $xmlSourceNode   original XML node that got copied into DOM
	 * @param  callback            $callBack to check/transform data or attributes of a node: $destinationData = function ( string|array $sourceData, CBSimpleXMLElement $sourceNode, CBSimpleXMLElement $destinationParentNode );
	 * @return CBSimpleXMLElement  New XML node
	 */
	function & _domCopyChildrenCallbackonNode( &$newNode, $xmlSourceNode, $callBack ) {
		$newNodeXML				=&	simplexml_import_dom( $newNode, get_class( $this ) );
		if ( $callBack === null ) {
			$newNode->nodeValue	=	$xmlSourceNode->data();
			$attributesToCopy	=	$xmlSourceNode->attributes();
			foreach ( $attributesToCopy as $k => $v ) {
				// PHP 5.2 doesn't copy attributes however PHP 5.3 does it, so let's copy again for PHP 5.2:	//TBD later check from which version this 5.2 bug has been fixed...
				$newNode->setAttribute( $k, $attributesToCopy[$k] );
			}
		} else {
			$newNode->nodeValue	=	call_user_func_array( $callBack, array( $xmlSourceNode->data(), $xmlSourceNode, $newNodeXML ) );
			$copiedAttributes	=	$newNode->attributes;
			foreach ( $copiedAttributes as $k => $v ) {
				$newNode->removeAttribute( $k );
			}
			// the new set of $attributes can be different from old one, thus we needed to remove old set (copied in PHP 5.3 only) first, then copy new:
			$attributes			=	call_user_func_array( $callBack, array( $xmlSourceNode->attributes(), $xmlSourceNode, $newNodeXML ) );
			foreach ( $attributes as $k => $v ) {
				$newNode->setAttribute( $k, $v );
			}
		}
		foreach ($xmlSourceNode->children() as $child ) {
			$newNodeXML->addChildWithDescendants( $child, $callBack );
		}
		return $newNodeXML;
	}