Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/Api/Parser/RpcV2ParserTrait.php

raw symbols SCIP
1 <?php
2 namespace Aws\Api\Parser;
3
4 use Aws\Api\Cbor\Exception\CborException;
5 use Aws\Api\DateTimeResult;
6 use Aws\Api\Parser\Exception\ParserException;
7 use Aws\Api\Shape;
8 use Psr\Http\Message\ResponseInterface;
9 use Psr\Http\Message\StreamInterface;
10
11 /**
12 * Shared parsing logic for RPC V2 Parsers.
13 *
14 * @internal
15 */
16 trait RpcV2ParserTrait
17 {
18 /**
19 * Resolves output shape fields that are present in the response
20 *
21 * @param Shape $shape
22 * @param mixed $value
23 *
24 * @return mixed
25 */
26 protected function resolveOutputShape(Shape $shape, mixed $value): mixed
27 {
28 if ($value === null) {
29 return $value;
30 }
31
32 switch ($shape['type']) {
33 case 'structure':
34 $target = [];
35 foreach ($shape->getMembers() as $name => $member) {
36 $locationName = $member['locationName'] ?: $name;
37 if (isset($value[$locationName])) {
38 $target[$name] = $this->resolveOutputShape($member, $value[$locationName]);
39 }
40 }
41 return $target;
42
43 case 'list':
44 $target = [];
45 foreach ($value as $v) {
46 $target[] = $this->resolveOutputShape($shape->getMember(), $v);
47 }
48 return $target;
49
50 case 'map':
51 $target = [];
52 foreach ($value as $k => $v) {
53 if ($v !== null) {
54 $target[$k] = $this->resolveOutputShape($shape->getValue(), $v);
55 }
56 }
57 return $target;
58
59 case 'timestamp':
60 try {
61 $value = DateTimeResult::fromEpoch($value);
62 } catch (\Exception $e) {
63 trigger_error(
64 'Unable to parse timestamp value for '
65 . $shape->getName()
66 . ': ' . $e->getMessage(),
67 E_USER_WARNING
68 );
69 }
70
71 return $value;
72
73 default:
74 return $value;
75 }
76 }
77
78 /**
79 * Parses CBOR-encoded response data from RPC V2 CBOR services.
80 *
81 * @param StreamInterface $stream
82 * @param ResponseInterface $response
83 *
84 * @return mixed
85 */
86 protected function parseCbor(
87 StreamInterface $stream,
88 ResponseInterface $response
89 ): mixed
90 {
91 try {
92 $cborString = (string) $stream;
93 return empty($cborString)
94 ? null
95 : $this->decoder->decode($cborString);
96 } catch (CborException $e) {
97 throw new ParserException(
98 "Malformed Response: error parsing CBOR: {$e->getMessage()}",
99 0,
100 $e,
101 ['response' => $response]
102 );
103 }
104 }
105 }