Skip to content

Commit

Permalink
fix: make shipmentOptions and pickupLocation properties snake_case fo…
Browse files Browse the repository at this point in the history
…r backwards compatibility (#481)

* fix(checkout): work with bbp and do6 from cdn

* fix: shipmentOptions and pickupLocation should be snake_case for backwards compatibility

* fix: implement feedback
  • Loading branch information
joerivanveen committed Apr 22, 2024
1 parent f43e0ad commit f4ec38d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
16 changes: 8 additions & 8 deletions src/Adapter/DeliveryOptions/PickupLocationV3Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ class PickupLocationV3Adapter extends AbstractPickupLocationAdapter
*/
public function __construct(array $data)
{
$this->location_name = $data["location_name"];
$this->location_code = $data["location_code"];
$this->retail_network_id = $data["retail_network_id"] ?? '';
$this->street = $data["street"];
$this->number = $data["number"];
$this->postal_code = $data["postal_code"];
$this->city = $data["city"];
$this->cc = $data["cc"];
$this->location_name = $data['location_name'];
$this->location_code = $data['location_code'];
$this->retail_network_id = $data['retail_network_id'] ?? '';
$this->street = $data['street'];
$this->number = $data['number'];
$this->postal_code = $data['postal_code'];
$this->city = $data['city'];
$this->cc = $data['cc'];
}
}
26 changes: 22 additions & 4 deletions src/Factory/DeliveryOptionsAdapterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use MyParcelNL\Sdk\src\Adapter\DeliveryOptions\DeliveryOptionsV2Adapter;
use MyParcelNL\Sdk\src\Adapter\DeliveryOptions\DeliveryOptionsV3Adapter;
use MyParcelNL\Sdk\src\Support\Arr;
use MyParcelNL\Sdk\src\Support\Str;

class DeliveryOptionsAdapterFactory
{
Expand All @@ -28,14 +29,31 @@ class DeliveryOptionsAdapterFactory
*/
public static function create(array $deliveryOptionsData): AbstractDeliveryOptionsAdapter
{
$deliveryOptionsData = Arr::fromObject($deliveryOptionsData);
/**
* To ensure backwards compatibility in consuming applications, we convert camelCase to snake_case here,
* only for shipmentOptions and pickupLocation. Everything else should remain camelCased.
*/
foreach (['shipmentOptions', 'pickupLocation'] as $item) {
if (isset($deliveryOptionsData[$item]) && is_array($deliveryOptionsData[$item])) {
foreach ($deliveryOptionsData[$item] as $key => $value) {
$snakeCasedKey = Str::snake($key);
if ($snakeCasedKey === $key) {
continue;
}
unset($deliveryOptionsData[$item][$key]);
$deliveryOptionsData[$item][$snakeCasedKey] = $value;
}
}
}

if (key_exists('time', $deliveryOptionsData) && is_array($deliveryOptionsData["time"])) {
if (array_key_exists('time', $deliveryOptionsData) && is_array($deliveryOptionsData['time'])) {
return new DeliveryOptionsV2Adapter($deliveryOptionsData);
} elseif (key_exists('deliveryType', $deliveryOptionsData)) {
}

if (array_key_exists('deliveryType', $deliveryOptionsData)) {
return new DeliveryOptionsV3Adapter($deliveryOptionsData);
}

throw new BadMethodCallException("Can't create DeliveryOptions. No suitable adapter found");
throw new BadMethodCallException('Can\'t create DeliveryOptions. No suitable adapter found');
}
}

0 comments on commit f4ec38d

Please sign in to comment.