-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added
CheckoutRequest
interface + Checkout refactoring - IN PROG
- Loading branch information
1 parent
99911ee
commit 66e45be
Showing
5 changed files
with
174 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Contains the CheckoutRequest interface. | ||
* | ||
* @copyright Copyright (c) 2023 Vanilo UG | ||
* @author Attila Fulop | ||
* @license MIT | ||
* @since 2023-12-06 | ||
* | ||
*/ | ||
|
||
namespace Vanilo\Checkout\Contracts; | ||
|
||
use ArrayAccess; | ||
use Vanilo\Contracts\Address; | ||
|
||
interface CheckoutRequest extends ArrayAccess | ||
{ | ||
public function has($key); | ||
|
||
public function all($keys = null); | ||
|
||
public function input($key = null, $default = null); | ||
|
||
public function old($key = null, $default = null); | ||
|
||
public function replace(array $input); | ||
|
||
public function doesntWantShipping(): bool; | ||
|
||
public function wantsShipping(): bool; | ||
|
||
public function wantsShippingToBillingAddress(): bool; | ||
|
||
public function getShippingAddress(): null|array|Address; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Contains the ImplicitCheckoutRequest class. | ||
* | ||
* @copyright Copyright (c) 2023 Vanilo UG | ||
* @author Attila Fulop | ||
* @license MIT | ||
* @since 2023-12-06 | ||
* | ||
*/ | ||
|
||
namespace Vanilo\Checkout\Drivers; | ||
|
||
use Illuminate\Http\Request; | ||
use Vanilo\Checkout\Contracts\CheckoutRequest; | ||
use Vanilo\Contracts\Address; | ||
|
||
/** | ||
* This class represents a checkout request that implicitly contains | ||
* the fields required for the checkout, without implementing the | ||
* CheckoutRequest interface. It decorates the via duck-typing | ||
*/ | ||
final class ImplicitCheckoutRequest implements CheckoutRequest | ||
{ | ||
public function __construct( | ||
private Request $request, | ||
) { | ||
} | ||
|
||
public static function from(Request|CheckoutRequest $request): CheckoutRequest | ||
{ | ||
if ($request instanceof CheckoutRequest) { | ||
return $request; | ||
} | ||
|
||
return new self($request); | ||
} | ||
|
||
public function doesntWantShipping(): bool | ||
{ | ||
return (bool) $this->input('no_shipping'); | ||
} | ||
|
||
public function wantsShipping(): bool | ||
{ | ||
return !$this->doesntWantShipping(); | ||
} | ||
|
||
public function wantsShippingToBillingAddress(): bool | ||
{ | ||
return (bool) $this->input('ship_to_billing_address'); | ||
} | ||
|
||
public function getShippingAddress(): null|array|Address | ||
{ | ||
$address = $this->input('shipping_address'); | ||
|
||
return match (true) { | ||
empty($address) => null, | ||
is_array($address) => $address, | ||
default => null, | ||
}; | ||
} | ||
|
||
public function has($key) | ||
{ | ||
return $this->request->has($key); | ||
} | ||
|
||
public function all($keys = null) | ||
{ | ||
return $this->request->all($keys); | ||
} | ||
|
||
public function input($key = null, $default = null) | ||
{ | ||
return $this->request->input($key, $default); | ||
} | ||
|
||
public function old($key = null, $default = null) | ||
{ | ||
return $this->request->old($key, $default); | ||
} | ||
|
||
public function replace(array $input) | ||
{ | ||
return $this->request->replace($input); | ||
} | ||
|
||
public function offsetExists(mixed $offset): bool | ||
{ | ||
return $this->request->offsetExists($offset); | ||
} | ||
|
||
public function offsetGet(mixed $offset): mixed | ||
{ | ||
return $this->request->offsetGet($offset); | ||
} | ||
|
||
public function offsetSet(mixed $offset, mixed $value): void | ||
{ | ||
$this->request->offsetSet($offset, $value); | ||
} | ||
|
||
public function offsetUnset(mixed $offset): void | ||
{ | ||
$this->request->offsetUnset($offset); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters