Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/Api/Serializer/XmlBody.php

raw symbols SCIP
1 <?php
2 namespace Aws\Api\Serializer;
3
4 use Aws\Api\MapShape;
5 use Aws\Api\Service;
6 use Aws\Api\Shape;
7 use Aws\Api\StructureShape;
8 use Aws\Api\ListShape;
9 use Aws\Api\TimestampShape;
10 use XMLWriter;
11
12 /**
13 * @internal Formats the XML body of a REST-XML services.
14 */
15 class XmlBody
16 {
17 /** @var Service */
18 private Service $api;
19
20 /**
21 * @param Service $api API being used to create the XML body.
22 */
23 public function __construct(Service $api)
24 {
25 $this->api = $api;
26 }
27
28 /**
29 * Builds the XML body based on an array of arguments.
30 *
31 * @param Shape $shape Operation being constructed
32 * @param array $args Associative array of arguments
33 *
34 * @return string
35 */
36 public function build(Shape $shape, array $args)
37 {
38 $xml = new XMLWriter();
39 $xml->openMemory();
40 $xml->startDocument('1.0', 'UTF-8');
41
42 $rootElementName = $this->determineRootElementName($shape);
43
44 $this->format($shape, $rootElementName, $args, $xml);
45 $xml->endDocument();
46
47 return $xml->outputMemory();
48 }
49
50 private function startElement(Shape $shape, $name, XMLWriter $xml)
51 {
52 $xml->startElement($name);
53
54 if ($ns = $shape['xmlNamespace']) {
55 $xml->writeAttribute(
56 isset($ns['prefix']) ? "xmlns:{$ns['prefix']}" : 'xmlns',
57 $ns['uri']
58 );
59 }
60 }
61
62 private function format(Shape $shape, $name, $value, XMLWriter $xml)
63 {
64 // Any method mentioned here has a custom serialization handler.
65 static $methods = [
66 'add_structure' => true,
67 'add_list' => true,
68 'add_blob' => true,
69 'add_timestamp' => true,
70 'add_boolean' => true,
71 'add_map' => true,
72 'add_string' => true
73 ];
74
75 $type = 'add_' . $shape['type'];
76 if (isset($methods[$type])) {
77 $this->{$type}($shape, $name, $value, $xml);
78 } else {
79 $this->defaultShape($shape, $name, $value, $xml);
80 }
81 }
82
83 private function defaultShape(Shape $shape, $name, $value, XMLWriter $xml)
84 {
85 $this->startElement($shape, $name, $xml);
86 $xml->text($value);
87 $xml->endElement();
88 }
89
90 private function add_structure(
91 StructureShape $shape,
92 $name,
93 array $value,
94 \XMLWriter $xml
95 ) {
96 $this->startElement($shape, $name, $xml);
97
98 foreach ($this->getStructureMembers($shape, $value) as $k => $definition) {
99 // Default to member name
100 $elementName = $k;
101
102 if ($definition['member']['locationName']
103 && !isset($definition['member']['locationNameAtStructureLevel'])) {
104 $elementName = $definition['member']['locationName'];
105 }
106
107 $this->format(
108 $definition['member'],
109 $elementName,
110 $definition['value'],
111 $xml
112 );
113 }
114
115 $xml->endElement();
116 }
117
118 private function getStructureMembers(StructureShape $shape, array $value)
119 {
120 $members = [];
121
122 foreach ($value as $k => $v) {
123 if ($v !== null && $shape->hasMember($k)) {
124 $definition = [
125 'member' => $shape->getMember($k),
126 'value' => $v,
127 ];
128
129 if ($definition['member']['xmlAttribute']) {
130 // array_unshift_associative
131 $members = [$k => $definition] + $members;
132 } else {
133 $members[$k] = $definition;
134 }
135 }
136 }
137
138 return $members;
139 }
140
141 private function add_list(
142 ListShape $shape,
143 $name,
144 array $value,
145 XMLWriter $xml
146 ) {
147 $items = $shape->getMember();
148
149 if ($shape['flattened']) {
150 $elementName = $name;
151 } else {
152 $this->startElement($shape, $name, $xml);
153 $elementName = $items['locationName'] ?: 'member';
154 }
155
156 foreach ($value as $v) {
157 $this->format($items, $elementName, $v, $xml);
158 }
159
160 if (!$shape['flattened']) {
161 $xml->endElement();
162 }
163 }
164
165 private function add_map(
166 MapShape $shape,
167 $name,
168 array $value,
169 XMLWriter $xml
170 ) {
171 $xmlEntry = $shape['flattened'] ? $name : 'entry';
172 $xmlKey = $shape->getKey()['locationName'] ?: 'key';
173 $xmlValue = $shape->getValue()['locationName'] ?: 'value';
174
175 if (!$shape['flattened']) {
176 $this->startElement($shape, $name, $xml);
177 }
178
179 foreach ($value as $key => $v) {
180 $this->startElement($shape, $xmlEntry, $xml);
181 $this->format($shape->getKey(), $xmlKey, $key, $xml);
182 $this->format($shape->getValue(), $xmlValue, $v, $xml);
183 $xml->endElement();
184 }
185
186 if (!$shape['flattened']) {
187 $xml->endElement();
188 }
189 }
190
191 private function add_blob(Shape $shape, $name, $value, XMLWriter $xml)
192 {
193 $this->startElement($shape, $name, $xml);
194 $xml->writeRaw(base64_encode($value));
195 $xml->endElement();
196 }
197
198 private function add_timestamp(
199 TimestampShape $shape,
200 $name,
201 $value,
202 XMLWriter $xml
203 ) {
204 $this->startElement($shape, $name, $xml);
205 $timestampFormat = !empty($shape['timestampFormat'])
206 ? $shape['timestampFormat']
207 : 'iso8601';
208 $xml->writeRaw(TimestampShape::format($value, $timestampFormat));
209 $xml->endElement();
210 }
211
212 private function add_boolean(
213 Shape $shape,
214 $name,
215 $value,
216 XMLWriter $xml
217 ) {
218 $this->startElement($shape, $name, $xml);
219 $xml->writeRaw($value ? 'true' : 'false');
220 $xml->endElement();
221 }
222
223 private function add_string(
224 Shape $shape,
225 $name,
226 $value,
227 XMLWriter $xml
228 ) {
229 if ($shape['xmlAttribute']) {
230 $xml->writeAttribute($shape['locationName'] ?: $name, $value);
231 } else {
232 $this->defaultShape($shape, $name, $value, $xml);
233 }
234 }
235
236 private function determineRootElementName(Shape $shape): string
237 {
238 $shapeName = $shape->getName();
239
240 // Look up the shape definition first
241 if ($shapeName && $shapeMap = $shape->getShapeMap()) {
242 if (isset($shapeMap[$shapeName]['locationName'])) {
243 return $shapeMap[$shapeName]['locationName'];
244 }
245 }
246
247 // Fall back to shape's current locationName
248 if ($shape['locationName']) {
249 return $shape['locationName'];
250 }
251
252 return $shapeName;
253 }
254 }