From 97081da82c095fe96ca70bb0651e9112beefe899 Mon Sep 17 00:00:00 2001 From: Ash Date: Wed, 15 Nov 2023 19:15:17 +0000 Subject: [PATCH] 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. --- src/Endpoints/AbstractEndpoint.php | 30 ++++++++++++++++++ src/Endpoints/ParcelMultiEndpoint.php | 44 +++++++++++++++++++++++++++ src/SendCloudApiClient.php | 4 +++ 3 files changed, 78 insertions(+) create mode 100644 src/Endpoints/ParcelMultiEndpoint.php diff --git a/src/Endpoints/AbstractEndpoint.php b/src/Endpoints/AbstractEndpoint.php index 1cc91b9..00b397f 100644 --- a/src/Endpoints/AbstractEndpoint.php +++ b/src/Endpoints/AbstractEndpoint.php @@ -66,6 +66,36 @@ protected function restCreate(array $body) return ResourceFactory::createFromApiResult($result, $this->getResourceObject(), $this->getSingleResourceKey()); } + /** + * @param array $body + * @return AbstractCollection + * @throws ApiException + */ + protected function restCreateCollection(array $body) + { + $result = $this->client->performHttpCall( + self::REST_CREATE, + $this->getResourcePath(), + $this->parseRequestBody($body) + ); + + /** @var AbstractCollection $collection */ + $collection = $this->getResourceCollectionObject( + null, + null + ); + + if (is_object($result)) { + $result = $result->{$collection->getCollectionResourceName()}; + } + + foreach ($result as $dataResult) { + $collection[] = ResourceFactory::createFromApiResult($dataResult, $this->getResourceObject()); + } + + return $collection; + } + /** * @param $id * @param array $filters diff --git a/src/Endpoints/ParcelMultiEndpoint.php b/src/Endpoints/ParcelMultiEndpoint.php new file mode 100644 index 0000000..f959a08 --- /dev/null +++ b/src/Endpoints/ParcelMultiEndpoint.php @@ -0,0 +1,44 @@ +client); + } + + /** + * @param $previous + * @param $next + * @return ParcelCollection + */ + protected function getResourceCollectionObject($previous, $next): ParcelCollection + { + return new ParcelCollection($this->client, $previous, $next); + } + + /** + * @param array $data + * @return ParcelCollection + * @throws ApiException + */ + public function create(array $data = []): ParcelCollection + { + return $this->restCreateCollection($data); + } +} diff --git a/src/SendCloudApiClient.php b/src/SendCloudApiClient.php index d053b56..c379a4a 100644 --- a/src/SendCloudApiClient.php +++ b/src/SendCloudApiClient.php @@ -12,6 +12,7 @@ use Imbue\SendCloud\Endpoints\InvoiceEndpoint; use Imbue\SendCloud\Endpoints\LabelEndpoint; use Imbue\SendCloud\Endpoints\ParcelEndpoint; +use Imbue\SendCloud\Endpoints\ParcelMultiEndpoint; use Imbue\SendCloud\Endpoints\ParcelStatusEndpoint; use Imbue\SendCloud\Endpoints\SenderAddressEndpoint; use Imbue\SendCloud\Endpoints\ShippingMethodEndpoint; @@ -55,6 +56,8 @@ class SendCloudApiClient /** @var ParcelEndpoint */ public $parcels; + /** @var ParcelMultiEndpoint */ + public $parcelsMulti; /** @var ParcelStatusEndpoint */ public $parcelStatuses; /** @var ShippingMethodEndpoint */ @@ -98,6 +101,7 @@ public function __construct(ClientInterface $httpClient = null) public function initializeEndpoints() { $this->parcels = new ParcelEndpoint($this); + $this->parcelsMulti = new ParcelMultiEndpoint($this); $this->parcelStatuses = new ParcelStatusEndpoint($this); $this->shippingMethods = new ShippingMethodEndpoint($this); $this->shippingProducts = new ShippingProductsEndpoint($this);