BodyLocation.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace GuzzleHttp\Command\Guzzle\RequestLocation;
  3. use GuzzleHttp\Command\CommandInterface;
  4. use GuzzleHttp\Command\Guzzle\Parameter;
  5. use GuzzleHttp\Psr7;
  6. use Psr\Http\Message\MessageInterface;
  7. use Psr\Http\Message\RequestInterface;
  8. /**
  9. * Adds a body to a request
  10. */
  11. class BodyLocation extends AbstractLocation
  12. {
  13. /**
  14. * Set the name of the location
  15. *
  16. * @param string $locationName
  17. */
  18. public function __construct($locationName = 'body')
  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. $oldValue = $request->getBody()->getContents();
  35. $value = $command[$param->getName()];
  36. $value = $param->getName() . '=' . $param->filter($value);
  37. if ($oldValue !== '') {
  38. $value = $oldValue . '&' . $value;
  39. }
  40. return $request->withBody(Psr7\stream_for($value));
  41. }
  42. }