PluginProbe
Media Cloud Sync / 1.4.2
Media Cloud Sync v1.4.2
1.4.2 1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 All 36 releases
media-cloud-sync / includes / sdk / s3 / Aws / Api / Serializer / XmlBody.php

XmlBody.php in Media Cloud Sync 1.4.2, at includes/sdk/s3/Aws/Api/Serializer/XmlBody.php

175 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Dudlewebs\WPMCS\s3\Aws\Api\Serializer;
4
5 use Dudlewebs\WPMCS\s3\Aws\Api\MapShape;
6 use Dudlewebs\WPMCS\s3\Aws\Api\Service;
7 use Dudlewebs\WPMCS\s3\Aws\Api\Shape;
8 use Dudlewebs\WPMCS\s3\Aws\Api\StructureShape;
9 use Dudlewebs\WPMCS\s3\Aws\Api\ListShape;
10 use Dudlewebs\WPMCS\s3\Aws\Api\TimestampShape;
11 use XMLWriter;
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 * @param Service $api API being used to create the XML body.
21 */
22 public function __construct(Service $api)
23 {
24 $this->api = $api;
25 }
26 /**
27 * Builds the XML body based on an array of arguments.
28 *
29 * @param Shape $shape Operation being constructed
30 * @param array $args Associative array of arguments
31 *
32 * @return string
33 */
34 public function build(Shape $shape, array $args)
35 {
36 $xml = new XMLWriter();
37 $xml->openMemory();
38 $xml->startDocument('1.0', 'UTF-8');
39 $rootElementName = $this->determineRootElementName($shape);
40 $this->format($shape, $rootElementName, $args, $xml);
41 $xml->endDocument();
42 return $xml->outputMemory();
43 }
44 private function startElement(Shape $shape, $name, XMLWriter $xml)
45 {
46 $xml->startElement($name);
47 if ($ns = $shape['xmlNamespace']) {
48 $xml->writeAttribute(isset($ns['prefix']) ? "xmlns:{$ns['prefix']}" : 'xmlns', $ns['uri']);
49 }
50 }
51 private function format(Shape $shape, $name, $value, XMLWriter $xml)
52 {
53 // Any method mentioned here has a custom serialization handler.
54 static $methods = ['add_structure' => \true, 'add_list' => \true, 'add_blob' => \true, 'add_timestamp' => \true, 'add_boolean' => \true, 'add_map' => \true, 'add_string' => \true];
55 $type = 'add_' . $shape['type'];
56 if (isset($methods[$type])) {
57 $this->{$type}($shape, $name, $value, $xml);
58 } else {
59 $this->defaultShape($shape, $name, $value, $xml);
60 }
61 }
62 private function defaultShape(Shape $shape, $name, $value, XMLWriter $xml)
63 {
64 $this->startElement($shape, $name, $xml);
65 $xml->text($value);
66 $xml->endElement();
67 }
68 private function add_structure(StructureShape $shape, $name, array $value, \XMLWriter $xml)
69 {
70 $this->startElement($shape, $name, $xml);
71 foreach ($this->getStructureMembers($shape, $value) as $k => $definition) {
72 // Default to member name
73 $elementName = $k;
74 // Only use locationName for non-structure members
75 if (!$definition['member'] instanceof StructureShape && $definition['member']['locationName']) {
76 $elementName = $definition['member']['locationName'];
77 }
78 $this->format($definition['member'], $elementName, $definition['value'], $xml);
79 }
80 $xml->endElement();
81 }
82 private function getStructureMembers(StructureShape $shape, array $value)
83 {
84 $members = [];
85 foreach ($value as $k => $v) {
86 if ($v !== null && $shape->hasMember($k)) {
87 $definition = ['member' => $shape->getMember($k), 'value' => $v];
88 if ($definition['member']['xmlAttribute']) {
89 // array_unshift_associative
90 $members = [$k => $definition] + $members;
91 } else {
92 $members[$k] = $definition;
93 }
94 }
95 }
96 return $members;
97 }
98 private function add_list(ListShape $shape, $name, array $value, XMLWriter $xml)
99 {
100 $items = $shape->getMember();
101 if ($shape['flattened']) {
102 $elementName = $name;
103 } else {
104 $this->startElement($shape, $name, $xml);
105 $elementName = $items['locationName'] ?: 'member';
106 }
107 foreach ($value as $v) {
108 $this->format($items, $elementName, $v, $xml);
109 }
110 if (!$shape['flattened']) {
111 $xml->endElement();
112 }
113 }
114 private function add_map(MapShape $shape, $name, array $value, XMLWriter $xml)
115 {
116 $xmlEntry = $shape['flattened'] ? $name : 'entry';
117 $xmlKey = $shape->getKey()['locationName'] ?: 'key';
118 $xmlValue = $shape->getValue()['locationName'] ?: 'value';
119 if (!$shape['flattened']) {
120 $this->startElement($shape, $name, $xml);
121 }
122 foreach ($value as $key => $v) {
123 $this->startElement($shape, $xmlEntry, $xml);
124 $this->format($shape->getKey(), $xmlKey, $key, $xml);
125 $this->format($shape->getValue(), $xmlValue, $v, $xml);
126 $xml->endElement();
127 }
128 if (!$shape['flattened']) {
129 $xml->endElement();
130 }
131 }
132 private function add_blob(Shape $shape, $name, $value, XMLWriter $xml)
133 {
134 $this->startElement($shape, $name, $xml);
135 $xml->writeRaw(\base64_encode($value));
136 $xml->endElement();
137 }
138 private function add_timestamp(TimestampShape $shape, $name, $value, XMLWriter $xml)
139 {
140 $this->startElement($shape, $name, $xml);
141 $timestampFormat = !empty($shape['timestampFormat']) ? $shape['timestampFormat'] : 'iso8601';
142 $xml->writeRaw(TimestampShape::format($value, $timestampFormat));
143 $xml->endElement();
144 }
145 private function add_boolean(Shape $shape, $name, $value, XMLWriter $xml)
146 {
147 $this->startElement($shape, $name, $xml);
148 $xml->writeRaw($value ? 'true' : 'false');
149 $xml->endElement();
150 }
151 private function add_string(Shape $shape, $name, $value, XMLWriter $xml)
152 {
153 if ($shape['xmlAttribute']) {
154 $xml->writeAttribute($shape['locationName'] ?: $name, $value);
155 } else {
156 $this->defaultShape($shape, $name, $value, $xml);
157 }
158 }
159 private function determineRootElementName(Shape $shape) : string
160 {
161 $shapeName = $shape->getName();
162 // Look up the shape definition first
163 if ($shapeName && ($shapeMap = $shape->getShapeMap())) {
164 if (isset($shapeMap[$shapeName]['locationName'])) {
165 return $shapeMap[$shapeName]['locationName'];
166 }
167 }
168 // Fall back to shape's current locationName
169 if ($shape['locationName']) {
170 return $shape['locationName'];
171 }
172 return $shapeName;
173 }
174 }
175