Skip to content

Commit 61ca57b

Browse files
committed
Modified response data, added travis, modified README
1 parent 5af4754 commit 61ca57b

17 files changed

Lines changed: 266 additions & 116 deletions

.travis.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
language: php
2+
3+
branches:
4+
only:
5+
- master
6+
7+
matrix:
8+
include:
9+
- php: 7.2
10+
dist: trusty
11+
sudo: required
12+
- php: 7.3
13+
dist: trusty
14+
sudo: required
15+
- php: hhvm
16+
dist: trusty
17+
sudo: required
18+
- php: nightly
19+
dist: trusty
20+
sudo: required
21+
22+
allow_failures:
23+
- php: hhvm
24+
- php: nightly
25+
26+
cache:
27+
directories:
28+
- $HOME/.composer/cache
29+
30+
install:
31+
- COMPOSER_MEMORY_LIMIT=-1 travis_retry composer install

README.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
<img src="https://i.imgur.com/kEZU7HH.png" />
44

5+
> Note that this library does not yet implement the complete functionality of the SendCloud API. Feel free to open a merge request with the additional implementation.
6+
57
## Installation
68

79
```
@@ -26,8 +28,33 @@ $sendCloud->setApiAuth('gb3iogpp8uf74p92holav67ij7jmpswe', '1m9mtv4ylnd8fy0xb61u
2628
Creating a new parcel
2729

2830
```php
29-
$payment = $sendcloud->parcels->create([
30-
'name' => 'Julie Appleseed',
31+
$parcel = $sendCloud->parcels->create([
32+
'parcel' => [
33+
'name' => 'Julie Appleseed',
34+
'company_name' => 'SendCloud',
35+
'address' => 'Insulindelaan 115',
36+
'house_number' => 115,
37+
'city' => 'Eindhoven',
38+
'postal_code' => '5642CV',
39+
'telephone' => '+31612345678',
40+
'request_label' => true,
41+
'email' => 'julie@appleseed.com',
42+
'country' => 'NL',
43+
'shipment' => [
44+
'id' => 8,
45+
],
46+
'weight' => '10.000',
47+
'order_number' => '1234567890',
48+
'insured_value' => 2000,
49+
]
50+
]);
51+
```
52+
53+
Insert or update (upsert) shipment for an integration
54+
55+
```php
56+
$shipment = $sendCloud->integrationShipments->upsert(1346, [
57+
'name' => 'Julie Appleseed',
3158
'company_name' => 'SendCloud',
3259
'address' => 'Insulindelaan 115',
3360
'house_number' => 115,
@@ -45,3 +72,15 @@ $payment = $sendcloud->parcels->create([
4572
'insured_value' => 2000,
4673
]);
4774
```
75+
76+
##### Partner ID
77+
78+
Set the Partner ID
79+
80+
```php
81+
$sendCloud->setPartnerId('3dd88a04-26e4-4959-af11-f5674491573e')
82+
```
83+
84+
## Want to help improving the library?
85+
86+
I will happily accept new [pull requests](https://github.com/imbue/sendcloud-api-php/pulls).

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "imbue/sendcloud-api-php",
3-
"description": "SendCloud API client library for PHP. SendCloud is a European shipping software for e-commerce.",
3+
"description": "SendCloud API client library for PHP. SendCloud is a European shipping software for e-commerce",
44
"keywords": [
55
"sendcloud",
66
"php",

src/Endpoints/AbstractEndpoint.php

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ abstract class AbstractEndpoint
2222
/** @var string */
2323
protected $resourcePath;
2424
/** @var string */
25+
protected $singleResourceKey;
26+
/** @var string */
27+
protected $listResourceKey;
28+
/** @var string */
2529
protected $parentId;
2630

2731
/**
@@ -43,15 +47,6 @@ protected function buildQueryString(array $filters)
4347
return '';
4448
}
4549

46-
foreach ($filters as $key => $value) {
47-
if ($value === true) {
48-
$filters[$key] = 'true';
49-
}
50-
if ($value === false) {
51-
$filters[$key] = 'false';
52-
}
53-
}
54-
5550
return '?' . http_build_query($filters, '', '&');
5651
}
5752

@@ -68,7 +63,7 @@ protected function restCreate(array $body)
6863
$this->parseRequestBody($body)
6964
);
7065

71-
return ResourceFactory::createFromApiResult($result, $this->getResourceObject());
66+
return ResourceFactory::createFromApiResult($result, $this->getResourceObject(), $this->getSingleResourceKey());
7267
}
7368

7469
/**
@@ -77,7 +72,7 @@ protected function restCreate(array $body)
7772
* @return AbstractResource
7873
* @throws ApiException
7974
*/
80-
protected function restRead($id, array $filters)
75+
protected function restRead($id, array $filters): AbstractResource
8176
{
8277
if (empty($id)) {
8378
throw new ApiException('Invalid resource id.');
@@ -90,7 +85,7 @@ protected function restRead($id, array $filters)
9085
"{$this->getResourcePath()}/{$id}" . $this->buildQueryString($filters)
9186
);
9287

93-
return ResourceFactory::createFromApiResult($result, $this->getResourceObject());
88+
return ResourceFactory::createFromApiResult($result, $this->getResourceObject(), $this->getSingleResourceKey());
9489
}
9590

9691
/**
@@ -99,7 +94,7 @@ protected function restRead($id, array $filters)
9994
* @return AbstractResource|null
10095
* @throws ApiException
10196
*/
102-
protected function restDelete($id, array $body = [])
97+
protected function restDelete($id, array $body = []): AbstractResource
10398
{
10499
if (empty($id)) {
105100
throw new ApiException('Invalid resource id.');
@@ -122,10 +117,10 @@ protected function restDelete($id, array $body = [])
122117

123118
/**
124119
* @param array $filters
125-
* @return array|AbstractCollection
120+
* @return AbstractCollection
126121
* @throws ApiException
127122
*/
128-
protected function restList(array $filters = [])
123+
protected function restList(array $filters = []): AbstractCollection
129124
{
130125
$apiPath = $this->getResourcePath();
131126

@@ -135,33 +130,29 @@ protected function restList(array $filters = [])
135130

136131
$result = $this->client->performHttpCall(self::REST_LIST, $apiPath);
137132

138-
$collection = null;
133+
$previous = null;
134+
$next = null;
139135

140-
if (is_object($result)) {
141-
$previous = null;
142-
$next = null;
136+
if (isset($result->previous)) {
137+
$previous = $result->previous;
138+
}
143139

144-
if (isset($result->previous)) {
145-
$previous = $result->previous;
146-
}
140+
if (isset($result->next)) {
141+
$next = $result->next;
142+
}
147143

148-
if (isset($result->next)) {
149-
$next = $result->next;
150-
}
144+
/** @var AbstractCollection $collection */
145+
$collection = $this->getResourceCollectionObject(
146+
$previous,
147+
$next
148+
);
151149

152-
/** @var AbstractCollection $collection */
153-
$collection = $this->getResourceCollectionObject(
154-
$previous,
155-
$next
156-
);
150+
if (is_object($result)) {
151+
$result = $result->{$collection->getCollectionResourceName()};
152+
}
157153

158-
foreach ($result->{$collection->getCollectionResourceName()} as $dataResult) {
159-
$collection[] = ResourceFactory::createFromApiResult($dataResult, $this->getResourceObject());
160-
}
161-
} else {
162-
foreach ($result as $dataResult) {
163-
$collection[] = ResourceFactory::createFromApiResult($dataResult, $this->getResourceObject());
164-
}
154+
foreach ($result as $dataResult) {
155+
$collection[] = ResourceFactory::createFromApiResult($dataResult, $this->getResourceObject());
165156
}
166157

167158
return $collection;
@@ -196,6 +187,22 @@ public function getResourcePath()
196187
return $this->resourcePath;
197188
}
198189

190+
/**
191+
* @return string
192+
*/
193+
protected function getSingleResourceKey()
194+
{
195+
return $this->singleResourceKey;
196+
}
197+
198+
/**
199+
* @return string
200+
*/
201+
protected function getListResourceKey()
202+
{
203+
return $this->listResourceKey;
204+
}
205+
199206
/**
200207
* @param array $body
201208
* @return null|string
@@ -206,8 +213,9 @@ protected function parseRequestBody(array $body)
206213
if (empty($body)) {
207214
return null;
208215
}
216+
209217
try {
210-
$encoded = json_encode($body);
218+
$encoded = \GuzzleHttp\json_encode($body);
211219
} catch (InvalidArgumentException $e) {
212220
throw new ApiException("Error encoding parameters into JSON: '" . $e->getMessage() . "'");
213221
}

src/Endpoints/IntegrationEndpoint.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Imbue\SendCloud\Endpoints;
44

55
use Imbue\SendCloud\Exceptions\ApiException;
6-
use Imbue\SendCloud\Resources\Collections\AbstractCollection;
6+
use Imbue\SendCloud\Resources\Collections\IntegrationCollection;
77
use Imbue\SendCloud\Resources\Integration;
88

99
class IntegrationEndpoint extends AbstractEndpoint
@@ -20,11 +20,19 @@ protected function getResourceObject(): Integration
2020
}
2121

2222
/**
23-
* @return array|AbstractCollection
23+
* @return IntegrationCollection
24+
*/
25+
protected function getResourceCollectionObject(): IntegrationCollection
26+
{
27+
return new IntegrationCollection();
28+
}
29+
30+
/**
31+
* @return IntegrationCollection
2432
* @throws ApiException
2533
*/
2634
public function list()
2735
{
28-
return $this->restList();
36+
return $this->restList([]);
2937
}
3038
}

src/Endpoints/InvoiceEndpoint.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Imbue\SendCloud\Endpoints;
44

55
use Imbue\SendCloud\Exceptions\ApiException;
6-
use Imbue\SendCloud\Resources\Collections\AbstractCollection;
6+
use Imbue\SendCloud\Resources\Collections\InvoiceCollection;
77
use Imbue\SendCloud\Resources\Invoice;
88

99
class InvoiceEndpoint extends AbstractEndpoint
@@ -20,7 +20,15 @@ protected function getResourceObject(): Invoice
2020
}
2121

2222
/**
23-
* @return array|AbstractCollection
23+
* @return InvoiceCollection
24+
*/
25+
protected function getResourceCollectionObject(): InvoiceCollection
26+
{
27+
return new InvoiceCollection();
28+
}
29+
30+
/**
31+
* @return InvoiceCollection
2432
* @throws ApiException
2533
*/
2634
public function list()
@@ -29,11 +37,12 @@ public function list()
2937
}
3038

3139
/**
32-
* @return array|AbstractCollection
40+
* @param $id
41+
* @return Invoice
3342
* @throws ApiException
3443
*/
35-
public function get()
44+
public function get($id)
3645
{
37-
return $this->restList([]);
46+
return $this->restRead($id, []);
3847
}
3948
}

src/Endpoints/ParcelEndpoint.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
namespace Imbue\SendCloud\Endpoints;
44

55
use Imbue\SendCloud\Exceptions\ApiException;
6-
use Imbue\SendCloud\Resources\Collections\AbstractCollection;
76
use Imbue\SendCloud\Resources\Collections\ParcelCollection;
87
use Imbue\SendCloud\Resources\Parcel;
98

109
class ParcelEndpoint extends AbstractEndpoint
1110
{
1211
/** @var string */
1312
protected $resourcePath = 'parcels';
13+
/** @var string */
14+
protected $singleResourceKey = 'parcel';
1415

1516
/**
1617
* @return Parcel
@@ -31,11 +32,31 @@ protected function getResourceCollectionObject($previous, $next): ParcelCollecti
3132
}
3233

3334
/**
34-
* @return array|AbstractCollection
35+
* @param $id
36+
* @return Parcel
37+
* @throws ApiException
38+
*/
39+
public function get($id)
40+
{
41+
return $this->restRead($id, []);
42+
}
43+
44+
/**
45+
* @return ParcelCollection
3546
* @throws ApiException
3647
*/
3748
public function list()
3849
{
3950
return $this->restList();
4051
}
52+
53+
/**
54+
* @param array $data
55+
* @return Parcel
56+
* @throws ApiException
57+
*/
58+
public function create(array $data = [])
59+
{
60+
return $this->restCreate($data);
61+
}
4162
}

0 commit comments

Comments
 (0)