RestJsonSerializer.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace Aws\Api\Serializer;
  3. use Aws\Api\Service;
  4. use Aws\Api\StructureShape;
  5. /**
  6. * Serializes requests for the REST-JSON protocol.
  7. * @internal
  8. */
  9. class RestJsonSerializer extends RestSerializer
  10. {
  11. /** @var JsonBody */
  12. private $jsonFormatter;
  13. /** @var string */
  14. private $contentType;
  15. /**
  16. * @param Service $api Service API description
  17. * @param string $endpoint Endpoint to connect to
  18. * @param JsonBody $jsonFormatter Optional JSON formatter to use
  19. */
  20. public function __construct(
  21. Service $api,
  22. $endpoint,
  23. JsonBody $jsonFormatter = null
  24. ) {
  25. parent::__construct($api, $endpoint);
  26. $this->contentType = JsonBody::getContentType($api);
  27. $this->jsonFormatter = $jsonFormatter ?: new JsonBody($api);
  28. }
  29. protected function payload(StructureShape $member, array $value, array &$opts)
  30. {
  31. $body = isset($value) ?
  32. ((string) $this->jsonFormatter->build($member, $value))
  33. : "{}";
  34. $opts['headers']['Content-Type'] = $this->contentType;
  35. $opts['body'] = $body;
  36. }
  37. }