PluginProbe
Media Cloud Sync / 1.4.1
Media Cloud Sync v1.4.1
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.4.1, at includes/sdk/s3/Aws/Api/Parser/XmlParser.php

131 lines 5.0 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 // Check if locationName came from shape definition
55 if ($shape instanceof StructureShape && isset($shape['locationName'])) {
56 $originalDef = $shape->getOriginalDefinition($shape->getName());
57 if ($originalDef && isset($originalDef['locationName']) && $originalDef['locationName'] === $shape['locationName']) {
58 return $name;
59 }
60 }
61 return $shape['locationName'] ?? $name;
62 }
63 private function parse_list(ListShape $shape, \SimpleXMLElement $value)
64 {
65 $target = [];
66 $member = $shape->getMember();
67 if (!$shape['flattened']) {
68 $value = $value->{$member['locationName'] ?: 'member'};
69 }
70 foreach ($value as $v) {
71 $target[] = $this->dispatch($member, $v);
72 }
73 return $target;
74 }
75 private function parse_map(MapShape $shape, \SimpleXMLElement $value)
76 {
77 $target = [];
78 if (!$shape['flattened']) {
79 $value = $value->entry;
80 }
81 $mapKey = $shape->getKey();
82 $mapValue = $shape->getValue();
83 $keyName = $shape->getKey()['locationName'] ?: 'key';
84 $valueName = $shape->getValue()['locationName'] ?: 'value';
85 foreach ($value as $node) {
86 $key = $this->dispatch($mapKey, $node->{$keyName});
87 $value = $this->dispatch($mapValue, $node->{$valueName});
88 $target[$key] = $value;
89 }
90 return $target;
91 }
92 private function parse_blob(Shape $shape, $value)
93 {
94 return \base64_decode((string) $value);
95 }
96 private function parse_float(Shape $shape, $value)
97 {
98 $value = (string) $value;
99 return match ($value) {
100 'NaN', 'Infinity', '-Infinity' => $value,
101 default => (float) $value,
102 };
103 }
104 private function parse_integer(Shape $shape, $value)
105 {
106 return (int) (string) $value;
107 }
108 private function parse_boolean(Shape $shape, $value)
109 {
110 return $value == 'true';
111 }
112 private function parse_timestamp(Shape $shape, $value)
113 {
114 if (\is_string($value) || \is_int($value) || \is_object($value) && \method_exists($value, '__toString')) {
115 return DateTimeResult::fromTimestamp((string) $value, !empty($shape['timestampFormat']) ? $shape['timestampFormat'] : null);
116 }
117 throw new ParserException('Invalid timestamp value passed to XmlParser::parse_timestamp');
118 }
119 private function parse_xml_attribute(Shape $shape, Shape $memberShape, $value)
120 {
121 $namespace = $shape['xmlNamespace']['uri'] ?? '';
122 $prefix = $shape['xmlNamespace']['prefix'] ?? '';
123 if (!empty($prefix)) {
124 $prefix .= ':';
125 }
126 $key = \str_replace($prefix, '', $memberShape['locationName']);
127 $attributes = $value->attributes($namespace);
128 return isset($attributes[$key]) ? (string) $attributes[$key] : null;
129 }
130 }
131