GetBucketLocationParser.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace Aws\S3;
  3. use Aws\Api\Parser\AbstractParser;
  4. use Aws\CommandInterface;
  5. use Psr\Http\Message\ResponseInterface;
  6. /**
  7. * @internal Decorates a parser for the S3 service to correctly handle the
  8. * GetBucketLocation operation.
  9. */
  10. class GetBucketLocationParser extends AbstractParser
  11. {
  12. /** @var callable */
  13. private $parser;
  14. /**
  15. * @param callable $parser Parser to wrap.
  16. */
  17. public function __construct(callable $parser)
  18. {
  19. $this->parser = $parser;
  20. }
  21. public function __invoke(
  22. CommandInterface $command,
  23. ResponseInterface $response
  24. ) {
  25. $fn = $this->parser;
  26. $result = $fn($command, $response);
  27. if ($command->getName() === 'GetBucketLocation') {
  28. $location = 'us-east-1';
  29. if (preg_match('/>(.+?)<\/LocationConstraint>/', $response->getBody(), $matches)) {
  30. $location = $matches[1] === 'EU' ? 'eu-west-1' : $matches[1];
  31. }
  32. $result['LocationConstraint'] = $location;
  33. }
  34. return $result;
  35. }
  36. }