PluginProbe
Media Cloud Sync / 1.2.7
Media Cloud Sync v1.2.7
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.2.7, at includes/sdk/s3/Aws/Api/Serializer/XmlBody.php

149 lines 5.4 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 \Aws\Api\Service */
18 private $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 $this->format($shape, $shape['locationName'] ?: $shape['name'], $args, $xml);
40 $xml->endDocument();
41 return $xml->outputMemory();
42 }
43 private function startElement(Shape $shape, $name, XMLWriter $xml)
44 {
45 $xml->startElement($name);
46 if ($ns = $shape['xmlNamespace']) {
47 $xml->writeAttribute(isset($ns['prefix']) ? "xmlns:{$ns['prefix']}" : 'xmlns', $shape['xmlNamespace']['uri']);
48 }
49 }
50 private function format(Shape $shape, $name, $value, XMLWriter $xml)
51 {
52 // Any method mentioned here has a custom serialization handler.
53 static $methods = ['add_structure' => \true, 'add_list' => \true, 'add_blob' => \true, 'add_timestamp' => \true, 'add_boolean' => \true, 'add_map' => \true, 'add_string' => \true];
54 $type = 'add_' . $shape['type'];
55 if (isset($methods[$type])) {
56 $this->{$type}($shape, $name, $value, $xml);
57 } else {
58 $this->defaultShape($shape, $name, $value, $xml);
59 }
60 }
61 private function defaultShape(Shape $shape, $name, $value, XMLWriter $xml)
62 {
63 $this->startElement($shape, $name, $xml);
64 $xml->text($value);
65 $xml->endElement();
66 }
67 private function add_structure(StructureShape $shape, $name, array $value, \XMLWriter $xml)
68 {
69 $this->startElement($shape, $name, $xml);
70 foreach ($this->getStructureMembers($shape, $value) as $k => $definition) {
71 $this->format($definition['member'], $definition['member']['locationName'] ?: $k, $definition['value'], $xml);
72 }
73 $xml->endElement();
74 }
75 private function getStructureMembers(StructureShape $shape, array $value)
76 {
77 $members = [];
78 foreach ($value as $k => $v) {
79 if ($v !== null && $shape->hasMember($k)) {
80 $definition = ['member' => $shape->getMember($k), 'value' => $v];
81 if ($definition['member']['xmlAttribute']) {
82 // array_unshift_associative
83 $members = [$k => $definition] + $members;
84 } else {
85 $members[$k] = $definition;
86 }
87 }
88 }
89 return $members;
90 }
91 private function add_list(ListShape $shape, $name, array $value, XMLWriter $xml)
92 {
93 $items = $shape->getMember();
94 if ($shape['flattened']) {
95 $elementName = $name;
96 } else {
97 $this->startElement($shape, $name, $xml);
98 $elementName = $items['locationName'] ?: 'member';
99 }
100 foreach ($value as $v) {
101 $this->format($items, $elementName, $v, $xml);
102 }
103 if (!$shape['flattened']) {
104 $xml->endElement();
105 }
106 }
107 private function add_map(MapShape $shape, $name, array $value, XMLWriter $xml)
108 {
109 $xmlEntry = $shape['flattened'] ? $shape['locationName'] : 'entry';
110 $xmlKey = $shape->getKey()['locationName'] ?: 'key';
111 $xmlValue = $shape->getValue()['locationName'] ?: 'value';
112 $this->startElement($shape, $name, $xml);
113 foreach ($value as $key => $v) {
114 $this->startElement($shape, $xmlEntry, $xml);
115 $this->format($shape->getKey(), $xmlKey, $key, $xml);
116 $this->format($shape->getValue(), $xmlValue, $v, $xml);
117 $xml->endElement();
118 }
119 $xml->endElement();
120 }
121 private function add_blob(Shape $shape, $name, $value, XMLWriter $xml)
122 {
123 $this->startElement($shape, $name, $xml);
124 $xml->writeRaw(\base64_encode($value));
125 $xml->endElement();
126 }
127 private function add_timestamp(TimestampShape $shape, $name, $value, XMLWriter $xml)
128 {
129 $this->startElement($shape, $name, $xml);
130 $timestampFormat = !empty($shape['timestampFormat']) ? $shape['timestampFormat'] : 'iso8601';
131 $xml->writeRaw(TimestampShape::format($value, $timestampFormat));
132 $xml->endElement();
133 }
134 private function add_boolean(Shape $shape, $name, $value, XMLWriter $xml)
135 {
136 $this->startElement($shape, $name, $xml);
137 $xml->writeRaw($value ? 'true' : 'false');
138 $xml->endElement();
139 }
140 private function add_string(Shape $shape, $name, $value, XMLWriter $xml)
141 {
142 if ($shape['xmlAttribute']) {
143 $xml->writeAttribute($shape['locationName'] ?: $name, $value);
144 } else {
145 $this->defaultShape($shape, $name, $value, $xml);
146 }
147 }
148 }
149