/includes/lib/Braintree/lib/Braintree/Xml/Parser.php

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