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 2 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'];
}
}
1 change: 0 additions & 1 deletion src/Adapter/DeliveryOptions/ShipmentOptionsV3Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class ShipmentOptionsV3Adapter extends AbstractShipmentOptionsAdapter
public function __construct(array $shipmentOptions)
{
$this->age_check = $shipmentOptions['age_check'] ?? null;
$this->extra_assurance = $shipmentOptions['extra_assurance'] ?? null;
joerivanveen marked this conversation as resolved.
Show resolved Hide resolved
$this->hide_sender = $shipmentOptions['hide_sender'] ?? null;
$this->insurance = $shipmentOptions['insurance'] ?? null;
$this->label_description = $shipmentOptions['label_description'] ?? null;
Expand Down
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');
}
}
Loading