diff --git a/Changelog.md b/Changelog.md index cd52820f..1c12e2ae 100644 --- a/Changelog.md +++ b/Changelog.md @@ -31,6 +31,9 @@ - Added the `Stockable` interface to the `Product` and `MasterProductVariant` models - Added the `backorder` field to products and product variants - BC: Added the `?CheckoutSubject` return type to the `getCart()` method of the `Checkout` interface +- BC: Changed `Checkout::getShippingAddress()` return type to be nullable +- BC: Added the void return type to `Checkout::setShippingAddress()` +- BC: Added the `removeShippingAddress()` method to the Checkout interface - BC: The unused `$config` parameter has been removed from the `RequestStore` checkout driver constructor - Fixed possible null return type on Billpayer::getName() when is_organization is true but the company name is null diff --git a/src/Checkout/Changelog.md b/src/Checkout/Changelog.md index 851a2f55..41317a5d 100644 --- a/src/Checkout/Changelog.md +++ b/src/Checkout/Changelog.md @@ -11,6 +11,9 @@ - Changed minimal Enum requirement to v4.1 - BC: Added the `?CheckoutSubject` return type to the `getCart()` method of the `Checkout` interface - BC: The unused `$config` parameter has been removed from the `RequestStore` checkout driver constructor +- BC: Changed `Checkout::getShippingAddress()` return type to be nullable +- BC: Added the void return type to `Checkout::setShippingAddress()` +- BC: Added the `removeShippingAddress()` method to the Checkout interface ## 3.x Series diff --git a/src/Checkout/CheckoutManager.php b/src/Checkout/CheckoutManager.php index 4179f67b..4a269905 100644 --- a/src/Checkout/CheckoutManager.php +++ b/src/Checkout/CheckoutManager.php @@ -97,22 +97,21 @@ public function setBillpayer(Billpayer $billpayer) return $this->store->setBillpayer($billpayer); } - /** - * @inheritdoc - */ - public function getShippingAddress(): Address + public function getShippingAddress(): ?Address { return $this->store->getShippingAddress(); } - /** - * @inheritDoc - */ - public function setShippingAddress(Address $address) + public function setShippingAddress(Address $address): void { $this->store->setShippingAddress($address); } + public function removeShippingAddress(): void + { + $this->store->removeShippingAddress(); + } + public function setCustomAttribute(string $key, $value): void { $this->store->setCustomAttribute($key, $value); diff --git a/src/Checkout/Contracts/Checkout.php b/src/Checkout/Contracts/Checkout.php index e70c1522..0449ef58 100644 --- a/src/Checkout/Contracts/Checkout.php +++ b/src/Checkout/Contracts/Checkout.php @@ -57,21 +57,11 @@ public function getBillpayer(): Billpayer; */ public function setBillpayer(Billpayer $billpayer); - /** - * Returns the shipping address - * - * @todo make this nullable in v4 - * - * @return Address - */ - public function getShippingAddress(): Address; + public function getShippingAddress(): ?Address; - /** - * Sets the shipping address - * - * @param Address $address - */ - public function setShippingAddress(Address $address); + public function setShippingAddress(Address $address): void; + + public function removeShippingAddress(): void; public function setCustomAttribute(string $key, $value): void; diff --git a/src/Checkout/Drivers/RequestStore.php b/src/Checkout/Drivers/RequestStore.php index 5de2750a..686711d4 100644 --- a/src/Checkout/Drivers/RequestStore.php +++ b/src/Checkout/Drivers/RequestStore.php @@ -45,8 +45,7 @@ class RequestStore extends BaseCheckoutStore /** @var Billpayer */ protected $billpayer; - /** @var Address */ - protected $shippingAddress; + protected ?Address $shippingAddress = null; protected ?string $shippingMethodId = null; @@ -66,6 +65,7 @@ public function __construct(CheckoutDataFactory $dataFactory, Request $request = parent::__construct($dataFactory); $this->request = $request ?? request(); $this->billpayer = $dataFactory->createBillpayer(); + /** @todo examine the request and only create one if there is one */ $this->shippingAddress = $dataFactory->createShippingAddress(); $this->taxes = new DetailedAmountDto(0); $this->shippingFees = new DetailedAmountDto(0); @@ -87,23 +87,22 @@ public function setBillpayer(Billpayer $billpayer) $this->billpayer = $billpayer; } - /** - * @inheritdoc - */ - public function getShippingAddress(): Address + public function getShippingAddress(): ?Address { return $this->shippingAddress; } - /** - * @inheritdoc - */ - public function setShippingAddress(Address $address) + public function setShippingAddress(Address $address): void { $this->shippingAddress = $address; Event::dispatch(new ShippingAddressChanged($this)); } + public function removeShippingAddress(): void + { + $this->shippingAddress = null; + } + public function getShippingAmount(): DetailedAmount { return $this->shippingFees; diff --git a/src/Checkout/Drivers/SessionStore.php b/src/Checkout/Drivers/SessionStore.php index 7c4caaec..8c5854f3 100644 --- a/src/Checkout/Drivers/SessionStore.php +++ b/src/Checkout/Drivers/SessionStore.php @@ -40,23 +40,31 @@ public function __construct(CheckoutDataFactory $factory, Session $session = nul $this->prefix = $prefix ?? static::DEFAULT_PREFIX; } - public function getShippingAddress(): Address + public function getShippingAddress(): ?Address { - $result = $this->factory->createShippingAddress(); - if (is_array($rawData = $this->readRawDataFromStore('shipping_address'))) { - $this->fill($result, $rawData); + $rawData = $this->readRawDataFromStore('shipping_address'); + if (!is_array($rawData)) { + return null; } + $result = $this->factory->createShippingAddress(); + $this->fill($result, $rawData); + return $result; } - public function setShippingAddress(Address $address) + public function setShippingAddress(Address $address): void { $this->writeRawDataToStore('shipping_address', $address); Event::dispatch(new ShippingAddressChanged($this)); } + public function removeShippingAddress(): void + { + $this->writeRawDataToStore('shipping_address', null); + } + public function getShippingAmount(): DetailedAmount { $raw = $this->readRawDataFromStore('shipping_fees', []); diff --git a/src/Checkout/Tests/Example/MemoryStore.php b/src/Checkout/Tests/Example/MemoryStore.php index a5faaaf6..98378ac4 100644 --- a/src/Checkout/Tests/Example/MemoryStore.php +++ b/src/Checkout/Tests/Example/MemoryStore.php @@ -30,7 +30,7 @@ class MemoryStore implements CheckoutStore private Billpayer $billpayer; - private Address $shippingAddress; + private ?Address $shippingAddress = null; private $customAttributes = []; @@ -76,16 +76,21 @@ public function setBillpayer(Billpayer $billpayer) $this->billpayer = $billpayer; } - public function getShippingAddress(): Address + public function getShippingAddress(): ?Address { return $this->shippingAddress; } - public function setShippingAddress(Address $address) + public function setShippingAddress(Address $address): void { $this->shippingAddress = $address; } + public function removeShippingAddress(): void + { + $this->shippingAddress = null; + } + public function setCustomAttribute(string $key, $value): void { $this->customAttributes[$key] = $value;