Completed
Push — master ( ad34f0...67a90e )
by Stefano
32s
created

src/RequestLocation/FormParamLocation.php (1 issue)

reflection_usage.get_name

Bug Minor

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace GuzzleHttp\Command\Guzzle\RequestLocation;
3
4
use GuzzleHttp\Command\CommandInterface;
5
use GuzzleHttp\Command\Guzzle\Operation;
6
use GuzzleHttp\Command\Guzzle\Parameter;
7
use GuzzleHttp\Psr7;
8
use Psr\Http\Message\RequestInterface;
9
10
/**
11
 * Add form_params to a request
12
 */
13
class FormParamLocation extends AbstractLocation
14
{
15
    /** @var string $contentType */
16
    protected $contentType = 'application/x-www-form-urlencoded; charset=utf-8';
17
18
    /** @var array $formParamsData */
19
    protected $formParamsData = [];
20
21
    /**
22
     * Set the name of the location
23
     *
24
     * @param string $locationName
25
     */
26 2
    public function __construct($locationName = 'formParam')
27
    {
28 2
        parent::__construct($locationName);
29 2
    }
30
31
    /**
32
     * @param CommandInterface $command
33
     * @param RequestInterface $request
34
     * @param Parameter        $param
35
     *
36
     * @return RequestInterface
37
     */
38 2
    public function visit(
39
        CommandInterface $command,
40
        RequestInterface $request,
41
        Parameter $param
42
    ) {
43 2
        $this->formParamsData['form_params'][$param->getWireName()] = $this->prepareValue(
44 2
            $command[$param->getName()],
0 ignored issues
show
Consider using $param->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
45
            $param
46 2
        );
47
48 2
        return $request;
49
    }
50
51
    /**
52
     * @param CommandInterface $command
53
     * @param RequestInterface $request
54
     * @param Operation        $operation
55
     *
56
     * @return RequestInterface
57
     */
58 2
    public function after(
59
        CommandInterface $command,
60
        RequestInterface $request,
61
        Operation $operation
62
    ) {
63 2
        $data = $this->formParamsData;
64 2
        $this->formParamsData = [];
65 2
        $modify = [];
66
67
        // Add additional parameters to the form_params array
68 2
        $additional = $operation->getAdditionalParameters();
69 2 View Code Duplication
        if ($additional && $additional->getLocation() == $this->locationName) {
70 1
            foreach ($command->toArray() as $key => $value) {
71 1
                if (!$operation->hasParam($key)) {
72 1
                    $data['form_params'][$key] = $this->prepareValue($value, $additional);
73 1
                }
74 1
            }
75 1
        }
76
77 2
        $body = http_build_query($data['form_params'], '', '&');
78 2
        $modify['body'] = Psr7\stream_for($body);
79 2
        $modify['set_headers']['Content-Type'] = $this->contentType;
80 2
        $request = Psr7\modify_request($request, $modify);
81
82 2
        return $request;
83
    }
84
}
85