MetadataParserTrait.php 2.6 KB

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