commercetools-sdk-php-v2
The commercetools platform, import-api and PHP sdks generated from our api reference.
Loading...
Searching...
No Matches
ApiRequest.php
1<?php
2
3declare(strict_types=1);
10namespace Commercetools\Client;
11
13use GuzzleHttp\ClientInterface;
14use GuzzleHttp\Exception\GuzzleException;
15use GuzzleHttp\Promise\PromiseInterface;
16use GuzzleHttp\Psr7\Query;
17use GuzzleHttp\Psr7\Request;
18use Psr\Http\Message\ResponseInterface;
19use stdClass;
20
22class ApiRequest extends Request implements ApiRequestInterface
23{
25 private $queryParts;
27 private $query;
28
33 private $client;
34
39 public function __construct(?ClientInterface $client, string $method, string $uri, array $headers = [], $body = null, string $version = '1.1')
40 {
41 $this->client = $client;
42 $headers = $this->ensureHeader($headers, 'Content-Type', 'application/json');
43
44 parent::__construct($method, $uri, $headers, $body, $version);
45 }
46
53 protected static function encodePathParam(string $value): string
54 {
55 if ($value === '.' || $value === '..') {
56 throw new InvalidArgumentException('Path parameter must not be a dot segment');
57 }
58
59 return urlencode($value);
60 }
61
71 protected function ensureHeader(array $headers, string $header, $defaultValue): array
72 {
73 $normalizedHeader = strtolower($header);
74 foreach (array_keys($headers) as $headerName) {
75 $normalized = strtolower($headerName);
76 if ($normalized !== $normalizedHeader) {
77 continue;
78 }
79 return $headers;
80 }
81 $headers[$header] = $defaultValue;
82
83 return $headers;
84 }
85
92 public function withQueryParam(string $parameterName, $value): ApiRequestInterface
93 {
94 $query = $this->getUri()->getQuery();
95 if ($this->query !== $query) {
97 $this->queryParts = array_map(
102 function ($value): array {
103 if (is_array($value)) {
104 return $value;
105 }
106 return [$value];
107 },
108 Query::parse($query)
109 );
110 }
111 if (is_array($value)) {
112 foreach ($value as $v) {
113 $this->queryParts[$parameterName][] = $v;
114 }
115 } else {
116 $this->queryParts[$parameterName][] = $value;
117 }
118 ksort($this->queryParts);
119 $this->query = Query::build($this->queryParts);
120
121 return $this->withUri($this->getUri()->withQuery($this->query));
122 }
123
130 public function send(array $options = []): ResponseInterface
131 {
132 if (is_null($this->client)) {
133 throw new InvalidArgumentException('No http client set to send the request');
134 }
135 return $this->client->send($this, $options);
136 }
137
144 public function sendAsync(array $options = []): PromiseInterface
145 {
146 if (is_null($this->client)) {
147 throw new InvalidArgumentException('No http client set to send the request');
148 }
149 return $this->client->sendAsync($this, $options);
150 }
151
152 public function getClient(): ?ClientInterface
153 {
154 return $this->client;
155 }
156
160 final protected function responseData(ResponseInterface $response)
161 {
162 $body = (string)$response->getBody();
164 $data = json_decode($body);
165 if (is_null($data)) {
166 return new stdClass();
167 }
168 return $data;
169 }
170}
responseData(ResponseInterface $response)
ensureHeader(array $headers, string $header, $defaultValue)
__construct(?ClientInterface $client, string $method, string $uri, array $headers=[], $body=null, string $version='1.1')
withQueryParam(string $parameterName, $value)
static encodePathParam(string $value)