Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/Api/Serializer/RpcV2CborSerializer.php

raw symbols SCIP
1 <?php
2 namespace Aws\Api\Serializer;
3
4 use Aws\Api\Cbor\CborEncoder;
5 use Aws\Api\Cbor\Exception\CborException;
6 use Aws\Api\Exception\RpcV2CborException;
7 use Aws\Api\Service;
8 use Aws\Api\StructureShape;
9 use DateTimeInterface;
10
11 /**
12 * Serializes requests according to Smithy RPC-V2 CBOR protocol standards.
13 *
14 * https://smithy.io/2.0/additional-specs/protocols/smithy-rpc-v2.html
15 *
16 * @internal
17 */
18 final class RpcV2CborSerializer extends AbstractRpcV2Serializer
19 {
20 /** @var array|string[] */
21 protected static array $defaultHeaders = [
22 self::HEADER_SMITHY_PROTOCOL => 'rpc-v2-cbor',
23 self::HEADER_CONTENT_TYPE => 'application/cbor',
24 self::HEADER_ACCEPT => 'application/cbor',
25 ];
26
27 /** @var CborEncoder */
28 private CborEncoder $encoder;
29
30 /**
31 * @param Service $api Service API description
32 * @param string $endpoint Endpoint to connect to
33 */
34 public function __construct(Service $api, string $endpoint)
35 {
36 $this->encoder = new CborEncoder();
37 parent::__construct($api, $endpoint);
38 }
39
40 /**
41 * @param StructureShape $inputShape
42 * @param array $commandArgs
43 *
44 * @return string
45 * @throws RpcV2CborException
46 */
47 public function serialize(
48 StructureShape $inputShape,
49 array $commandArgs
50 ): string
51 {
52 try {
53 $resolvedInput = $this->resolveInputShape($inputShape, $commandArgs);
54 return !empty($resolvedInput)
55 ? $this->encoder->encode($resolvedInput)
56 : $this->encoder->encodeEmptyIndefiniteMap();
57 } catch (CborException $e) {
58 throw new RpcV2CborException(
59 'Unable to encode CBOR document ' . $inputShape->getName() . ': ' .
60 $e->getMessage() . PHP_EOL
61 );
62 }
63 }
64
65 /**
66 * Wraps blob values in order to be encoded properly into
67 * byte strings.
68 *
69 * @param mixed $value
70 *
71 * @return string[]
72 * @throws RpcV2CborException
73 */
74 protected function resolveBlob(mixed $value): array
75 {
76 if (is_resource($value)) {
77 $value = stream_get_contents($value);
78 if ($value === false) {
79 throw new RpcV2CborException(
80 'Failed to read resource stream value during serialization',
81 );
82 }
83 }
84
85 // Wrapper to differentiate byte string values during encoding
86 return ['__cbor_bytes' => (string) $value];
87 }
88
89 /**
90 * Wraps timestamp values in order to be encoded properly into
91 * value tag 1.
92 *
93 * @param mixed $value
94 *
95 * @return string[]
96 * @throws RpcV2CborException
97 */
98 protected function resolveTimestamp(
99 int|float|string|DateTimeInterface $value
100 ): array
101 {
102 if (is_numeric($value)) {
103 return ['__cbor_timestamp' => $value];
104 }
105
106 if ($value instanceof DateTimeInterface) {
107 // Preserve milliseconds
108 $micro = (int) $value->format('u');
109 $value = $value->getTimestamp() + $micro / 1e6;
110 } else {
111 $timestamp = strtotime($value);
112 if ($timestamp === false) {
113 throw new RpcV2CborException(
114 'Request serialization failed: Invalid date/time: ' . $value,
115 );
116 }
117
118 $value = $timestamp;
119 }
120
121 // Wrapper to differentiate timestamp values during encoding
122 return ['__cbor_timestamp' => $value];
123 }
124 }