PluginProbe
Media Cloud Sync / 1.2.4
Media Cloud Sync v1.2.4
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 1.2.9 All 35 releases
media-cloud-sync / includes / sdk / s3 / Aws / Api / Parser / XmlParser.php

XmlParser.php in Media Cloud Sync 1.2.4, at includes/sdk/s3/Aws/Api/Parser/XmlParser.php

126 lines 4.7 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\Parser;
4
5 use Dudlewebs\WPMCS\s3\Aws\Api\DateTimeResult;
6 use Dudlewebs\WPMCS\s3\Aws\Api\ListShape;
7 use Dudlewebs\WPMCS\s3\Aws\Api\MapShape;
8 use Dudlewebs\WPMCS\s3\Aws\Api\Parser\Exception\ParserException;
9 use Dudlewebs\WPMCS\s3\Aws\Api\Shape;
10 use Dudlewebs\WPMCS\s3\Aws\Api\StructureShape;
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 private function dispatch($shape, \SimpleXMLElement $value)
21 {
22 static $methods = ['structure' => 'parse_structure', 'list' => 'parse_list', 'map' => 'parse_map', 'blob' => 'parse_blob', 'boolean' => 'parse_boolean', 'integer' => 'parse_integer', 'float' => 'parse_float', 'double' => 'parse_float', 'timestamp' => 'parse_timestamp'];
23 $type = $shape['type'];
24 if (isset($methods[$type])) {
25 return $this->{$methods[$type]}($shape, $value);
26 }
27 return (string) $value;
28 }
29 private function parse_structure(StructureShape $shape, \SimpleXMLElement $value)
30 {
31 $target = [];
32 foreach ($shape->getMembers() as $name => $member) {
33 // Extract the name of the XML node
34 $node = $this->memberKey($member, $name);
35 if (isset($value->{$node})) {
36 $target[$name] = $this->dispatch($member, $value->{$node});
37 } else {
38 $memberShape = $shape->getMember($name);
39 if (!empty($memberShape['xmlAttribute'])) {
40 $target[$name] = $this->parse_xml_attribute($shape, $memberShape, $value);
41 }
42 }
43 }
44 if (isset($shape['union']) && $shape['union'] && empty($target)) {
45 foreach ($value as $key => $val) {
46 $name = $val->children()->getName();
47 $target['Unknown'][$name] = $val->{$name};
48 }
49 }
50 return $target;
51 }
52 private function memberKey(Shape $shape, $name)
53 {
54 if (null !== $shape['locationName']) {
55 return $shape['locationName'];
56 }
57 if ($shape instanceof ListShape && $shape['flattened']) {
58 return $shape->getMember()['locationName'] ?: $name;
59 }
60 return $name;
61 }
62 private function parse_list(ListShape $shape, \SimpleXMLElement $value)
63 {
64 $target = [];
65 $member = $shape->getMember();
66 if (!$shape['flattened']) {
67 $value = $value->{$member['locationName'] ?: 'member'};
68 }
69 foreach ($value as $v) {
70 $target[] = $this->dispatch($member, $v);
71 }
72 return $target;
73 }
74 private function parse_map(MapShape $shape, \SimpleXMLElement $value)
75 {
76 $target = [];
77 if (!$shape['flattened']) {
78 $value = $value->entry;
79 }
80 $mapKey = $shape->getKey();
81 $mapValue = $shape->getValue();
82 $keyName = $shape->getKey()['locationName'] ?: 'key';
83 $valueName = $shape->getValue()['locationName'] ?: 'value';
84 foreach ($value as $node) {
85 $key = $this->dispatch($mapKey, $node->{$keyName});
86 $value = $this->dispatch($mapValue, $node->{$valueName});
87 $target[$key] = $value;
88 }
89 return $target;
90 }
91 private function parse_blob(Shape $shape, $value)
92 {
93 return \base64_decode((string) $value);
94 }
95 private function parse_float(Shape $shape, $value)
96 {
97 return (float) (string) $value;
98 }
99 private function parse_integer(Shape $shape, $value)
100 {
101 return (int) (string) $value;
102 }
103 private function parse_boolean(Shape $shape, $value)
104 {
105 return $value == 'true';
106 }
107 private function parse_timestamp(Shape $shape, $value)
108 {
109 if (\is_string($value) || \is_int($value) || \is_object($value) && \method_exists($value, '__toString')) {
110 return DateTimeResult::fromTimestamp((string) $value, !empty($shape['timestampFormat']) ? $shape['timestampFormat'] : null);
111 }
112 throw new ParserException('Invalid timestamp value passed to XmlParser::parse_timestamp');
113 }
114 private function parse_xml_attribute(Shape $shape, Shape $memberShape, $value)
115 {
116 $namespace = $shape['xmlNamespace']['uri'] ? $shape['xmlNamespace']['uri'] : '';
117 $prefix = $shape['xmlNamespace']['prefix'] ? $shape['xmlNamespace']['prefix'] : '';
118 if (!empty($prefix)) {
119 $prefix .= ':';
120 }
121 $key = \str_replace($prefix, '', $memberShape['locationName']);
122 $attributes = $value->attributes($namespace);
123 return isset($attributes[$key]) ? (string) $attributes[$key] : null;
124 }
125 }
126