Update Cart.php

This commit is contained in:
Patrick
2022-01-18 00:05:53 +01:00
committed by GitHub
parent c67d72d65c
commit 0acb540e95

View File

@@ -23,24 +23,20 @@ class Cart
/**
* Instance of the session manager.
*
* @var \Illuminate\Session\SessionManager
*/
private $session;
private \Illuminate\Session\SessionManager $session;
/**
* Instance of the event dispatcher.
*
* @var \Illuminate\Contracts\Events\Dispatcher
*/
private $events;
private \Illuminate\Contracts\Events\Dispatcher $events;
/**
* Holds the current cart instance.
*
* @var string
*/
private $instance;
private string $instance;
/**
* Holds the creation date of the cart.
@@ -92,7 +88,7 @@ class Cart
*
* @return \Gloudemans\Shoppingcart\Cart
*/
public function instance($instance = null)
public function instance(?string $instance = null)
{
$instance = $instance ?: self::DEFAULT_INSTANCE;
@@ -128,7 +124,7 @@ class Cart
*
* @return \Gloudemans\Shoppingcart\CartItem
*/
public function add($id, $name = null, $qty = null, $price = null, $weight = 0, array $options = [])
public function add($id, ?string $name = null, $qty = null, $price = null, $weight = 0, array $options = [])
{
if ($this->isMulti($id)) {
return array_map(function ($item) {
@@ -151,7 +147,7 @@ class Cart
*
* @return \Gloudemans\Shoppingcart\CartItem The CartItem
*/
public function addCartItem($item, $keepDiscount = false, $keepTax = false, $dispatchEvent = true)
public function addCartItem(CartItem $item, bool $keepDiscount = false, bool $keepTax = false, bool $dispatchEvent = true)
{
if (!$keepDiscount) {
$item->setDiscountRate($this->discount);
@@ -190,7 +186,7 @@ class Cart
*
* @return \Gloudemans\Shoppingcart\CartItem
*/
public function update($rowId, $qty)
public function update(string $rowId, $qty)
{
$cartItem = $this->get($rowId);
@@ -245,7 +241,7 @@ class Cart
*
* @return void
*/
public function remove($rowId)
public function remove(string $rowId)
{
$cartItem = $this->get($rowId);
@@ -267,7 +263,7 @@ class Cart
*
* @return \Gloudemans\Shoppingcart\CartItem
*/
public function get($rowId)
public function get(string $rowId)
{
$content = $this->getContent();
@@ -315,20 +311,16 @@ class Cart
/**
* Get the amount of CartItems in the Cart.
* Keep in mind that this does NOT count quantity.
*
* @return int|float
*/
public function countItems()
public function countItems() : int
{
return $this->getContent()->count();
}
/**
* Get the total price of the items in the cart.
*
* @return float
*/
public function totalFloat()
public function totalFloat() : float
{
return $this->getContent()->reduce(function ($total, CartItem $cartItem) {
return $total + $cartItem->total;
@@ -337,24 +329,16 @@ class Cart
/**
* Get the total price of the items in the cart as formatted string.
*
* @param int $decimals
* @param string $decimalPoint
* @param string $thousandSeperator
*
* @return string
*/
public function total($decimals = null, $decimalPoint = null, $thousandSeperator = null)
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.
*
* @return float
*/
public function taxFloat()
public function taxFloat() : float
{
return $this->getContent()->reduce(function ($tax, CartItem $cartItem) {
return $tax + $cartItem->taxTotal;
@@ -363,24 +347,16 @@ class Cart
/**
* Get the total tax of the items in the cart as formatted string.
*
* @param int $decimals
* @param string $decimalPoint
* @param string $thousandSeperator
*
* @return string
*/
public function tax($decimals = null, $decimalPoint = null, $thousandSeperator = null)
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.
*
* @return float
*/
public function subtotalFloat()
public function subtotalFloat() : float
{
return $this->getContent()->reduce(function ($subTotal, CartItem $cartItem) {
return $subTotal + $cartItem->subtotal;
@@ -396,7 +372,7 @@ class Cart
*
* @return string
*/
public function subtotal($decimals = null, $decimalPoint = null, $thousandSeperator = null)
public function subtotal(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null) : string
{
return $this->numberFormat($this->subtotalFloat(), $decimals, $decimalPoint, $thousandSeperator);
}
@@ -406,7 +382,7 @@ class Cart
*
* @return float
*/
public function discountFloat()
public function discountFloat() : float
{
return $this->getContent()->reduce(function ($discount, CartItem $cartItem) {
return $discount + $cartItem->discountTotal;
@@ -415,24 +391,16 @@ class Cart
/**
* Get the discount of the items in the cart as formatted string.
*
* @param int $decimals
* @param string $decimalPoint
* @param string $thousandSeperator
*
* @return string
*/
public function discount($decimals = null, $decimalPoint = null, $thousandSeperator = null)
public function discount(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null) : string
{
return $this->numberFormat($this->discountFloat(), $decimals, $decimalPoint, $thousandSeperator);
}
/**
* Get the price of the items in the cart (not rounded).
*
* @return float
*/
public function initialFloat()
public function initialFloat() : float
{
return $this->getContent()->reduce(function ($initial, CartItem $cartItem) {
return $initial + ($cartItem->qty * $cartItem->price);
@@ -441,24 +409,16 @@ class Cart
/**
* Get the price of the items in the cart as formatted string.
*
* @param int $decimals
* @param string $decimalPoint
* @param string $thousandSeperator
*
* @return string
*/
public function initial($decimals = null, $decimalPoint = null, $thousandSeperator = null)
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).
*
* @return float
*/
public function priceTotalFloat()
public function priceTotalFloat() : float
{
return $this->getContent()->reduce(function ($initial, CartItem $cartItem) {
return $initial + $cartItem->priceTotal;
@@ -467,24 +427,16 @@ class Cart
/**
* Get the price of the items in the cart as formatted string.
*
* @param int $decimals
* @param string $decimalPoint
* @param string $thousandSeperator
*
* @return string
*/
public function priceTotal($decimals = null, $decimalPoint = null, $thousandSeperator = null)
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.
*
* @return float
*/
public function weightFloat()
public function weightFloat() : float
{
return $this->getContent()->reduce(function ($total, CartItem $cartItem) {
return $total + ($cartItem->qty * $cartItem->weight);
@@ -500,7 +452,7 @@ class Cart
*
* @return string
*/
public function weight($decimals = null, $decimalPoint = null, $thousandSeperator = null)
public function weight(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null) : string
{
return $this->numberFormat($this->weightFloat(), $decimals, $decimalPoint, $thousandSeperator);
}
@@ -512,7 +464,7 @@ class Cart
*
* @return \Illuminate\Support\Collection
*/
public function search(Closure $search)
public function search(Closure $search) : Collection
{
return $this->getContent()->filter($search);
}
@@ -525,7 +477,7 @@ class Cart
*
* @return void
*/
public function associate($rowId, $model)
public function associate(string $rowId, $model)
{
if (is_string($model) && !class_exists($model)) {
throw new UnknownModelException("The supplied model {$model} does not exist.");
@@ -550,7 +502,7 @@ class Cart
*
* @return void
*/
public function setTax($rowId, $taxRate)
public function setTax(string $rowId, $taxRate)
{
$cartItem = $this->get($rowId);
@@ -589,7 +541,7 @@ class Cart
*
* @return void
*/
public function setDiscount($rowId, $discount)
public function setDiscount(string $rowId, $discount)
{
$cartItem = $this->get($rowId);
@@ -732,7 +684,7 @@ class Cart
*
* @return bool
*/
public function merge($identifier, $keepDiscount = false, $keepTax = false, $dispatchAdd = true, $instance = self::DEFAULT_INSTANCE)
public function merge($identifier, bool $keepDiscount = false, bool $keepTax = false, bool $dispatchAdd = true, $instance = self::DEFAULT_INSTANCE)
{
if (!$this->storedCartInstanceWithIdentifierExists($instance, $identifier)) {
return false;
@@ -759,7 +711,7 @@ class Cart
*
* @return float|null
*/
public function __get($attribute)
public function __get(string $attribute)
{
switch ($attribute) {
case 'total':
@@ -778,7 +730,7 @@ class Cart
*
* @return \Illuminate\Support\Collection
*/
protected function getContent()
protected function getContent() : Collection
{
if ($this->session->has($this->instance)) {
return $this->session->get($this->instance);
@@ -799,7 +751,7 @@ class Cart
*
* @return \Gloudemans\Shoppingcart\CartItem
*/
private function createCartItem($id, $name, $qty, $price, $weight, array $options)
private function createCartItem($id, string $name, $qty, $price, $weight, array $options) : CartItem
{
if ($id instanceof Buyable) {
$cartItem = CartItem::fromBuyable($id, $qty ?: []);
@@ -886,7 +838,7 @@ class Cart
*
* @return string
*/
private function numberFormat($value, $decimals, $decimalPoint, $thousandSeperator)
private function numberFormat($value, ?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null)
{
if (is_null($decimals)) {
$decimals = config('cart.format.decimals', 2);
@@ -908,7 +860,7 @@ class Cart
*
* @return \Carbon\Carbon|null
*/
public function createdAt()
public function createdAt() : ?Carbon
{
return $this->createdAt;
}
@@ -918,7 +870,7 @@ class Cart
*
* @return \Carbon\Carbon|null
*/
public function updatedAt()
public function updatedAt() : ?Carbon
{
return $this->updatedAt;
}