commercetools-sdk-php-v2  master
The platform, import-api and ml-api PHP sdks generated from our api reference.
ByProjectKeyProductProjectionsGet.php
1 <?php
2 
3 declare(strict_types=1);
10 
22 use GuzzleHttp\ClientInterface;
23 use GuzzleHttp\Exception\ClientException;
24 use GuzzleHttp\Exception\RequestException;
25 use GuzzleHttp\Exception\ServerException;
26 use GuzzleHttp\Promise\PromiseInterface;
27 
28 use Psr\Http\Message\ResponseInterface;
29 
45 {
50  public function __construct(string $projectKey, $body = null, array $headers = [], ClientInterface $client = null)
51  {
52  $uri = str_replace(['{projectKey}'], [$projectKey], '{projectKey}/product-projections');
53  parent::__construct($client, 'GET', $uri, $headers, is_object($body) || is_array($body) ? json_encode($body) : $body);
54  }
55 
61  public function mapFromResponse(?ResponseInterface $response, string $resultType = null)
62  {
63  if (is_null($response)) {
64  return null;
65  }
66  if (is_null($resultType)) {
67  switch ($response->getStatusCode()) {
68  case '200':
69  $resultType = ProductProjectionPagedQueryResponseModel::class;
70 
71  break;
72  case '400':
73  $resultType = ErrorResponseModel::class;
74 
75  break;
76  case '401':
77  $resultType = ErrorResponseModel::class;
78 
79  break;
80  case '403':
81  $resultType = ErrorResponseModel::class;
82 
83  break;
84  case '500':
85  $resultType = ErrorResponseModel::class;
86 
87  break;
88  case '502':
89  $resultType = ErrorResponseModel::class;
90 
91  break;
92  case '503':
93  $resultType = ErrorResponseModel::class;
94 
95  break;
96  default:
97  $resultType = JsonObjectModel::class;
98 
99  break;
100  }
101  }
102 
103  return $resultType::of($this->responseData($response));
104  }
105 
112  public function execute(array $options = [], string $resultType = null)
113  {
114  try {
115  $response = $this->send($options);
116  } catch (ServerException $e) {
117  $response = $e->getResponse();
118  $e = ExceptionFactory::createServerException($e, $this, $response, $this->mapFromResponse($response, $resultType));
119  throw $e;
120  } catch (ClientException $e) {
121  $response = $e->getResponse();
122  $e = ExceptionFactory::createClientException($e, $this, $response, $this->mapFromResponse($response, $resultType));
123  throw $e;
124  }
125 
126  return $this->mapFromResponse($response, $resultType);
127  }
128 
135  public function executeAsync(array $options = [], string $resultType = null)
136  {
137  return $this->sendAsync($options)->then(
138  function (ResponseInterface $response) use ($resultType) {
139  return $this->mapFromResponse($response, $resultType);
140  },
141  function (RequestException $e) use ($resultType) {
142  $response = $e->getResponse();
143  if ($e instanceof ServerException) {
144  $e = ExceptionFactory::createServerException($e, $this, $response, $this->mapFromResponse($response, $resultType));
145  }
146  if ($e instanceof ClientException) {
147  $e = ExceptionFactory::createClientException($e, $this, $response, $this->mapFromResponse($response, $resultType));
148  }
149  throw $e;
150  }
151  );
152  }
153 
159  {
160  return $this->withQueryParam('staged', $staged);
161  }
162 
167  public function withPriceCurrency($priceCurrency): ByProjectKeyProductProjectionsGet
168  {
169  return $this->withQueryParam('priceCurrency', $priceCurrency);
170  }
171 
176  public function withPriceCountry($priceCountry): ByProjectKeyProductProjectionsGet
177  {
178  return $this->withQueryParam('priceCountry', $priceCountry);
179  }
180 
185  public function withPriceCustomerGroup($priceCustomerGroup): ByProjectKeyProductProjectionsGet
186  {
187  return $this->withQueryParam('priceCustomerGroup', $priceCustomerGroup);
188  }
189 
194  public function withPriceChannel($priceChannel): ByProjectKeyProductProjectionsGet
195  {
196  return $this->withQueryParam('priceChannel', $priceChannel);
197  }
198 
203  public function withLocaleProjection($localeProjection): ByProjectKeyProductProjectionsGet
204  {
205  return $this->withQueryParam('localeProjection', $localeProjection);
206  }
207 
212  public function withStoreProjection($storeProjection): ByProjectKeyProductProjectionsGet
213  {
214  return $this->withQueryParam('storeProjection', $storeProjection);
215  }
216 
222  {
223  return $this->withQueryParam('expand', $expand);
224  }
225 
231  {
232  return $this->withQueryParam('sort', $sort);
233  }
234 
240  {
241  return $this->withQueryParam('limit', $limit);
242  }
243 
249  {
250  return $this->withQueryParam('offset', $offset);
251  }
252 
258  {
259  return $this->withQueryParam('withTotal', $withTotal);
260  }
261 
267  {
268  return $this->withQueryParam('where', $where);
269  }
270 
275  public function withPredicateVar(string $varName, $predicateVar): ByProjectKeyProductProjectionsGet
276  {
277  return $this->withQueryParam(sprintf('var.%s', $varName), $predicateVar);
278  }
279 }
mapFromResponse(?ResponseInterface $response, string $resultType=null)
__construct(string $projectKey, $body=null, array $headers=[], ClientInterface $client=null)
responseData(ResponseInterface $response)
Definition: ApiRequest.php:145
withQueryParam(string $parameterName, $value)
Definition: ApiRequest.php:77
sendAsync(array $options=[])
Definition: ApiRequest.php:129
static createClientException(ClientException $e, ApiRequest $request, ?ResponseInterface $response, ?JsonObject $result)
static createServerException(ServerException $e, ApiRequest $request, ?ResponseInterface $response, ?JsonObject $result)