BraintreeXmlParser

Braintree XML Parser.

Defined (1)

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

/includes/lib/Braintree/lib/Braintree/Xml/Parser.php  
  1. class Parser 
  2. /** 
  3. * Converts an XML string into a multidimensional array 
  4. * @param string $xml 
  5. * @return array 
  6. */ 
  7. public static function arrayFromXml($xml) 
  8. $document = new DOMDocument('1.0', 'UTF-8'); 
  9. $document->loadXML($xml); 
  10.  
  11. $root = $document->documentElement->nodeName; 
  12.  
  13. return Util::delimiterToCamelCaseArray([ 
  14. $root => self::_nodeToValue($document->childNodes->item(0)),  
  15. ]); 
  16.  
  17. /** 
  18. * Converts a node to an array of values or nodes 
  19. * @param DOMNode @node 
  20. * @return mixed 
  21. */ 
  22. private static function _nodeToArray($node) 
  23. $type = null; 
  24. if ($node instanceof DOMElement) { 
  25. $type = $node->getAttribute('type'); 
  26.  
  27. switch($type) { 
  28. case 'array': 
  29. $array = []; 
  30. foreach ($node->childNodes as $child) { 
  31. $value = self::_nodeToValue($child); 
  32. if ($value !== null) { 
  33. $array[] = $value; 
  34. return $array; 
  35. case 'collection': 
  36. $collection = []; 
  37. foreach ($node->childNodes as $child) { 
  38. $value = self::_nodetoValue($child); 
  39. if ($value !== null) { 
  40. if (!isset($collection[$child->nodeName])) { 
  41. $collection[$child->nodeName] = []; 
  42. $collection[$child->nodeName][] = self::_nodeToValue($child); 
  43. return $collection; 
  44. default: 
  45. $values = []; 
  46. if ($node->childNodes->length === 1 && $node->childNodes->item(0) instanceof DOMText) { 
  47. return $node->childNodes->item(0)->nodeValue; 
  48. } else { 
  49. foreach ($node->childNodes as $child) { 
  50. if (!$child instanceof DOMText) { 
  51. $values[$child->nodeName] = self::_nodeToValue($child); 
  52. return $values; 
  53.  
  54. /** 
  55. * Converts a node to a PHP value 
  56. * @param DOMNode $node 
  57. * @return mixed 
  58. */ 
  59. private static function _nodeToValue($node) 
  60. $type = null; 
  61. if ($node instanceof DOMElement) { 
  62. $type = $node->getAttribute('type'); 
  63.  
  64. switch($type) { 
  65. case 'datetime': 
  66. return self::_timestampToUTC((string) $node->nodeValue); 
  67. case 'date': 
  68. return new DateTime((string) $node->nodeValue); 
  69. case 'integer': 
  70. return (int) $node->nodeValue; 
  71. case 'boolean': 
  72. $value = (string) $node->nodeValue; 
  73. if(is_numeric($value)) { 
  74. return (bool) $value; 
  75. } else { 
  76. return ($value !== "true") ? false : true; 
  77. case 'array': 
  78. case 'collection': 
  79. return self::_nodeToArray($node); 
  80. default: 
  81. if ($node->hasChildNodes()) { 
  82. return self::_nodeToArray($node); 
  83. } elseif (trim($node->nodeValue) === '') { 
  84. return null; 
  85. } else { 
  86. return $node->nodeValue; 
  87.  
  88.  
  89. /** 
  90. * Converts XML timestamps into DateTime instances 
  91. * @param string $timestamp 
  92. * @return DateTime 
  93. */ 
  94. private static function _timestampToUTC($timestamp) 
  95. $tz = new DateTimeZone('UTC'); 
  96. $dateTime = new DateTime($timestamp, $tz); 
  97. $dateTime->setTimezone($tz); 
  98. return $dateTime;