BraintreeXmlParser

Braintree XML Parser.

Defined (1)

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

/includes/lib/Braintree/Braintree/Xml/Parser.php  
  1. class Braintree_Xml_Parser 
  2.  
  3. private static $_xmlRoot; 
  4. private static $_responseType; 
  5.  
  6. /** 
  7. * sets up the SimpleXMLIterator and starts the parsing 
  8. * @access public 
  9. * @param string $xml 
  10. * @return array array mapped to the passed xml 
  11. */ 
  12. public static function arrayFromXml($xml) 
  13. // SimpleXML provides the root information on construct 
  14. $iterator = new SimpleXMLIterator($xml); 
  15. $xmlRoot = Braintree_Util::delimiterToCamelCase($iterator->getName()); 
  16. $type = $iterator->attributes()->type; 
  17.  
  18. self::$_xmlRoot = $iterator->getName(); 
  19. self::$_responseType = $type; 
  20.  
  21. // return the mapped array with the root element as the header 
  22. return array($xmlRoot => self::_iteratorToArray($iterator)); 
  23.  
  24.  
  25. /** 
  26. * processes SimpleXMLIterator objects recursively 
  27. * @access protected 
  28. * @param object $iterator 
  29. * @return array xml converted to array 
  30. */ 
  31. private static function _iteratorToArray($iterator) 
  32. $xmlArray = array(); 
  33. $value = null; 
  34.  
  35. // rewind the iterator and check if the position is valid 
  36. // if not, return the string it contains 
  37. $iterator->rewind(); 
  38. if (!$iterator->valid()) { 
  39. return self::_typecastXmlValue($iterator); 
  40. for ($iterator->rewind(); $iterator->valid(); $iterator->next()) { 
  41.  
  42. $tmpArray = null; 
  43. $value = null; 
  44.  
  45. // get the attribute type string for use in conditions below 
  46. $attributeType = $iterator->attributes()->type; 
  47.  
  48. // extract the parent element via xpath query 
  49. $parentElement = $iterator->xpath($iterator->key() . '/..'); 
  50. if ($parentElement[0] instanceof SimpleXMLIterator) { 
  51. $parentElement = $parentElement[0]; 
  52. $parentKey = Braintree_Util::delimiterToCamelCase($parentElement->getName()); 
  53. } else { 
  54. $parentElement = null; 
  55.  
  56.  
  57. if ($parentKey == "customFields") { 
  58. $key = Braintree_Util::delimiterToUnderscore($iterator->key()); 
  59. } else { 
  60. $key = Braintree_Util::delimiterToCamelCase($iterator->key()); 
  61.  
  62. // process children recursively 
  63. if ($iterator->hasChildren()) { 
  64. // return the child elements 
  65. $value = self::_iteratorToArray($iterator->current()); 
  66.  
  67. // if the element is an array type,  
  68. // use numeric keys to allow multiple values 
  69. if ($attributeType != 'array') { 
  70. $tmpArray[$key] = $value; 
  71. } else { 
  72. // cast values according to attributes 
  73. $tmpArray[$key] = self::_typecastXmlValue($iterator->current()); 
  74.  
  75. // set the output string 
  76. $output = isset($value) ? $value : $tmpArray[$key]; 
  77.  
  78. // determine if there are multiple tags of this name at the same level 
  79. if (isset($parentElement) && 
  80. ($parentElement->attributes()->type == 'collection') && 
  81. $iterator->hasChildren()) { 
  82. $xmlArray[$key][] = $output; 
  83. continue; 
  84.  
  85. // if the element was an array type, output to a numbered key 
  86. // otherwise, use the element name 
  87. if ($attributeType == 'array') { 
  88. $xmlArray[] = $output; 
  89. } else { 
  90. $xmlArray[$key] = $output; 
  91.  
  92. return $xmlArray; 
  93.  
  94. /** 
  95. * typecast xml value based on attributes 
  96. * @param object $valueObj SimpleXMLElement 
  97. * @return mixed value for placing into array 
  98. */ 
  99. private static function _typecastXmlValue($valueObj) 
  100. // get the element attributes 
  101. $attribs = $valueObj->attributes(); 
  102. // the element is null, so jump out here 
  103. if (isset($attribs->nil) && $attribs->nil) { 
  104. return null; 
  105. // switch on the type attribute 
  106. // switch works even if $attribs->type isn't set 
  107. switch ($attribs->type) { 
  108. case 'datetime': 
  109. return self::_timestampToUTC((string) $valueObj); 
  110. break; 
  111. case 'date': 
  112. return new DateTime((string)$valueObj); 
  113. break; 
  114. case 'integer': 
  115. return (int) $valueObj; 
  116. break; 
  117. case 'boolean': 
  118. $value = (string) $valueObj; 
  119. // look for a number inside the string 
  120. if(is_numeric($value)) { 
  121. return (bool) $value; 
  122. } else { 
  123. // look for the string "true", return false in all other cases 
  124. return ($value != "true") ? FALSE : TRUE; 
  125. break; 
  126. case 'array': 
  127. return array(); 
  128. default: 
  129. return (string) $valueObj; 
  130.  
  131.  
  132. /** 
  133. * convert xml timestamps into DateTime 
  134. * @param string $timestamp 
  135. * @return string UTC formatted datetime string 
  136. */ 
  137. private static function _timestampToUTC($timestamp) 
  138. $tz = new DateTimeZone('UTC'); 
  139. // strangely DateTime requires an explicit set below 
  140. // to show the proper time zone 
  141. $dateTime = new DateTime($timestamp, $tz); 
  142. $dateTime->setTimezone($tz); 
  143. return $dateTime;