QueryParamBuilder.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace Aws\Api\Serializer;
  3. use Aws\Api\StructureShape;
  4. use Aws\Api\ListShape;
  5. use Aws\Api\MapShape;
  6. use Aws\Api\Shape;
  7. use Aws\Api\TimestampShape;
  8. /**
  9. * @internal
  10. */
  11. class QueryParamBuilder
  12. {
  13. private $methods;
  14. protected function queryName(Shape $shape, $default = null)
  15. {
  16. if (null !== $shape['queryName']) {
  17. return $shape['queryName'];
  18. }
  19. if (null !== $shape['locationName']) {
  20. return $shape['locationName'];
  21. }
  22. if ($this->isFlat($shape) && !empty($shape['member']['locationName'])) {
  23. return $shape['member']['locationName'];
  24. }
  25. return $default;
  26. }
  27. protected function isFlat(Shape $shape)
  28. {
  29. return $shape['flattened'] === true;
  30. }
  31. public function __invoke(StructureShape $shape, array $params)
  32. {
  33. if (!$this->methods) {
  34. $this->methods = array_fill_keys(get_class_methods($this), true);
  35. }
  36. $query = [];
  37. $this->format_structure($shape, $params, '', $query);
  38. return $query;
  39. }
  40. protected function format(Shape $shape, $value, $prefix, array &$query)
  41. {
  42. $type = 'format_' . $shape['type'];
  43. if (isset($this->methods[$type])) {
  44. $this->{$type}($shape, $value, $prefix, $query);
  45. } else {
  46. $query[$prefix] = (string) $value;
  47. }
  48. }
  49. protected function format_structure(
  50. StructureShape $shape,
  51. array $value,
  52. $prefix,
  53. &$query
  54. ) {
  55. if ($prefix) {
  56. $prefix .= '.';
  57. }
  58. foreach ($value as $k => $v) {
  59. if ($shape->hasMember($k)) {
  60. $member = $shape->getMember($k);
  61. $this->format(
  62. $member,
  63. $v,
  64. $prefix . $this->queryName($member, $k),
  65. $query
  66. );
  67. }
  68. }
  69. }
  70. protected function format_list(
  71. ListShape $shape,
  72. array $value,
  73. $prefix,
  74. &$query
  75. ) {
  76. // Handle empty list serialization
  77. if (!$value) {
  78. $query[$prefix] = '';
  79. return;
  80. }
  81. $items = $shape->getMember();
  82. if (!$this->isFlat($shape)) {
  83. $locationName = $shape->getMember()['locationName'] ?: 'member';
  84. $prefix .= ".$locationName";
  85. } elseif ($name = $this->queryName($items)) {
  86. $parts = explode('.', $prefix);
  87. $parts[count($parts) - 1] = $name;
  88. $prefix = implode('.', $parts);
  89. }
  90. foreach ($value as $k => $v) {
  91. $this->format($items, $v, $prefix . '.' . ($k + 1), $query);
  92. }
  93. }
  94. protected function format_map(
  95. MapShape $shape,
  96. array $value,
  97. $prefix,
  98. array &$query
  99. ) {
  100. $vals = $shape->getValue();
  101. $keys = $shape->getKey();
  102. if (!$this->isFlat($shape)) {
  103. $prefix .= '.entry';
  104. }
  105. $i = 0;
  106. $keyName = '%s.%d.' . $this->queryName($keys, 'key');
  107. $valueName = '%s.%s.' . $this->queryName($vals, 'value');
  108. foreach ($value as $k => $v) {
  109. $i++;
  110. $this->format($keys, $k, sprintf($keyName, $prefix, $i), $query);
  111. $this->format($vals, $v, sprintf($valueName, $prefix, $i), $query);
  112. }
  113. }
  114. protected function format_blob(Shape $shape, $value, $prefix, array &$query)
  115. {
  116. $query[$prefix] = base64_encode($value);
  117. }
  118. protected function format_timestamp(
  119. TimestampShape $shape,
  120. $value,
  121. $prefix,
  122. array &$query
  123. ) {
  124. $timestampFormat = !empty($shape['timestampFormat'])
  125. ? $shape['timestampFormat']
  126. : 'iso8601';
  127. $query[$prefix] = TimestampShape::format($value, $timestampFormat);
  128. }
  129. protected function format_boolean(Shape $shape, $value, $prefix, array &$query)
  130. {
  131. $query[$prefix] = ($value) ? 'true' : 'false';
  132. }
  133. }