Debian Codegraph @

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

raw symbols SCIP
1 <?php
2 namespace Aws\Api\Parser;
3
4 use Aws\Api\DateTimeResult;
5 use Aws\Api\Shape;
6 use Psr\Http\Message\ResponseInterface;
7
8 trait MetadataParserTrait
9 {
10 /**
11 * Extract a single header from the response into the result.
12 */
13 protected function extractHeader(
14 $name,
15 Shape $shape,
16 ResponseInterface $response,
17 &$result
18 ) {
19 $value = $response->getHeaderLine($shape['locationName'] ?: $name);
20 // Empty values should not be deserialized
21 if ($value === null || $value === '') {
22 return;
23 }
24
25 switch ($shape->getType()) {
26 case 'float':
27 case 'double':
28 $value = (float) $value;
29 break;
30 case 'long':
31 case 'integer':
32 $value = (int) $value;
33 break;
34 case 'boolean':
35 $value = filter_var($value, FILTER_VALIDATE_BOOLEAN);
36 break;
37 case 'blob':
38 $value = base64_decode($value);
39 break;
40 case 'timestamp':
41 try {
42 $value = DateTimeResult::fromTimestamp(
43 $value,
44 !empty($shape['timestampFormat']) ? $shape['timestampFormat'] : null
45 );
46 break;
47 } catch (\Exception $e) {
48 // If the value cannot be parsed, then do not add it to the
49 // output structure.
50 return;
51 }
52 case 'string':
53 if ($shape['jsonvalue']) {
54 $value = $this->parseJson(base64_decode($value), $response);
55 }
56 break;
57 }
58
59 $result[$name] = $value;
60 }
61
62 /**
63 * Extract a map of headers with an optional prefix from the response.
64 */
65 protected function extractHeaders(
66 $name,
67 Shape $shape,
68 ResponseInterface $response,
69 &$result
70 ) {
71 // Check if the headers are prefixed by a location name
72 $result[$name] = [];
73 $prefix = $shape['locationName'];
74 $prefixLen = strlen($prefix);
75
76 foreach ($response->getHeaders() as $k => $values) {
77 if (!$prefixLen) {
78 $result[$name][$k] = implode(', ', $values);
79 } elseif (stripos($k, $prefix) === 0) {
80 $result[$name][substr($k, $prefixLen)] = implode(', ', $values);
81 }
82 }
83 }
84
85 /**
86 * Places the status code of the response into the result array.
87 */
88 protected function extractStatus(
89 $name,
90 ResponseInterface $response,
91 array &$result
92 ) {
93 $result[$name] = (int) $response->getStatusCode();
94 }
95 }