This commit is contained in:
Patrick Henninger
2022-02-06 19:28:56 +01:00
parent c4fb20271f
commit 5d55788584
9 changed files with 62 additions and 62 deletions

View File

@@ -2,9 +2,8 @@
namespace Gloudemans\Shoppingcart; namespace Gloudemans\Shoppingcart;
use Gloudemans\Shoppingcart\CartItemOptions;
use Money\Money;
use Money\Currency; use Money\Currency;
use Money\Money;
trait CanBeBought trait CanBeBought
{ {
@@ -25,9 +24,9 @@ trait CanBeBought
{ {
if (($name = $this->getAttribute('name'))) { if (($name = $this->getAttribute('name'))) {
return $name; return $name;
} else if (($title = $this->getAttribute('title'))) { } elseif (($title = $this->getAttribute('title'))) {
return $title; return $title;
} else if (($description = $this->getAttribute('description'))) { } elseif (($description = $this->getAttribute('description'))) {
return $description; return $description;
} else { } else {
return null; return null;

View File

@@ -2,11 +2,8 @@
namespace Gloudemans\Shoppingcart; namespace Gloudemans\Shoppingcart;
use Closure;
use InvalidArgumentException;
use Carbon\Carbon; use Carbon\Carbon;
use Money\Money; use Closure;
use Money\Currency;
use Gloudemans\Shoppingcart\Contracts\Buyable; use Gloudemans\Shoppingcart\Contracts\Buyable;
use Gloudemans\Shoppingcart\Contracts\InstanceIdentifier; use Gloudemans\Shoppingcart\Contracts\InstanceIdentifier;
use Gloudemans\Shoppingcart\Exceptions\CartAlreadyStoredException; use Gloudemans\Shoppingcart\Exceptions\CartAlreadyStoredException;
@@ -17,8 +14,11 @@ use Illuminate\Database\DatabaseManager;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Session\SessionManager; use Illuminate\Session\SessionManager;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Config;
use Illuminate\Support\Traits\Macroable;
use InvalidArgumentException;
use Money\Currency;
use Money\Money;
class Cart class Cart
{ {
@@ -116,23 +116,23 @@ class Cart
{ {
/* Allow adding a CartItem by raw parameters */ /* Allow adding a CartItem by raw parameters */
if (is_int($id) || is_string($id)) { if (is_int($id) || is_string($id)) {
if (! is_null($nameOrQty) && ! is_string($nameOrQty)) { if (!is_null($nameOrQty) && !is_string($nameOrQty)) {
throw new InvalidArgumentException('$nameOrQty must be of type string (name) or null when adding with raw parameters'); throw new InvalidArgumentException('$nameOrQty must be of type string (name) or null when adding with raw parameters');
} }
if (! is_null($qtyOrOptions) && ! is_int($qtyOrOptions)) { if (!is_null($qtyOrOptions) && !is_int($qtyOrOptions)) {
throw new InvalidArgumentException('$nameOrQty must be of type int (quantity) or null when adding with raw parameters'); throw new InvalidArgumentException('$nameOrQty must be of type int (quantity) or null when adding with raw parameters');
} }
return $this->addCartItem(CartItem::fromAttributes($id, $nameOrQty, $price, $qtyOrOptions ?: 1, $weight ?: 0, $options ?: new CartItemOptions([]))); return $this->addCartItem(CartItem::fromAttributes($id, $nameOrQty, $price, $qtyOrOptions ?: 1, $weight ?: 0, $options ?: new CartItemOptions([])));
} }
/* Also allow passing a Buyable instance, get data from the instance rather than parameters */ /* Also allow passing a Buyable instance, get data from the instance rather than parameters */
else if ($id instanceof Buyable) { elseif ($id instanceof Buyable) {
if (! is_null($qtyOrOptions) && ! is_int($nameOrQty)) { if (!is_null($qtyOrOptions) && !is_int($nameOrQty)) {
throw new InvalidArgumentException('$nameOrQty must be of type int (quantity) when adding a Buyable instance'); throw new InvalidArgumentException('$nameOrQty must be of type int (quantity) when adding a Buyable instance');
} }
if (! is_null($qtyOrOptions) && ! $qtyOrOptions instanceof CartItemOptions) { if (!is_null($qtyOrOptions) && !$qtyOrOptions instanceof CartItemOptions) {
throw new InvalidArgumentException('$qtyOrOptions must be of type CartItemOptions (options) or null when adding a Buyable instance'); throw new InvalidArgumentException('$qtyOrOptions must be of type CartItemOptions (options) or null when adding a Buyable instance');
} }
@@ -145,7 +145,7 @@ class Cart
return $this->addCartItem($cartItem); return $this->addCartItem($cartItem);
} }
/* Also allow passing multiple definitions at the same time, simply call same method and collec return value */ /* Also allow passing multiple definitions at the same time, simply call same method and collec return value */
else if (is_array($id)) { elseif (is_array($id)) {
/* Check if this iterable contains instances */ /* Check if this iterable contains instances */
if (is_array(head($id)) || head($id) instanceof Buyable) { if (is_array(head($id)) || head($id) instanceof Buyable) {
return array_map(function (Buyable|iterable $item) { return array_map(function (Buyable|iterable $item) {
@@ -179,7 +179,7 @@ class Cart
{ {
$item->setInstance($this->currentInstance()); $item->setInstance($this->currentInstance());
if (! $keepDiscount) { if (!$keepDiscount) {
$item->setDiscount($this->discount); $item->setDiscount($this->discount);
} }
@@ -363,7 +363,7 @@ class Cart
if ($calculated instanceof Money) { if ($calculated instanceof Money) {
return $calculated; return $calculated;
} else { } else {
throw new \TypeError("Calculated price is not an instance of Money"); throw new \TypeError('Calculated price is not an instance of Money');
} }
} }
@@ -381,7 +381,7 @@ class Cart
if ($calculated instanceof Money) { if ($calculated instanceof Money) {
return $calculated; return $calculated;
} else { } else {
throw new \TypeError("Calculated discount is not an instance of Money"); throw new \TypeError('Calculated discount is not an instance of Money');
} }
} }
@@ -397,7 +397,7 @@ class Cart
if ($calculated instanceof Money) { if ($calculated instanceof Money) {
return $calculated; return $calculated;
} else { } else {
throw new \TypeError("Calculated subtotal is not an instance of Money"); throw new \TypeError('Calculated subtotal is not an instance of Money');
} }
} }
@@ -427,7 +427,7 @@ class Cart
if ($calculated instanceof Money) { if ($calculated instanceof Money) {
return $calculated; return $calculated;
} else { } else {
throw new \TypeError("Calculated total is not an instance of Money"); throw new \TypeError('Calculated total is not an instance of Money');
} }
} }
@@ -443,7 +443,7 @@ class Cart
if (is_int($calculated)) { if (is_int($calculated)) {
return $calculated; return $calculated;
} else { } else {
throw new \TypeError("Calculated weight was not an integer"); throw new \TypeError('Calculated weight was not an integer');
} }
} }

View File

@@ -8,9 +8,9 @@ use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Config;
use Money\Money;
use Money\Formatter\DecimalMoneyFormatter;
use Money\Currencies\ISOCurrencies; use Money\Currencies\ISOCurrencies;
use Money\Formatter\DecimalMoneyFormatter;
use Money\Money;
class CartItem implements Arrayable, Jsonable class CartItem implements Arrayable, Jsonable
{ {
@@ -118,7 +118,7 @@ class CartItem implements Arrayable, Jsonable
* *
* @param mixed $model * @param mixed $model
*/ */
public function associate(string|Model $model) : self public function associate(string|Model $model): self
{ {
$this->associatedModel = is_string($model) ? $model : get_class($model); $this->associatedModel = is_string($model) ? $model : get_class($model);
@@ -128,7 +128,7 @@ class CartItem implements Arrayable, Jsonable
/** /**
* Set the tax rate. * Set the tax rate.
*/ */
public function setTaxRate(float $taxRate) : self public function setTaxRate(float $taxRate): self
{ {
$this->taxRate = $taxRate; $this->taxRate = $taxRate;
@@ -138,7 +138,7 @@ class CartItem implements Arrayable, Jsonable
/** /**
* Set the discount rate. * Set the discount rate.
*/ */
public function setDiscount(float|Money $discount) : self public function setDiscount(float|Money $discount): self
{ {
$this->discount = $discount; $this->discount = $discount;
@@ -148,7 +148,7 @@ class CartItem implements Arrayable, Jsonable
/** /**
* Set cart instance. * Set cart instance.
*/ */
public function setInstance(?string $instance) : self public function setInstance(?string $instance): self
{ {
$this->instance = $instance; $this->instance = $instance;
@@ -197,7 +197,7 @@ class CartItem implements Arrayable, Jsonable
} }
/** /**
* This is the tax, based on the subtotal (all previous calculations) and set tax rate * This is the tax, based on the subtotal (all previous calculations) and set tax rate.
*/ */
public function tax(): Money public function tax(): Money
{ {
@@ -223,18 +223,20 @@ class CartItem implements Arrayable, Jsonable
/** /**
* Create a new instance from a Buyable. * Create a new instance from a Buyable.
*/ */
public static function fromBuyable(Buyable $item, int $qty = 1, ?CartItemOptions $options = null) : self public static function fromBuyable(Buyable $item, int $qty = 1, ?CartItemOptions $options = null): self
{ {
$options = $options ?: new CartItemOptions([]); $options = $options ?: new CartItemOptions([]);
return new self($item->getBuyableIdentifier($options), $item->getBuyableDescription($options), $item->getBuyablePrice($options), $qty, $item->getBuyableWeight($options), $options); return new self($item->getBuyableIdentifier($options), $item->getBuyableDescription($options), $item->getBuyablePrice($options), $qty, $item->getBuyableWeight($options), $options);
} }
/** /**
* Create a new instance from the given array. * Create a new instance from the given array.
*/ */
public static function fromArray(array $attributes) : self public static function fromArray(array $attributes): self
{ {
$options = new CartItemOptions(Arr::get($attributes, 'options', [])); $options = new CartItemOptions(Arr::get($attributes, 'options', []));
return new self($attributes['id'], $attributes['name'], $attributes['price'], $attributes['qty'], $attributes['weight'], $options); return new self($attributes['id'], $attributes['name'], $attributes['price'], $attributes['qty'], $attributes['weight'], $options);
} }
@@ -243,9 +245,10 @@ class CartItem implements Arrayable, Jsonable
* *
* @param int|string $id * @param int|string $id
*/ */
public static function fromAttributes(int|string $id, string $name, Money $price, int $qty = 1, int $weight = 0, ?CartItemOptions $options = null) : self public static function fromAttributes(int|string $id, string $name, Money $price, int $qty = 1, int $weight = 0, ?CartItemOptions $options = null): self
{ {
$options = $options ?: new CartItemOptions([]); $options = $options ?: new CartItemOptions([]);
return new self($id, $name, $price, $qty, $weight, $options); return new self($id, $name, $price, $qty, $weight, $options);
} }
@@ -269,7 +272,7 @@ class CartItem implements Arrayable, Jsonable
'discount' => self::formatMoney($this->discount()), 'discount' => self::formatMoney($this->discount()),
'subtotal' => self::formatMoney($this->subtotal()), 'subtotal' => self::formatMoney($this->subtotal()),
'tax' => self::formatMoney($this->tax()), 'tax' => self::formatMoney($this->tax()),
'total' => self::formatMoney($this->total()), 'total' => self::formatMoney($this->total()),
]; ];
} }
@@ -288,7 +291,7 @@ class CartItem implements Arrayable, Jsonable
/** /**
* Generate a unique id for the cart item. * Generate a unique id for the cart item.
*/ */
private static function formatMoney(Money $money) : string private static function formatMoney(Money $money): string
{ {
return (new DecimalMoneyFormatter(new ISOCurrencies()))->format($money); return (new DecimalMoneyFormatter(new ISOCurrencies()))->format($money);
} }
@@ -296,10 +299,10 @@ class CartItem implements Arrayable, Jsonable
/** /**
* Generate a unique id for the cart item. * Generate a unique id for the cart item.
*/ */
protected function generateRowId(string $id, array $options) : string protected function generateRowId(string $id, array $options): string
{ {
ksort($options); ksort($options);
return md5($id . serialize($options)); return md5($id.serialize($options));
} }
} }

View File

@@ -2,8 +2,6 @@
namespace Gloudemans\Shoppingcart; namespace Gloudemans\Shoppingcart;
use Illuminate\Auth\Events\Logout;
use Illuminate\Session\SessionManager;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
class ShoppingcartServiceProvider extends ServiceProvider class ShoppingcartServiceProvider extends ServiceProvider

View File

@@ -5,9 +5,9 @@ namespace Gloudemans\Tests\Shoppingcart;
use Gloudemans\Shoppingcart\CartItem; use Gloudemans\Shoppingcart\CartItem;
use Gloudemans\Shoppingcart\CartItemOptions; use Gloudemans\Shoppingcart\CartItemOptions;
use Gloudemans\Shoppingcart\ShoppingcartServiceProvider; use Gloudemans\Shoppingcart\ShoppingcartServiceProvider;
use Orchestra\Testbench\TestCase;
use Money\Money;
use Money\Currency; use Money\Currency;
use Money\Money;
use Orchestra\Testbench\TestCase;
class CartItemTest extends TestCase class CartItemTest extends TestCase
{ {

View File

@@ -3,8 +3,6 @@
namespace Gloudemans\Tests\Shoppingcart; namespace Gloudemans\Tests\Shoppingcart;
use Carbon\Carbon; use Carbon\Carbon;
use Money\Money;
use Money\Currency;
use Gloudemans\Shoppingcart\Cart; use Gloudemans\Shoppingcart\Cart;
use Gloudemans\Shoppingcart\CartItem; use Gloudemans\Shoppingcart\CartItem;
use Gloudemans\Shoppingcart\CartItemOptions; use Gloudemans\Shoppingcart\CartItemOptions;
@@ -15,6 +13,8 @@ use Gloudemans\Tests\Shoppingcart\Fixtures\Identifiable;
use Gloudemans\Tests\Shoppingcart\Fixtures\ProductModel; use Gloudemans\Tests\Shoppingcart\Fixtures\ProductModel;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Money\Currency;
use Money\Money;
use Orchestra\Testbench\TestCase; use Orchestra\Testbench\TestCase;
class CartTest extends TestCase class CartTest extends TestCase
@@ -31,7 +31,7 @@ class CartTest extends TestCase
protected function getPackageProviders($app) protected function getPackageProviders($app)
{ {
return [ return [
ShoppingcartServiceProvider::class ShoppingcartServiceProvider::class,
]; ];
} }
@@ -1009,7 +1009,7 @@ class CartTest extends TestCase
$this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->price); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->price);
$this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discount()); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discount());
$this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->subtotal()); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->subtotal());
$this->assertEquals(new Money(190, new Currency('USD')), $cartItem->tax()); $this->assertEquals(new Money(190, new Currency('USD')), $cartItem->tax());
$this->assertEquals(new Money(1190, new Currency('USD')), $cartItem->total()); $this->assertEquals(new Money(1190, new Currency('USD')), $cartItem->total());
} }

View File

@@ -2,11 +2,11 @@
namespace Gloudemans\Tests\Shoppingcart\Fixtures; namespace Gloudemans\Tests\Shoppingcart\Fixtures;
use Gloudemans\Shoppingcart\Contracts\Buyable;
use Gloudemans\Shoppingcart\CartItemOptions; use Gloudemans\Shoppingcart\CartItemOptions;
use Gloudemans\Shoppingcart\Contracts\Buyable;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Money\Money;
use Money\Currency; use Money\Currency;
use Money\Money;
class BuyableProduct extends Model implements Buyable class BuyableProduct extends Model implements Buyable
{ {
@@ -48,7 +48,7 @@ class BuyableProduct extends Model implements Buyable
* *
* @return string * @return string
*/ */
public function getBuyableDescription(CartItemOptions $options) : ?string public function getBuyableDescription(CartItemOptions $options): ?string
{ {
return $this->name; return $this->name;
} }

View File

@@ -8,7 +8,7 @@ class ProductModel extends Model
{ {
public $someValue = 'Some value'; public $someValue = 'Some value';
public function find($id) : self public function find($id): self
{ {
return $this; return $this;
} }