HeaderLocation.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace GuzzleHttp\Command\Guzzle\ResponseLocation;
  3. use GuzzleHttp\Command\Guzzle\Parameter;
  4. use GuzzleHttp\Command\ResultInterface;
  5. use Psr\Http\Message\ResponseInterface;
  6. /**
  7. * Extracts headers from the response into a result fields
  8. */
  9. class HeaderLocation extends AbstractLocation
  10. {
  11. /**
  12. * Set the name of the location
  13. *
  14. * @param string $locationName
  15. */
  16. public function __construct($locationName = 'header')
  17. {
  18. parent::__construct($locationName);
  19. }
  20. /**
  21. * @param ResultInterface $result
  22. * @param ResponseInterface $response
  23. * @param Parameter $param
  24. *
  25. * @return ResultInterface
  26. */
  27. public function visit(
  28. ResultInterface $result,
  29. ResponseInterface $response,
  30. Parameter $param
  31. ) {
  32. // Retrieving a single header by name
  33. $name = $param->getName();
  34. if ($header = $response->getHeader($param->getWireName())) {
  35. if (is_array($header)) {
  36. $header = array_shift($header);
  37. }
  38. $result[$name] = $param->filter($header);
  39. }
  40. return $result;
  41. }
  42. }