wip: initial working database support

This commit is contained in:
Joel Male
2025-02-25 11:47:10 +10:00
parent 2196f58f01
commit b92dce9ca4
7 changed files with 267 additions and 53 deletions
+23 -48
View File
@@ -15,6 +15,7 @@ class Cart
protected $instanceName;
protected $customStorage = false;
protected $sessionKey;
protected $sessionKeyCartItems;
protected $sessionKeyCartConditions;
@@ -26,41 +27,17 @@ class Cart
protected $decimals;
protected $decPoint;
public function __construct($session, $events, $instanceName, $session_key, $config)
public function __construct($session, $events, $instanceName, $sessionKey, $config)
{
$this->events = $events;
$this->session = $session;
$this->session = new CartSession($session, $sessionKey, $config);
$this->instanceName = $instanceName;
$this->sessionKey = $session_key;
$this->sessionKeyCartItems = $this->sessionKey . '_cart_items';
$this->sessionKeyCartConditions = $this->sessionKey . '_cart_conditions';
$this->config = $config;
$this->currentItemId = null;
$this->fireEvent('created');
}
/**
* sets the session key
*
* @param string $sessionKey the session key or identifier
* @return $this|bool
*
* @throws \Exception
*/
public function session($sessionKey)
{
if (! $sessionKey) {
throw new \Exception('Session key is required.');
}
$this->sessionKey = $sessionKey;
$this->sessionKeyCartItems = $this->sessionKey . '_cart_items';
$this->sessionKeyCartConditions = $this->sessionKey . '_cart_conditions';
return $this;
}
/**
* get instance name of the cart
*
@@ -284,10 +261,7 @@ public function clear()
return false;
}
$this->session->put(
$this->sessionKeyCartItems,
[]
);
$this->session->putItems([]);
$this->fireEvent('cleared');
@@ -326,7 +300,9 @@ public function condition(array|CartCondition $condition): self
$conditions->put($condition->getName(), $condition);
$conditions = $conditions->sortBy(function ($condition, $key) {
$conditions = $conditions->reject(function ($condition) {
return empty($condition);
})->sortBy(function ($condition, $key) {
return $condition->getOrder();
});
@@ -342,9 +318,14 @@ public function condition(array|CartCondition $condition): self
*/
public function getConditions(bool $array = false, bool $active = false): CartConditionCollection|array
{
$conditions = new CartConditionCollection(
$this->session->get($this->sessionKeyCartConditions)
);
$conditions = new CartConditionCollection($this->session->getConditions());
$conditions = $conditions->reject(function ($condition) {
return empty($condition);
})
->transform(function ($condition) {
return $condition instanceof CartCondition ? $condition : new CartCondition($condition);
});
if ($active) {
return $conditions->filter(function (CartCondition $condition) {
@@ -488,10 +469,7 @@ public function clearItemConditions($itemId)
*/
public function clearCartConditions(): void
{
$this->session->put(
$this->sessionKeyCartConditions,
[]
);
$this->session->putConditions([]);
}
/**
@@ -653,14 +631,11 @@ public function getTotalQuantity()
return $count;
}
/**
* get the cart
*
* @return CartCollection
*/
public function getContent()
public function getContent(): CartCollection
{
return (new CartCollection($this->session->get($this->sessionKeyCartItems)))->reject(function ($item) {
return (new CartCollection($this->session->getItems()))->transform(function ($item) {
return $item instanceof ItemCollection ? $item : new ItemCollection($item, $this->config);
})->reject(function ($item) {
return ! ($item instanceof ItemCollection);
});
}
@@ -734,11 +709,11 @@ protected function addItem($id, $item)
/**
* save the cart
*
* @param $cart CartCollection
* @param $cart CartCollection
*/
protected function save($cart)
{
$this->session->put($this->sessionKeyCartItems, $cart);
$this->session->putItems($cart);
}
/**
@@ -746,7 +721,7 @@ protected function save($cart)
*/
protected function saveConditions($conditions)
{
$this->session->put($this->sessionKeyCartConditions, $conditions);
$this->session->putConditions($conditions);
}
/**
+103
View File
@@ -0,0 +1,103 @@
<?php
namespace Joelwmale\Cart;
class CartSession
{
private $customStorage;
private $session;
private $sessionId = null;
private $itemsKey = null;
private $conditionsKey = null;
public function __construct($session, $sessionKey, $config)
{
if ($this->isInternalSession($session)) {
$this->customStorage = false;
$this->session = $session;
$this->itemsKey = $sessionKey . '_cart_items';
$this->conditionsKey = $sessionKey . '_cart_conditions';
} else {
$this->customStorage = true;
// @TODO validate these 3 exist
$this->sessionId = $config['storage_id'];
$this->itemsKey = $config['storage_items'];
$this->conditionsKey = $config['storage_conditions'];
// get the base class to do findOrCreate
$this->session = $config['storage'];
$this->session = (new $this->session)->firstOrNew([$this->sessionId => $sessionKey]);
$this->session[$this->itemsKey] = $this->session[$this->itemsKey] ?? [];
$this->session[$this->conditionsKey] = $this->session[$this->conditionsKey] ?? [];
}
}
public function isInternalSession($session)
{
return in_array(is_object($session) ? get_class($session) : $session, [
'Illuminate\Session\SessionManager',
'Joelwmale\Cart\Tests\Helpers\SessionMock',
]);
}
public function has($key)
{
return $this->session->has($key) ?? $this->session;
}
public function getItems()
{
return ! $this->customStorage ? $this->session->get($this->itemsKey) : $this->session[$this->itemsKey];
}
public function getConditions()
{
return ! $this->customStorage ? $this->session->get($this->conditionsKey) : $this->session[$this->conditionsKey];
}
public function putItems($value)
{
if (! $this->customStorage) {
return $this->session->put($this->itemsKey, $value);
}
$this->session[$this->itemsKey] = $value;
$this->session->save();
return $this->session;
}
public function putConditions($value)
{
if (! $this->customStorage) {
return $this->session->put($this->conditionsKey, $value);
}
if (! empty($value)) {
$value = $value->mapWithKeys(function ($condition) {
return [$condition->getName() => $condition->toArray()];
});
}
$this->session[$this->conditionsKey] = $value;
$this->session->save();
return $this->session;
}
public function clear()
{
if ($this->customStorage) {
return $this->session->delete();
}
$this->session->forget($this->itemsKey);
$this->session->forget($this->conditionsKey);
return true;
}
}