HeaderLocation.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace GuzzleHttp\Command\Guzzle\RequestLocation;
  3. use GuzzleHttp\Command\CommandInterface;
  4. use GuzzleHttp\Command\Guzzle\Operation;
  5. use GuzzleHttp\Command\Guzzle\Parameter;
  6. use Psr\Http\Message\MessageInterface;
  7. use Psr\Http\Message\RequestInterface;
  8. /**
  9. * Request header location
  10. */
  11. class HeaderLocation extends AbstractLocation
  12. {
  13. /**
  14. * Set the name of the location
  15. *
  16. * @param string $locationName
  17. */
  18. public function __construct($locationName = 'header')
  19. {
  20. parent::__construct($locationName);
  21. }
  22. /**
  23. * @param CommandInterface $command
  24. * @param RequestInterface $request
  25. * @param Parameter $param
  26. *
  27. * @return MessageInterface
  28. */
  29. public function visit(
  30. CommandInterface $command,
  31. RequestInterface $request,
  32. Parameter $param
  33. ) {
  34. $value = $command[$param->getName()];
  35. return $request->withHeader($param->getWireName(), $param->filter($value));
  36. }
  37. /**
  38. * @param CommandInterface $command
  39. * @param RequestInterface $request
  40. * @param Operation $operation
  41. *
  42. * @return RequestInterface
  43. */
  44. public function after(
  45. CommandInterface $command,
  46. RequestInterface $request,
  47. Operation $operation
  48. ) {
  49. /** @var Parameter $additional */
  50. $additional = $operation->getAdditionalParameters();
  51. if ($additional && ($additional->getLocation() === $this->locationName)) {
  52. foreach ($command->toArray() as $key => $value) {
  53. if (!$operation->hasParam($key)) {
  54. $request = $request->withHeader($key, $additional->filter($value));
  55. }
  56. }
  57. }
  58. return $request;
  59. }
  60. }