BraintreeXmlGenerator

Generates XML output from arrays using PHP's built-in XMLWriter.

Defined (1)

The class is defined in the following location(s).

/includes/lib/Braintree/lib/Braintree/Xml/Generator.php  
  1. class Generator 
  2. /** 
  3. * arrays passed to this method should have a single root element 
  4. * with an array as its value 
  5. * @param array $aData the array of data 
  6. * @return string XML string 
  7. */ 
  8. public static function arrayToXml($aData) 
  9. $aData = Util::camelCaseToDelimiterArray($aData, '-'); 
  10. // set up the XMLWriter 
  11. $writer = new XMLWriter(); 
  12. $writer->openMemory(); 
  13.  
  14. $writer->setIndent(true); 
  15. $writer->setIndentString(' '); 
  16. $writer->startDocument('1.0', 'UTF-8'); 
  17.  
  18. // get the root element name 
  19. $aKeys = array_keys($aData); 
  20. $rootElementName = $aKeys[0]; 
  21. // open the root element 
  22. $writer->startElement($rootElementName); 
  23. // create the body 
  24. self::_createElementsFromArray($writer, $aData[$rootElementName], $rootElementName); 
  25.  
  26. // close the root element and document 
  27. $writer->endElement(); 
  28. $writer->endDocument(); 
  29.  
  30. // send the output as string 
  31. return $writer->outputMemory(); 
  32.  
  33. /** 
  34. * Construct XML elements with attributes from an associative array. 
  35. * @access protected 
  36. * @static 
  37. * @param object $writer XMLWriter object 
  38. * @param array $aData contains attributes and values 
  39. * @return void 
  40. */ 
  41. private static function _createElementsFromArray(&$writer, $aData) 
  42. if (!is_array($aData)) { 
  43. if (is_bool($aData)) { 
  44. $writer->text($aData ? 'true' : 'false'); 
  45. } else { 
  46. $writer->text($aData); 
  47. return; 
  48. foreach ($aData AS $elementName => $element) { 
  49. // handle child elements 
  50. $writer->startElement($elementName); 
  51. if (is_array($element)) { 
  52. if (array_key_exists(0, $element) || empty($element)) { 
  53. $writer->writeAttribute('type', 'array'); 
  54. foreach ($element AS $ignored => $itemInArray) { 
  55. $writer->startElement('item'); 
  56. self::_createElementsFromArray($writer, $itemInArray); 
  57. $writer->endElement(); 
  58. else { 
  59. self::_createElementsFromArray($writer, $element); 
  60. } else { 
  61. // generate attributes as needed 
  62. $attribute = self::_generateXmlAttribute($element); 
  63. if (is_array($attribute)) { 
  64. $writer->writeAttribute($attribute[0], $attribute[1]); 
  65. $element = $attribute[2]; 
  66. $writer->text($element); 
  67. $writer->endElement(); 
  68.  
  69. /** 
  70. * convert passed data into an array of attributeType, attributeName, and value 
  71. * dates sent as DateTime objects will be converted to strings 
  72. * @access protected 
  73. * @param mixed $value 
  74. * @return array attributes and element value 
  75. */ 
  76. private static function _generateXmlAttribute($value) 
  77. if ($value instanceof DateTime) { 
  78. return ['type', 'datetime', self::_dateTimeToXmlTimestamp($value)]; 
  79. if (is_int($value)) { 
  80. return ['type', 'integer', $value]; 
  81. if (is_bool($value)) { 
  82. return ['type', 'boolean', ($value ? 'true' : 'false')]; 
  83. if ($value === NULL) { 
  84. return ['nil', 'true', $value]; 
  85. /** 
  86. * converts datetime back to xml schema format 
  87. * @access protected 
  88. * @param object $dateTime 
  89. * @return string XML schema formatted timestamp 
  90. */ 
  91. private static function _dateTimeToXmlTimestamp($dateTime) 
  92. $dateTimeForUTC = clone $dateTime; 
  93.  
  94. $dateTimeForUTC->setTimeZone(new DateTimeZone('UTC')); 
  95. return ($dateTimeForUTC->format('Y-m-d\TH:i:s') . 'Z'); 
  96.  
  97. private static function _castDateTime($string) 
  98. try { 
  99. if (empty($string)) { 
  100. return false; 
  101. $dateTime = new DateTime($string); 
  102. return self::_dateTimeToXmlTimestamp($dateTime); 
  103. } catch (Exception $e) { 
  104. // not a datetime 
  105. return false;