Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/Api/Parser/XmlParser.php

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