XmlParser.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. return $target;
  59. }
  60. private function memberKey(Shape $shape, $name)
  61. {
  62. if (null !== $shape['locationName']) {
  63. return $shape['locationName'];
  64. }
  65. if ($shape instanceof ListShape && $shape['flattened']) {
  66. return $shape->getMember()['locationName'] ?: $name;
  67. }
  68. return $name;
  69. }
  70. private function parse_list(ListShape $shape, \SimpleXMLElement $value)
  71. {
  72. $target = [];
  73. $member = $shape->getMember();
  74. if (!$shape['flattened']) {
  75. $value = $value->{$member['locationName'] ?: 'member'};
  76. }
  77. foreach ($value as $v) {
  78. $target[] = $this->dispatch($member, $v);
  79. }
  80. return $target;
  81. }
  82. private function parse_map(MapShape $shape, \SimpleXMLElement $value)
  83. {
  84. $target = [];
  85. if (!$shape['flattened']) {
  86. $value = $value->entry;
  87. }
  88. $mapKey = $shape->getKey();
  89. $mapValue = $shape->getValue();
  90. $keyName = $shape->getKey()['locationName'] ?: 'key';
  91. $valueName = $shape->getValue()['locationName'] ?: 'value';
  92. foreach ($value as $node) {
  93. $key = $this->dispatch($mapKey, $node->{$keyName});
  94. $value = $this->dispatch($mapValue, $node->{$valueName});
  95. $target[$key] = $value;
  96. }
  97. return $target;
  98. }
  99. private function parse_blob(Shape $shape, $value)
  100. {
  101. return base64_decode((string) $value);
  102. }
  103. private function parse_float(Shape $shape, $value)
  104. {
  105. return (float) (string) $value;
  106. }
  107. private function parse_integer(Shape $shape, $value)
  108. {
  109. return (int) (string) $value;
  110. }
  111. private function parse_boolean(Shape $shape, $value)
  112. {
  113. return $value == 'true';
  114. }
  115. private function parse_timestamp(Shape $shape, $value)
  116. {
  117. if (is_string($value)
  118. || is_int($value)
  119. || (is_object($value)
  120. && method_exists($value, '__toString'))
  121. ) {
  122. return DateTimeResult::fromTimestamp(
  123. (string) $value,
  124. !empty($shape['timestampFormat']) ? $shape['timestampFormat'] : null
  125. );
  126. }
  127. throw new ParserException('Invalid timestamp value passed to XmlParser::parse_timestamp');
  128. }
  129. private function parse_xml_attribute(Shape $shape, Shape $memberShape, $value)
  130. {
  131. $namespace = $shape['xmlNamespace']['uri']
  132. ? $shape['xmlNamespace']['uri']
  133. : '';
  134. $prefix = $shape['xmlNamespace']['prefix']
  135. ? $shape['xmlNamespace']['prefix']
  136. : '';
  137. if (!empty($prefix)) {
  138. $prefix .= ':';
  139. }
  140. $key = str_replace($prefix, '', $memberShape['locationName']);
  141. $attributes = $value->attributes($namespace);
  142. return isset($attributes[$key]) ? (string) $attributes[$key] : null;
  143. }
  144. }