BodyLocation.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. * @return MessageInterface
  24. */
  25. public function visit(
  26. CommandInterface $command,
  27. RequestInterface $request,
  28. Parameter $param
  29. ) {
  30. $oldValue = $request->getBody()->getContents();
  31. $value = $command[$param->getName()];
  32. $value = $param->getName().'='.$param->filter($value);
  33. if ($oldValue !== '') {
  34. $value = $oldValue.'&'.$value;
  35. }
  36. return $request->withBody(Psr7\Utils::streamFor($value));
  37. }
  38. }