diff --git a/composer.json b/composer.json index 6508451c..03a460ca 100755 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "myparcelnl/sdk", - "version": "v2.1.0-beta.3", + "version": "v2.1.0-beta.4", "description": "This package is designed to send and receive data from MyParcel by means of an API.", "homepage": "https://www.myparcel.nl", "tags": ["MyParcel", "My Parcel", "Flespakket", "Post NL", "PostNL"], diff --git a/src/Adapter/ConsignmentAdapter.php b/src/Adapter/ConsignmentAdapter.php index c9adb388..1e4782b0 100644 --- a/src/Adapter/ConsignmentAdapter.php +++ b/src/Adapter/ConsignmentAdapter.php @@ -24,14 +24,16 @@ class ConsignmentAdapter /** * ConsignmentDecode constructor. + * * @param array $data - * @param string $apiKey + * @param MyParcelConsignment $consignment + * * @throws \Exception */ - public function __construct($data, $apiKey) + public function __construct($data, $consignment) { $this->data = $data; - $this->consignment = (new MyParcelConsignment())->setApiKey($apiKey); + $this->consignment = $consignment; $this ->baseOptions() @@ -67,8 +69,8 @@ private function baseOptions() ->setPostalCode($recipient['postal_code']) ->setStreet($recipient['street']) ->setCity($recipient['city']) - ->setEmail($recipient['email']) - ->setPhone($recipient['phone']) + ->setEmail( isset($recipient['email']) ? $recipient['email'] : '') + ->setPhone( isset($recipient['phone']) ? $recipient['phone'] : '') ->setPackageType($options['package_type']) ->setLabelDescription(isset($options['label_description']) ? $options['label_description'] : '') ; diff --git a/src/Helper/MyParcelCollection.php b/src/Helper/MyParcelCollection.php index a1c66ebf..94516c9b 100644 --- a/src/Helper/MyParcelCollection.php +++ b/src/Helper/MyParcelCollection.php @@ -175,6 +175,46 @@ public function addConsignment(MyParcelConsignment $consignment) return $this; } + /** + * @param int[] $ids + * @param sting $apiKey + * + * @return self + * @throws \Exception + */ + public function addConsignmentByConsignmentIds($ids, $apiKey) + { + foreach ($ids as $consignmentId) { + $consignment = (new MyParcelConsignment()) + ->setApiKey($apiKey) + ->setMyParcelConsignmentId($consignmentId); + + $this->addConsignment($consignment); + } + + return $this; + } + + /** + * @param string[] $ids + * @param string $apiKey + * + * @return self + * @throws \Exception + */ + public function addConsignmentByReferenceIds($ids, $apiKey) + { + foreach ($ids as $referenceId) { + $consignment = (new MyParcelConsignment()) + ->setApiKey($apiKey) + ->setReferenceId($referenceId); + + $this->addConsignment($consignment); + } + + return $this; + } + /** * @param MyParcelConsignment $consignment * @param $amount @@ -185,9 +225,11 @@ public function addMultiCollo(MyParcelConsignment $consignment, $amount): self { $i = 1; - $consignment->setMultiCollo(); + if ($amount > 1) { + $consignment->setMultiCollo(); + } - if (null == $consignment->getReferenceId()) { + if ($consignment->isPartOfMultiCollo() && null == $consignment->getReferenceId()) { $consignment->setReferenceId('random_multi_collo_' . uniqid()); } @@ -320,7 +362,7 @@ public function setLatestDataWithoutIds($key, $size = 300) } foreach ($request->getResult()['data']['shipments'] as $shipment) { - $consignmentAdapter = new ConsignmentAdapter($shipment, $key); + $consignmentAdapter = new ConsignmentAdapter($shipment, (new MyParcelConsignment())->setApiKey($key)); $this->addConsignment($consignmentAdapter->getConsignment()); } @@ -545,7 +587,7 @@ public function getUserAgent() * @param string $platform * @param string $version * @internal param string $user_agent - * @return $this + * @return self */ public function setUserAgent($platform, $version = null) { @@ -603,14 +645,14 @@ private function getNewCollectionFromResult($result) $consignments = $this->getConsignmentsByReferenceId($shipment['reference_identifier']); } - $consignmentAdapter = new ConsignmentAdapter($shipment, $consignments->first()->getApiKey()); + $consignmentAdapter = new ConsignmentAdapter($shipment, $consignments->first()); $isMultiCollo = ! empty($shipment['secondary_shipments']); $newCollection->addConsignment($consignmentAdapter->getConsignment()->setMultiCollo($isMultiCollo)); foreach ($shipment['secondary_shipments'] as $secondaryShipment) { $secondaryShipment = Arr::arrayMergeRecursiveDistinct($shipment, $secondaryShipment); - $consignmentAdapter = new ConsignmentAdapter($secondaryShipment, $this->getConsignmentsByReferenceId($secondaryShipment['reference_identifier'])); + $consignmentAdapter = new ConsignmentAdapter($secondaryShipment, $this->getConsignmentsByReferenceId($secondaryShipment['reference_identifier'])->first()); $newCollection->addConsignment($consignmentAdapter->getConsignment()->setMultiCollo($isMultiCollo)); } diff --git a/src/Model/MyParcelConsignment.php b/src/Model/MyParcelConsignment.php index 74d9b67f..42a0151a 100755 --- a/src/Model/MyParcelConsignment.php +++ b/src/Model/MyParcelConsignment.php @@ -381,9 +381,14 @@ public function getApiKey() * @param string $apiKey * * @return $this + * @throws \Exception */ public function setApiKey($apiKey) { + if (! is_string($apiKey)) { + throw new \Exception('Api key need to be a type of sting'); + } + $this->api_key = $apiKey; return $this;