XmlParser.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace Aws\Api\Parser;
  3. use Aws\Api\DateTimeResult;
  4. use Aws\Api\ListShape;
  5. use Aws\Api\MapShape;
  6. use Aws\Api\Parser\Exception\ParserException;
  7. use Aws\Api\Shape;
  8. use Aws\Api\StructureShape;
  9. /**
  10. * @internal Implements standard XML parsing for REST-XML and Query protocols.
  11. */
  12. class XmlParser
  13. {
  14. public function parse(StructureShape $shape, \SimpleXMLElement $value)
  15. {
  16. return $this->dispatch($shape, $value);
  17. }
  18. private function dispatch($shape, \SimpleXMLElement $value)
  19. {
  20. static $methods = [
  21. 'structure' => 'parse_structure',
  22. 'list' => 'parse_list',
  23. 'map' => 'parse_map',
  24. 'blob' => 'parse_blob',
  25. 'boolean' => 'parse_boolean',
  26. 'integer' => 'parse_integer',
  27. 'float' => 'parse_float',
  28. 'double' => 'parse_float',
  29. 'timestamp' => 'parse_timestamp',
  30. ];
  31. $type = $shape['type'];
  32. if (isset($methods[$type])) {
  33. return $this->{$methods[$type]}($shape, $value);
  34. }
  35. return (string) $value;
  36. }
  37. private function parse_structure(
  38. StructureShape $shape,
  39. \SimpleXMLElement $value
  40. ) {
  41. $target = [];
  42. foreach ($shape->getMembers() as $name => $member) {
  43. // Extract the name of the XML node
  44. $node = $this->memberKey($member, $name);
  45. if (isset($value->{$node})) {
  46. $target[$name] = $this->dispatch($member, $value->{$node});
  47. } else {
  48. $memberShape = $shape->getMember($name);
  49. if (!empty($memberShape['xmlAttribute'])) {
  50. $target[$name] = $this->parse_xml_attribute(
  51. $shape,
  52. $memberShape,
  53. $value
  54. );
  55. }
  56. }
  57. }
  58. if (isset($shape['union'])
  59. && $shape['union']
  60. && empty($target)
  61. ) {
  62. foreach ($value as $key => $val) {
  63. $name = $val->children()->getName();
  64. $target['Unknown'][$name] = $val->$name;
  65. }
  66. }
  67. return $target;
  68. }
  69. private function memberKey(Shape $shape, $name)
  70. {
  71. if (null !== $shape['locationName']) {
  72. return $shape['locationName'];
  73. }
  74. if ($shape instanceof ListShape && $shape['flattened']) {
  75. return $shape->getMember()['locationName'] ?: $name;
  76. }
  77. return $name;
  78. }
  79. private function parse_list(ListShape $shape, \SimpleXMLElement $value)
  80. {
  81. $target = [];
  82. $member = $shape->getMember();
  83. if (!$shape['flattened']) {
  84. $value = $value->{$member['locationName'] ?: 'member'};
  85. }
  86. foreach ($value as $v) {
  87. $target[] = $this->dispatch($member, $v);
  88. }
  89. return $target;
  90. }
  91. private function parse_map(MapShape $shape, \SimpleXMLElement $value)
  92. {
  93. $target = [];
  94. if (!$shape['flattened']) {
  95. $value = $value->entry;
  96. }
  97. $mapKey = $shape->getKey();
  98. $mapValue = $shape->getValue();
  99. $keyName = $shape->getKey()['locationName'] ?: 'key';
  100. $valueName = $shape->getValue()['locationName'] ?: 'value';
  101. foreach ($value as $node) {
  102. $key = $this->dispatch($mapKey, $node->{$keyName});
  103. $value = $this->dispatch($mapValue, $node->{$valueName});
  104. $target[$key] = $value;
  105. }
  106. return $target;
  107. }
  108. private function parse_blob(Shape $shape, $value)
  109. {
  110. return base64_decode((string) $value);
  111. }
  112. private function parse_float(Shape $shape, $value)
  113. {
  114. return (float) (string) $value;
  115. }
  116. private function parse_integer(Shape $shape, $value)
  117. {
  118. return (int) (string) $value;
  119. }
  120. private function parse_boolean(Shape $shape, $value)
  121. {
  122. return $value == 'true';
  123. }
  124. private function parse_timestamp(Shape $shape, $value)
  125. {
  126. if (is_string($value)
  127. || is_int($value)
  128. || (is_object($value)
  129. && method_exists($value, '__toString'))
  130. ) {
  131. return DateTimeResult::fromTimestamp(
  132. (string) $value,
  133. !empty($shape['timestampFormat']) ? $shape['timestampFormat'] : null
  134. );
  135. }
  136. throw new ParserException('Invalid timestamp value passed to XmlParser::parse_timestamp');
  137. }
  138. private function parse_xml_attribute(Shape $shape, Shape $memberShape, $value)
  139. {
  140. $namespace = $shape['xmlNamespace']['uri']
  141. ? $shape['xmlNamespace']['uri']
  142. : '';
  143. $prefix = $shape['xmlNamespace']['prefix']
  144. ? $shape['xmlNamespace']['prefix']
  145. : '';
  146. if (!empty($prefix)) {
  147. $prefix .= ':';
  148. }
  149. $key = str_replace($prefix, '', $memberShape['locationName']);
  150. $attributes = $value->attributes($namespace);
  151. return isset($attributes[$key]) ? (string) $attributes[$key] : null;
  152. }
  153. }