Skip to content

Commit d00e979

Browse files
authored
Merge pull request #48 from kleiram/main
Add support for query options when inserting, updating or deleting models
2 parents 119d369 + ebc83fa commit d00e979

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

src/Picqer/Carriers/SendCloud/Connection.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,14 @@ public function get($url, $params = []): array
9898
* Perform a POST request
9999
* @param string $url
100100
* @param mixed $body
101+
* @param array $query
101102
* @return array
102103
* @throws SendCloudApiException
103104
*/
104-
public function post($url, $body): array
105+
public function post($url, $body, $query = []): array
105106
{
106107
try {
107-
$result = $this->client()->post($url, ['body' => $body]);
108+
$result = $this->client()->post($url, ['body' => $body, 'query' => $query]);
108109
return $this->parseResponse($result);
109110
} catch (RequestException $e) {
110111
if ($e->hasResponse()) {
@@ -119,13 +120,14 @@ public function post($url, $body): array
119120
* Perform PUT request
120121
* @param string $url
121122
* @param mixed $body
123+
* @param array $query
122124
* @return array
123125
* @throws SendCloudApiException
124126
*/
125-
public function put($url, $body): array
127+
public function put($url, $body, $query = []): array
126128
{
127129
try {
128-
$result = $this->client()->put($url, ['body' => $body]);
130+
$result = $this->client()->put($url, ['body' => $body, 'query' => $query]);
129131
return $this->parseResponse($result);
130132
} catch (RequestException $e) {
131133
if ($e->hasResponse()) {
@@ -139,13 +141,14 @@ public function put($url, $body): array
139141
/**
140142
* Perform DELETE request
141143
* @param string $url
144+
* @param array $query
142145
* @return array
143146
* @throws SendCloudApiException
144147
*/
145-
public function delete($url)
148+
public function delete($url, $query = [])
146149
{
147150
try {
148-
$result = $this->client()->delete($url);
151+
$result = $this->client()->delete($url, ['query' => $query]);
149152
return $this->parseResponse($result);
150153
} catch (RequestException $e) {
151154
if ($e->hasResponse()) {
@@ -171,10 +174,15 @@ public function parseResponse(ResponseInterface $response)
171174
$resultArray = json_decode($responseBody, true);
172175

173176
if (! is_array($resultArray)) {
174-
throw new SendCloudApiException(sprintf('SendCloud error %s: %s', $response->getStatusCode(), $responseBody), $response->getStatusCode());
177+
throw new SendCloudApiException(sprintf(
178+
'SendCloud error %s: %s',
179+
$response->getStatusCode(),
180+
$responseBody
181+
), $response->getStatusCode());
175182
}
176183

177-
if (array_key_exists('error', $resultArray)
184+
if (
185+
array_key_exists('error', $resultArray)
178186
&& is_array($resultArray['error'])
179187
&& array_key_exists('message', $resultArray['error'])
180188
) {
@@ -231,4 +239,3 @@ public function download($url, array $headers = ['Accept' => 'application/pdf'])
231239
return $result->getBody()->getContents();
232240
}
233241
}
234-

src/Picqer/Carriers/SendCloud/Persistance/Storable.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,29 @@
1313
*/
1414
trait Storable
1515
{
16-
public function save()
16+
public function save($options = [])
1717
{
1818
if ($this->exists()) {
19-
$this->fill($this->update());
19+
$this->fill($this->update($options));
2020
} else {
21-
$this->fill($this->insert());
21+
$this->fill($this->insert($options));
2222
}
2323

2424
return $this;
2525
}
2626

27-
public function insert()
27+
public function insert(array $options = [])
2828
{
29-
return $this->connection()->post($this->url, $this->json());
29+
return $this->connection()->post($this->url, $this->json(), $options);
3030
}
3131

32-
public function update()
32+
public function update(array $options = [])
3333
{
34-
return $this->connection()->put($this->url . '/' . urlencode($this->id), $this->json());
34+
return $this->connection()->put($this->url . '/' . urlencode($this->id), $this->json(), $options);
3535
}
3636

37-
public function delete()
37+
public function delete(array $options = [])
3838
{
39-
return $this->connection()->delete($this->url . '/' . urlencode($this->id));
39+
return $this->connection()->delete($this->url . '/' . urlencode($this->id), $options);
4040
}
41-
}
41+
}

0 commit comments

Comments
 (0)