Skip to content

Commit 97081da

Browse files
author
Ash
committed
Add restCreateCollection method to enable calling a rest create method which returns a collection rather than a single result.
Add ParcelMultiEndpoint to allow creating multi-collo shipments.
1 parent 2bdc222 commit 97081da

3 files changed

Lines changed: 78 additions & 0 deletions

File tree

src/Endpoints/AbstractEndpoint.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,36 @@ protected function restCreate(array $body)
6666
return ResourceFactory::createFromApiResult($result, $this->getResourceObject(), $this->getSingleResourceKey());
6767
}
6868

69+
/**
70+
* @param array $body
71+
* @return AbstractCollection
72+
* @throws ApiException
73+
*/
74+
protected function restCreateCollection(array $body)
75+
{
76+
$result = $this->client->performHttpCall(
77+
self::REST_CREATE,
78+
$this->getResourcePath(),
79+
$this->parseRequestBody($body)
80+
);
81+
82+
/** @var AbstractCollection $collection */
83+
$collection = $this->getResourceCollectionObject(
84+
null,
85+
null
86+
);
87+
88+
if (is_object($result)) {
89+
$result = $result->{$collection->getCollectionResourceName()};
90+
}
91+
92+
foreach ($result as $dataResult) {
93+
$collection[] = ResourceFactory::createFromApiResult($dataResult, $this->getResourceObject());
94+
}
95+
96+
return $collection;
97+
}
98+
6999
/**
70100
* @param $id
71101
* @param array $filters
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Imbue\SendCloud\Endpoints;
4+
5+
use Imbue\SendCloud\Exceptions\ApiException;
6+
use Imbue\SendCloud\Resources\Collections\ParcelCollection;
7+
use Imbue\SendCloud\Resources\GenericStatus;
8+
use Imbue\SendCloud\Resources\Parcel;
9+
use Imbue\SendCloud\Resources\ParcelMulti;
10+
use Imbue\SendCloud\Resources\ResourceFactory;
11+
12+
class ParcelMultiEndpoint extends AbstractEndpoint
13+
{
14+
/** @var string */
15+
protected $resourcePath = 'parcels';
16+
17+
/**
18+
* @return Parcel
19+
*/
20+
protected function getResourceObject(): Parcel
21+
{
22+
return new Parcel($this->client);
23+
}
24+
25+
/**
26+
* @param $previous
27+
* @param $next
28+
* @return ParcelCollection
29+
*/
30+
protected function getResourceCollectionObject($previous, $next): ParcelCollection
31+
{
32+
return new ParcelCollection($this->client, $previous, $next);
33+
}
34+
35+
/**
36+
* @param array $data
37+
* @return ParcelCollection
38+
* @throws ApiException
39+
*/
40+
public function create(array $data = []): ParcelCollection
41+
{
42+
return $this->restCreateCollection($data);
43+
}
44+
}

src/SendCloudApiClient.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Imbue\SendCloud\Endpoints\InvoiceEndpoint;
1313
use Imbue\SendCloud\Endpoints\LabelEndpoint;
1414
use Imbue\SendCloud\Endpoints\ParcelEndpoint;
15+
use Imbue\SendCloud\Endpoints\ParcelMultiEndpoint;
1516
use Imbue\SendCloud\Endpoints\ParcelStatusEndpoint;
1617
use Imbue\SendCloud\Endpoints\SenderAddressEndpoint;
1718
use Imbue\SendCloud\Endpoints\ShippingMethodEndpoint;
@@ -55,6 +56,8 @@ class SendCloudApiClient
5556

5657
/** @var ParcelEndpoint */
5758
public $parcels;
59+
/** @var ParcelMultiEndpoint */
60+
public $parcelsMulti;
5861
/** @var ParcelStatusEndpoint */
5962
public $parcelStatuses;
6063
/** @var ShippingMethodEndpoint */
@@ -98,6 +101,7 @@ public function __construct(ClientInterface $httpClient = null)
98101
public function initializeEndpoints()
99102
{
100103
$this->parcels = new ParcelEndpoint($this);
104+
$this->parcelsMulti = new ParcelMultiEndpoint($this);
101105
$this->parcelStatuses = new ParcelStatusEndpoint($this);
102106
$this->shippingMethods = new ShippingMethodEndpoint($this);
103107
$this->shippingProducts = new ShippingProductsEndpoint($this);

0 commit comments

Comments
 (0)