RestJsonSerializer.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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 = 'application/json';
  27. $this->jsonFormatter = $jsonFormatter ?: new JsonBody($api);
  28. }
  29. protected function payload(StructureShape $member, array $value, array &$opts)
  30. {
  31. $opts['headers']['Content-Type'] = $this->contentType;
  32. $opts['body'] = (string) $this->jsonFormatter->build($member, $value);
  33. }
  34. }