$locationName
$locationName : string
Add form_params to a request
$locationName : string
$contentType : string
$formParamsData : array
visit(\GuzzleHttp\Command\CommandInterface $command, \Psr\Http\Message\RequestInterface $request, \GuzzleHttp\Command\Guzzle\Parameter $param) : \Psr\Http\Message\RequestInterface
Visits a location for each top-level parameter
\GuzzleHttp\Command\CommandInterface | $command | |
\Psr\Http\Message\RequestInterface | $request | |
\GuzzleHttp\Command\Guzzle\Parameter | $param |
after(\GuzzleHttp\Command\CommandInterface $command, \Psr\Http\Message\RequestInterface $request, \GuzzleHttp\Command\Guzzle\Operation $operation) : \Psr\Http\Message\RequestInterface
Called when all of the parameters of a command have been visited.
\GuzzleHttp\Command\CommandInterface | $command | |
\Psr\Http\Message\RequestInterface | $request | |
\GuzzleHttp\Command\Guzzle\Operation | $operation |
resolveRecursively(array $value, \GuzzleHttp\Command\Guzzle\Parameter $param) : array
Recursively prepare and filter nested values.
array | $value | Value to map |
\GuzzleHttp\Command\Guzzle\Parameter | $param | Parameter related to the current key. |
Returns the mapped array
<?php
namespace GuzzleHttp\Command\Guzzle\RequestLocation;
use GuzzleHttp\Command\CommandInterface;
use GuzzleHttp\Command\Guzzle\Operation;
use GuzzleHttp\Command\Guzzle\Parameter;
use GuzzleHttp\Psr7;
use Psr\Http\Message\RequestInterface;
/**
* Add form_params to a request
*/
class FormParamLocation extends AbstractLocation
{
/** @var string $contentType */
protected $contentType = 'application/x-www-form-urlencoded; charset=utf-8';
/** @var array $formParamsData */
protected $formParamsData = [];
/**
* Set the name of the location
*
* @param string $locationName
*/
public function __construct($locationName = 'formParam')
{
parent::__construct($locationName);
}
/**
* @param CommandInterface $command
* @param RequestInterface $request
* @param Parameter $param
*
* @return RequestInterface
*/
public function visit(
CommandInterface $command,
RequestInterface $request,
Parameter $param
) {
$this->formParamsData['form_params'][$param->getWireName()] = $this->prepareValue(
$command[$param->getName()],
$param
);
return $request;
}
/**
* @param CommandInterface $command
* @param RequestInterface $request
* @param Operation $operation
*
* @return RequestInterface
*/
public function after(
CommandInterface $command,
RequestInterface $request,
Operation $operation
) {
$data = $this->formParamsData;
$this->formParamsData = [];
$modify = [];
// Add additional parameters to the form_params array
$additional = $operation->getAdditionalParameters();
if ($additional && $additional->getLocation() == $this->locationName) {
foreach ($command->toArray() as $key => $value) {
if (!$operation->hasParam($key)) {
$data['form_params'][$key] = $this->prepareValue($value, $additional);
}
}
}
$body = http_build_query($data['form_params'], '', '&');
$modify['body'] = Psr7\stream_for($body);
$modify['set_headers']['Content-Type'] = $this->contentType;
$request = Psr7\modify_request($request, $modify);
return $request;
}
}