Update Cart.php

This commit is contained in:
Patrick
2022-02-03 00:14:15 +01:00
committed by GitHub
parent 06a828208b
commit 5e6adedd70

View File

@@ -3,6 +3,7 @@
namespace Gloudemans\Shoppingcart; namespace Gloudemans\Shoppingcart;
use Carbon\Carbon; use Carbon\Carbon;
use Money\Money;
use Closure; use Closure;
use Gloudemans\Shoppingcart\Contracts\Buyable; use Gloudemans\Shoppingcart\Contracts\Buyable;
use Gloudemans\Shoppingcart\Contracts\InstanceIdentifier; use Gloudemans\Shoppingcart\Contracts\InstanceIdentifier;
@@ -33,24 +34,18 @@ class Cart
/** /**
* Holds the current cart instance. * Holds the current cart instance.
*
* @var string
*/ */
private string $instance; private string $instance;
/** /**
* Holds the creation date of the cart. * Holds the creation date of the cart.
*
* @var mixed
*/ */
private $createdAt; private ?Carbon $createdAt = null;
/** /**
* Holds the update date of the cart. * Holds the update date of the cart.
*
* @var mixed
*/ */
private $updatedAt; private ?Carbon $updatedAt = null;
/** /**
* Defines the discount percentage. * Defines the discount percentage.
@@ -316,61 +311,31 @@ class Cart
/** /**
* Get the total price of the items in the cart. * Get the total price of the items in the cart.
*/ */
public function totalFloat(): float public function totalFloat(): Money
{ {
return $this->getContent()->reduce(function ($total, CartItem $cartItem) { return $this->getContent()->reduce(function (Money $total, CartItem $cartItem) {
return $total + $cartItem->total; return $total->add($cartItem->total);
}, 0); }, new Money());
} }
/**
* Get the total price of the items in the cart as formatted string.
*/
public function total(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null): string
{
return $this->numberFormat($this->totalFloat(), $decimals, $decimalPoint, $thousandSeperator);
}
/** /**
* Get the total tax of the items in the cart. * Get the total tax of the items in the cart.
*/ */
public function taxFloat(): float public function taxFloat(): float
{ {
return $this->getContent()->reduce(function ($tax, CartItem $cartItem) { return $this->getContent()->reduce(function (Money $tax, CartItem $cartItem) {
return $tax + $cartItem->taxTotal; return $tax->add($cartItem->taxTotal);
}, 0); }, new Money());
}
/**
* Get the total tax of the items in the cart as formatted string.
*/
public function tax(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null): string
{
return $this->numberFormat($this->taxFloat(), $decimals, $decimalPoint, $thousandSeperator);
} }
/** /**
* Get the subtotal (total - tax) of the items in the cart. * Get the subtotal (total - tax) of the items in the cart.
*/ */
public function subtotalFloat(): float public function subtotal(): Money
{ {
return $this->getContent()->reduce(function ($subTotal, CartItem $cartItem) { return $this->getContent()->reduce(function (Money $subTotal, CartItem $cartItem) {
return $subTotal + $cartItem->subtotal; return $subTotal->add($cartItem->subtotal);
}, 0); }, new Money());
}
/**
* Get the subtotal (total - tax) of the items in the cart as formatted string.
*
* @param int $decimals
* @param string $decimalPoint
* @param string $thousandSeperator
*
* @return string
*/
public function subtotal(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null): string
{
return $this->numberFormat($this->subtotalFloat(), $decimals, $decimalPoint, $thousandSeperator);
} }
/** /**
@@ -378,19 +343,11 @@ class Cart
* *
* @return float * @return float
*/ */
public function discountFloat(): float public function discount(): float
{ {
return $this->getContent()->reduce(function ($discount, CartItem $cartItem) { return $this->getContent()->reduce(function (Money $discount, CartItem $cartItem) {
return $discount + $cartItem->discountTotal; return $discount->add($cartItem->discountTotal);
}, 0); }, new Money());
}
/**
* Get the discount of the items in the cart as formatted string.
*/
public function discount(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null): string
{
return $this->numberFormat($this->discountFloat(), $decimals, $decimalPoint, $thousandSeperator);
} }
/** /**
@@ -398,61 +355,31 @@ class Cart
*/ */
public function initialFloat(): float public function initialFloat(): float
{ {
return $this->getContent()->reduce(function ($initial, CartItem $cartItem) { return $this->getContent()->reduce(function (Money $initial, CartItem $cartItem) {
return $initial + ($cartItem->qty * $cartItem->price); return $initial->add($cartItem->price->multiply($cartItem->qty));
}, 0); }, new Money());
}
/**
* Get the price of the items in the cart as formatted string.
*/
public function initial(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null): string
{
return $this->numberFormat($this->initialFloat(), $decimals, $decimalPoint, $thousandSeperator);
} }
/** /**
* Get the price of the items in the cart (previously rounded). * Get the price of the items in the cart (previously rounded).
*/ */
public function priceTotalFloat(): float public function priceTotal(): Money
{ {
return $this->getContent()->reduce(function ($initial, CartItem $cartItem) { return $this->getContent()->reduce(function (Money $initial, CartItem $cartItem) {
return $initial + $cartItem->priceTotal; return $initial->add($cartItem->priceTotal)
}, 0); }, new Money());
}
/**
* Get the price of the items in the cart as formatted string.
*/
public function priceTotal(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null): string
{
return $this->numberFormat($this->priceTotalFloat(), $decimals, $decimalPoint, $thousandSeperator);
} }
/** /**
* Get the total weight of the items in the cart. * Get the total weight of the items in the cart.
*/ */
public function weightFloat(): float public function weight(): float
{ {
return $this->getContent()->reduce(function ($total, CartItem $cartItem) { return $this->getContent()->reduce(function (float $total, CartItem $cartItem) {
return $total + ($cartItem->qty * $cartItem->weight); return $total + ($cartItem->qty * $cartItem->weight);
}, 0); }, 0);
} }
/**
* Get the total weight of the items in the cart.
*
* @param int $decimals
* @param string $decimalPoint
* @param string $thousandSeperator
*
* @return string
*/
public function weight(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null): string
{
return $this->numberFormat($this->weightFloat(), $decimals, $decimalPoint, $thousandSeperator);
}
/** /**
* Search the cart content for a cart item matching the given search closure. * Search the cart content for a cart item matching the given search closure.
* *
@@ -591,7 +518,7 @@ class Cart
throw new CartAlreadyStoredException("A cart with identifier {$identifier} was already stored."); throw new CartAlreadyStoredException("A cart with identifier {$identifier} was already stored.");
} }
$this->getConnection()->table($this->getTableName())->insert([ $this->getConnection()->table(self::getTableName())->insert([
'identifier' => $identifier, 'identifier' => $identifier,
'instance' => $instance, 'instance' => $instance,
'content' => serialize($content), 'content' => serialize($content),
@@ -621,7 +548,7 @@ class Cart
return; return;
} }
$stored = $this->getConnection()->table($this->getTableName()) $stored = $this->getConnection()->table(self::getTableName())
->where(['identifier'=> $identifier, 'instance' => $currentInstance])->first(); ->where(['identifier'=> $identifier, 'instance' => $currentInstance])->first();
$storedContent = unserialize(data_get($stored, 'content')); $storedContent = unserialize(data_get($stored, 'content'));
@@ -643,7 +570,7 @@ class Cart
$this->createdAt = Carbon::parse(data_get($stored, 'created_at')); $this->createdAt = Carbon::parse(data_get($stored, 'created_at'));
$this->updatedAt = Carbon::parse(data_get($stored, 'updated_at')); $this->updatedAt = Carbon::parse(data_get($stored, 'updated_at'));
$this->getConnection()->table($this->getTableName())->where(['identifier' => $identifier, 'instance' => $currentInstance])->delete(); $this->getConnection()->table(self::getTableName())->where(['identifier' => $identifier, 'instance' => $currentInstance])->delete();
} }
/** /**
@@ -665,7 +592,7 @@ class Cart
return; return;
} }
$this->getConnection()->table($this->getTableName())->where(['identifier' => $identifier, 'instance' => $instance])->delete(); $this->getConnection()->table(self::getTableName())->where(['identifier' => $identifier, 'instance' => $instance])->delete();
$this->events->dispatch('cart.erased'); $this->events->dispatch('cart.erased');
} }
@@ -686,7 +613,7 @@ class Cart
return false; return false;
} }
$stored = $this->getConnection()->table($this->getTableName()) $stored = $this->getConnection()->table(self::getTableName())
->where(['identifier'=> $identifier, 'instance'=> $instance])->first(); ->where(['identifier'=> $identifier, 'instance'=> $instance])->first();
$storedContent = unserialize($stored->content); $storedContent = unserialize($stored->content);
@@ -721,8 +648,6 @@ class Cart
/** /**
* Get the carts content, if there is no cart content set yet, return a new empty Collection. * Get the carts content, if there is no cart content set yet, return a new empty Collection.
*
* @return \Illuminate\Support\Collection
*/ */
protected function getContent(): Collection protected function getContent(): Collection
{ {
@@ -742,8 +667,6 @@ class Cart
* @param float $price * @param float $price
* @param float $weight * @param float $weight
* @param array $options * @param array $options
*
* @return \Gloudemans\Shoppingcart\CartItem
*/ */
private function createCartItem($id, ?string $name = null, int $qty, ?Money $price = null, int $weight = 0, array $options = []): CartItem private function createCartItem($id, ?string $name = null, int $qty, ?Money $price = null, int $weight = 0, array $options = []): CartItem
{ {
@@ -768,12 +691,10 @@ class Cart
* Check if the item is a multidimensional array or an array of Buyables. * Check if the item is a multidimensional array or an array of Buyables.
* *
* @param mixed $item * @param mixed $item
*
* @return bool
*/ */
private function isMulti($item): bool private function isMulti($item): bool
{ {
if (!is_array($item)) { if (! is_array($item)) {
return false; return false;
} }
@@ -782,20 +703,16 @@ class Cart
/** /**
* @param $identifier * @param $identifier
*
* @return bool
*/ */
private function storedCartInstanceWithIdentifierExists(string $instance, string $identifier): bool private function storedCartInstanceWithIdentifierExists(string $instance, string $identifier): bool
{ {
return $this->getConnection()->table($this->getTableName())->where(['identifier' => $identifier, 'instance'=> $instance])->exists(); return $this->getConnection()->table(self::getTableName())->where(['identifier' => $identifier, 'instance'=> $instance])->exists();
} }
/** /**
* Get the database connection. * Get the database connection.
*
* @return \Illuminate\Database\Connection
*/ */
private function getConnection() private function getConnection(): \Illuminate\Database\Connection
{ {
return app(DatabaseManager::class)->connection($this->getConnectionName()); return app(DatabaseManager::class)->connection($this->getConnectionName());
} }
@@ -805,50 +722,21 @@ class Cart
* *
* @return string * @return string
*/ */
private function getTableName(): string private static function getTableName(): string
{ {
return config('cart.database.table', 'shoppingcart'); return config('cart.database.table', 'shoppingcart');
} }
/** /**
* Get the database connection name. * Get the database connection name.
*
* @return string
*/ */
private function getConnectionName(): string private function getConnectionName(): ?string
{ {
$connection = config('cart.database.connection'); $connection = config('cart.database.connection');
return is_null($connection) ? config('database.default') : $connection; return is_null($connection) ? config('database.default') : $connection;
} }
/**
* Get the Formatted number.
*
* @param $value
* @param $decimals
* @param $decimalPoint
* @param $thousandSeperator
*
* @return string
*/
private function numberFormat($value, ?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null): string
{
if (is_null($decimals)) {
$decimals = config('cart.format.decimals', 2);
}
if (is_null($decimalPoint)) {
$decimalPoint = config('cart.format.decimal_point', '.');
}
if (is_null($thousandSeperator)) {
$thousandSeperator = config('cart.format.thousand_separator', ',');
}
return number_format($value, $decimals, $decimalPoint, $thousandSeperator);
}
/** /**
* Get the creation date of the cart (db context). * Get the creation date of the cart (db context).
* *