diff --git a/src/MyParcelComApi.php b/src/MyParcelComApi.php index c2c89fd..ce2f17d 100644 --- a/src/MyParcelComApi.php +++ b/src/MyParcelComApi.php @@ -385,7 +385,7 @@ public function resolveDynamicServiceRates( ShipmentInterface|array $shipmentData, ?ServiceRateInterface $dynamicServiceRate = null, ): array { - $data = ($shipmentData instanceof ShipmentInterface) ? $shipmentData->jsonSerialize() : $shipmentData; + $data = ($shipmentData instanceof ShipmentInterface) ? $shipmentData->getData() : $shipmentData; if (!isset($data['relationships'])) { $data['relationships'] = []; @@ -584,7 +584,7 @@ public function createAndRegisterShipment( '/registered-shipments?' . http_build_query(['include' => Shipment::RELATIONSHIP_FILES]), 'post', [ - 'data' => $shipment, + 'data' => $shipment->getData(), 'meta' => array_filter($shipment->getMeta()), ], $this->authenticator->getAuthorizationHeader() + [ @@ -648,7 +648,7 @@ public function createAndRegisterMultiColliShipment( ]), 'post', [ - 'data' => $shipment, + 'data' => $shipment->getData(), 'meta' => array_merge( [ 'colli' => array_map( diff --git a/src/Resources/Proxy/ShipmentProxy.php b/src/Resources/Proxy/ShipmentProxy.php index 77ea8d4..5000b0c 100644 --- a/src/Resources/Proxy/ShipmentProxy.php +++ b/src/Resources/Proxy/ShipmentProxy.php @@ -40,6 +40,11 @@ class ShipmentProxy implements ShipmentInterface, ResourceProxyInterface private string $type = ResourceInterface::TYPE_SHIPMENT; + public function getData(): array + { + return $this->getResource()->getData(); + } + public function getMeta(): array { return $this->getResource()->getMeta(); diff --git a/src/Resources/Shipment.php b/src/Resources/Shipment.php index 24322f9..3e7506d 100644 --- a/src/Resources/Shipment.php +++ b/src/Resources/Shipment.php @@ -163,6 +163,22 @@ class Shipment implements ShipmentInterface /** @var callable */ private $statusHistoryCallback = null; + /** + * Prepare the data for a request to our API. This filters the read-only relationships to avoid validation errors. + */ + public function getData(): array + { + $data = $this->jsonSerialize(); + + // Remove read-only relationships. + unset($data['relationships']['colli']); + unset($data['relationships']['files']); + unset($data['relationships']['shipment_status']); + unset($data['relationships']['shipment_surcharges']); + + return $data; + } + public function getMeta(): array { return $this->meta;