Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make shipmentOptions and pickupLocation properties snake_case for backwards compatibility #481

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
*/
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;

Check warning on line 44 in src/Factory/DeliveryOptionsAdapterFactory.php

View check run for this annotation

Codecov / codecov/patch

src/Factory/DeliveryOptionsAdapterFactory.php#L43-L44

Added lines #L43 - L44 were not covered by tests
}
}
}

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');
}
}
Loading