RestXmlSerializer.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace Aws\Api\Serializer;
  3. use Aws\Api\StructureShape;
  4. use Aws\Api\Service;
  5. /**
  6. * @internal
  7. */
  8. class RestXmlSerializer extends RestSerializer
  9. {
  10. /** @var XmlBody */
  11. private $xmlBody;
  12. /**
  13. * @param Service $api Service API description
  14. * @param string $endpoint Endpoint to connect to
  15. * @param XmlBody $xmlBody Optional XML formatter to use
  16. */
  17. public function __construct(
  18. Service $api,
  19. $endpoint,
  20. XmlBody $xmlBody = null
  21. ) {
  22. parent::__construct($api, $endpoint);
  23. $this->xmlBody = $xmlBody ?: new XmlBody($api);
  24. }
  25. protected function payload(StructureShape $member, array $value, array &$opts)
  26. {
  27. $opts['headers']['Content-Type'] = 'application/xml';
  28. $opts['body'] = $this->getXmlBody($member, $value);
  29. }
  30. /**
  31. * @param StructureShape $member
  32. * @param array $value
  33. * @return string
  34. */
  35. private function getXmlBody(StructureShape $member, array $value)
  36. {
  37. $xmlBody = (string)$this->xmlBody->build($member, $value);
  38. $xmlBody = str_replace("'", "&apos;", $xmlBody);
  39. $xmlBody = str_replace('\r', "&#13;", $xmlBody);
  40. $xmlBody = str_replace('\n', "&#10;", $xmlBody);
  41. return $xmlBody;
  42. }
  43. }