1<?php 2 3namespace FINDOLOGIC\Export\Helpers; 4 5class XMLHelper 6{ 7 /** 8 * Shortcut for creating an XML element. 9 * 10 * @param \DOMDocument $document The document used to create the element. It will NOT be modified. 11 * @param string $name Name of the element. 12 * @param array $attributes String-to-string mapping of attributes to set on the element. 13 * @return \DOMElement The newly constructed independent DOM element. 14 */ 15 public static function createElement(\DOMDocument $document, $name, array $attributes = []) 16 { 17 $element = $document->createElement($name); 18 19 foreach ($attributes as $attribName => $attribValue) { 20 $element->setAttribute($attribName, $attribValue); 21 } 22 23 return $element; 24 } 25 26 /** 27 * Shortcut for creating an XML element that contains CDATA-wrapped text. 28 * 29 * @param \DOMDocument $document The document used to create the element. It will NOT be modified. 30 * @param string $name Name of the element. 31 * @param string $text The text body of the document. 32 * @param array $attributes String-to-string mapping of attributes to set on the element. 33 * @return \DOMElement The newly constructed independent DOM element. 34 */ 35 public static function createElementWithText(\DOMDocument $document, $name, $text, array $attributes = []) 36 { 37 $element = self::createElement($document, $name, $attributes); 38 $wrappedText = $document->createCDATASection($text); 39 $element->appendChild($wrappedText); 40 41 return $element; 42 } 43} 44