Skip to content

Commit

Permalink
Add restCreateCollection method to enable calling a rest create metho…
Browse files Browse the repository at this point in the history
…d which returns a collection rather than a single result.

Add ParcelMultiEndpoint to allow creating multi-collo shipments.
  • Loading branch information
Ash committed Nov 15, 2023
1 parent 2bdc222 commit 97081da
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/Endpoints/AbstractEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions src/Endpoints/ParcelMultiEndpoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Imbue\SendCloud\Endpoints;

use Imbue\SendCloud\Exceptions\ApiException;
use Imbue\SendCloud\Resources\Collections\ParcelCollection;
use Imbue\SendCloud\Resources\GenericStatus;
use Imbue\SendCloud\Resources\Parcel;
use Imbue\SendCloud\Resources\ParcelMulti;
use Imbue\SendCloud\Resources\ResourceFactory;

class ParcelMultiEndpoint extends AbstractEndpoint
{
/** @var string */
protected $resourcePath = 'parcels';

/**
* @return Parcel
*/
protected function getResourceObject(): Parcel
{
return new Parcel($this->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);
}
}
4 changes: 4 additions & 0 deletions src/SendCloudApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,6 +56,8 @@ class SendCloudApiClient

/** @var ParcelEndpoint */
public $parcels;
/** @var ParcelMultiEndpoint */
public $parcelsMulti;
/** @var ParcelStatusEndpoint */
public $parcelStatuses;
/** @var ShippingMethodEndpoint */
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 97081da

Please sign in to comment.