From f4ec38d78615c16d6b4485279bb796cfb68b9f0d Mon Sep 17 00:00:00 2001 From: Joeri van Veen Date: Mon, 22 Apr 2024 14:41:48 +0200 Subject: [PATCH] fix: make shipmentOptions and pickupLocation properties snake_case for 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 --- .../PickupLocationV3Adapter.php | 16 ++++++------ src/Factory/DeliveryOptionsAdapterFactory.php | 26 ++++++++++++++++--- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/Adapter/DeliveryOptions/PickupLocationV3Adapter.php b/src/Adapter/DeliveryOptions/PickupLocationV3Adapter.php index 62419aa3..dba683fa 100644 --- a/src/Adapter/DeliveryOptions/PickupLocationV3Adapter.php +++ b/src/Adapter/DeliveryOptions/PickupLocationV3Adapter.php @@ -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']; } } diff --git a/src/Factory/DeliveryOptionsAdapterFactory.php b/src/Factory/DeliveryOptionsAdapterFactory.php index 9162e5fe..18ca54a2 100644 --- a/src/Factory/DeliveryOptionsAdapterFactory.php +++ b/src/Factory/DeliveryOptionsAdapterFactory.php @@ -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 { @@ -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'); } }