Exception
11 months ago
AbstractParser.php
11 months ago
AbstractRestParser.php
11 months ago
Crc32ValidatingParser.php
11 months ago
DecodingEventStreamIterator.php
11 months ago
EventParsingIterator.php
11 months ago
JsonParser.php
11 months ago
JsonRpcParser.php
11 months ago
MetadataParserTrait.php
11 months ago
PayloadParserTrait.php
11 months ago
QueryParser.php
11 months ago
RestJsonParser.php
11 months ago
RestXmlParser.php
11 months ago
XmlParser.php
11 months ago
MetadataParserTrait.php
91 lines
| 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 | |
| 21 | switch ($shape->getType()) { |
| 22 | case 'float': |
| 23 | case 'double': |
| 24 | $value = (float) $value; |
| 25 | break; |
| 26 | case 'long': |
| 27 | $value = (int) $value; |
| 28 | break; |
| 29 | case 'boolean': |
| 30 | $value = filter_var($value, FILTER_VALIDATE_BOOLEAN); |
| 31 | break; |
| 32 | case 'blob': |
| 33 | $value = base64_decode($value); |
| 34 | break; |
| 35 | case 'timestamp': |
| 36 | try { |
| 37 | $value = DateTimeResult::fromTimestamp( |
| 38 | $value, |
| 39 | !empty($shape['timestampFormat']) ? $shape['timestampFormat'] : null |
| 40 | ); |
| 41 | break; |
| 42 | } catch (\Exception $e) { |
| 43 | // If the value cannot be parsed, then do not add it to the |
| 44 | // output structure. |
| 45 | return; |
| 46 | } |
| 47 | case 'string': |
| 48 | if ($shape['jsonvalue']) { |
| 49 | $value = $this->parseJson(base64_decode($value), $response); |
| 50 | } |
| 51 | break; |
| 52 | } |
| 53 | |
| 54 | $result[$name] = $value; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Extract a map of headers with an optional prefix from the response. |
| 59 | */ |
| 60 | protected function extractHeaders( |
| 61 | $name, |
| 62 | Shape $shape, |
| 63 | ResponseInterface $response, |
| 64 | &$result |
| 65 | ) { |
| 66 | // Check if the headers are prefixed by a location name |
| 67 | $result[$name] = []; |
| 68 | $prefix = $shape['locationName']; |
| 69 | $prefixLen = strlen($prefix); |
| 70 | |
| 71 | foreach ($response->getHeaders() as $k => $values) { |
| 72 | if (!$prefixLen) { |
| 73 | $result[$name][$k] = implode(', ', $values); |
| 74 | } elseif (stripos($k, $prefix) === 0) { |
| 75 | $result[$name][substr($k, $prefixLen)] = implode(', ', $values); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Places the status code of the response into the result array. |
| 82 | */ |
| 83 | protected function extractStatus( |
| 84 | $name, |
| 85 | ResponseInterface $response, |
| 86 | array &$result |
| 87 | ) { |
| 88 | $result[$name] = (int) $response->getStatusCode(); |
| 89 | } |
| 90 | } |
| 91 |