1<?php
2namespace Aws\Api\Parser;
3
4use Aws\Api\DateTimeResult;
5use Aws\Api\ListShape;
6use Aws\Api\MapShape;
7use Aws\Api\Parser\Exception\ParserException;
8use Aws\Api\Shape;
9use Aws\Api\StructureShape;
10
11/**
12 * @internal Implements standard XML parsing for REST-XML and Query protocols.
13 */
14class XmlParser
15{
16    public function parse(StructureShape $shape, \SimpleXMLElement $value)
17    {
18        return $this->dispatch($shape, $value);
19    }
20
21    private function dispatch($shape, \SimpleXMLElement $value)
22    {
23        static $methods = [
24            'structure' => 'parse_structure',
25            'list'      => 'parse_list',
26            'map'       => 'parse_map',
27            'blob'      => 'parse_blob',
28            'boolean'   => 'parse_boolean',
29            'integer'   => 'parse_integer',
30            'float'     => 'parse_float',
31            'double'    => 'parse_float',
32            'timestamp' => 'parse_timestamp',
33        ];
34
35        $type = $shape['type'];
36        if (isset($methods[$type])) {
37            return $this->{$methods[$type]}($shape, $value);
38        }
39
40        return (string) $value;
41    }
42
43    private function parse_structure(
44        StructureShape $shape,
45        \SimpleXMLElement $value
46    ) {
47        $target = [];
48
49        foreach ($shape->getMembers() as $name => $member) {
50            // Extract the name of the XML node
51            $node = $this->memberKey($member, $name);
52            if (isset($value->{$node})) {
53                $target[$name] = $this->dispatch($member, $value->{$node});
54            } else {
55                $memberShape = $shape->getMember($name);
56                if (!empty($memberShape['xmlAttribute'])) {
57                    $target[$name] = $this->parse_xml_attribute(
58                        $shape,
59                        $memberShape,
60                        $value
61                    );
62                }
63            }
64        }
65
66        return $target;
67    }
68
69    private function memberKey(Shape $shape, $name)
70    {
71        if (null !== $shape['locationName']) {
72            return $shape['locationName'];
73        }
74
75        if ($shape instanceof ListShape && $shape['flattened']) {
76            return $shape->getMember()['locationName'] ?: $name;
77        }
78
79        return $name;
80    }
81
82    private function parse_list(ListShape $shape, \SimpleXMLElement  $value)
83    {
84        $target = [];
85        $member = $shape->getMember();
86
87        if (!$shape['flattened']) {
88            $value = $value->{$member['locationName'] ?: 'member'};
89        }
90
91        foreach ($value as $v) {
92            $target[] = $this->dispatch($member, $v);
93        }
94
95        return $target;
96    }
97
98    private function parse_map(MapShape $shape, \SimpleXMLElement $value)
99    {
100        $target = [];
101
102        if (!$shape['flattened']) {
103            $value = $value->entry;
104        }
105
106        $mapKey = $shape->getKey();
107        $mapValue = $shape->getValue();
108        $keyName = $shape->getKey()['locationName'] ?: 'key';
109        $valueName = $shape->getValue()['locationName'] ?: 'value';
110
111        foreach ($value as $node) {
112            $key = $this->dispatch($mapKey, $node->{$keyName});
113            $value = $this->dispatch($mapValue, $node->{$valueName});
114            $target[$key] = $value;
115        }
116
117        return $target;
118    }
119
120    private function parse_blob(Shape $shape, $value)
121    {
122        return base64_decode((string) $value);
123    }
124
125    private function parse_float(Shape $shape, $value)
126    {
127        return (float) (string) $value;
128    }
129
130    private function parse_integer(Shape $shape, $value)
131    {
132        return (int) (string) $value;
133    }
134
135    private function parse_boolean(Shape $shape, $value)
136    {
137        return $value == 'true';
138    }
139
140    private function parse_timestamp(Shape $shape, $value)
141    {
142        if (is_string($value)
143            || is_int($value)
144            || (is_object($value)
145                && method_exists($value, '__toString'))
146        ) {
147            return DateTimeResult::fromTimestamp(
148                (string) $value,
149                !empty($shape['timestampFormat']) ? $shape['timestampFormat'] : null
150            );
151        }
152        throw new ParserException('Invalid timestamp value passed to XmlParser::parse_timestamp');
153    }
154
155    private function parse_xml_attribute(Shape $shape, Shape $memberShape, $value)
156    {
157        $namespace = $shape['xmlNamespace']['uri']
158            ? $shape['xmlNamespace']['uri']
159            : '';
160        $prefix = $shape['xmlNamespace']['prefix']
161            ? $shape['xmlNamespace']['prefix']
162            : '';
163        if (!empty($prefix)) {
164            $prefix .= ':';
165        }
166        $key = str_replace($prefix, '', $memberShape['locationName']);
167
168        $attributes = $value->attributes($namespace);
169        return isset($attributes[$key]) ? (string) $attributes[$key] : null;
170    }
171}
172