diff --git a/src/CanBeBought.php b/src/CanBeBought.php index 9088fb0..eb9810f 100644 --- a/src/CanBeBought.php +++ b/src/CanBeBought.php @@ -2,9 +2,8 @@ namespace Gloudemans\Shoppingcart; -use Gloudemans\Shoppingcart\CartItemOptions; -use Money\Money; use Money\Currency; +use Money\Money; trait CanBeBought { @@ -25,13 +24,13 @@ trait CanBeBought { if (($name = $this->getAttribute('name'))) { return $name; - } else if (($title = $this->getAttribute('title'))) { + } elseif (($title = $this->getAttribute('title'))) { return $title; - } else if (($description = $this->getAttribute('description'))) { + } elseif (($description = $this->getAttribute('description'))) { return $description; } else { return null; - } + } } /** diff --git a/src/Cart.php b/src/Cart.php index 5fb6910..72e57e3 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -2,11 +2,8 @@ namespace Gloudemans\Shoppingcart; -use Closure; -use InvalidArgumentException; use Carbon\Carbon; -use Money\Money; -use Money\Currency; +use Closure; use Gloudemans\Shoppingcart\Contracts\Buyable; use Gloudemans\Shoppingcart\Contracts\InstanceIdentifier; use Gloudemans\Shoppingcart\Exceptions\CartAlreadyStoredException; @@ -17,8 +14,11 @@ use Illuminate\Database\DatabaseManager; use Illuminate\Database\Eloquent\Model; use Illuminate\Session\SessionManager; use Illuminate\Support\Collection; -use Illuminate\Support\Traits\Macroable; use Illuminate\Support\Facades\Config; +use Illuminate\Support\Traits\Macroable; +use InvalidArgumentException; +use Money\Currency; +use Money\Money; class Cart { @@ -116,36 +116,36 @@ class Cart { /* Allow adding a CartItem by raw parameters */ 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'); } - - 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'); } - + 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 */ - else if ($id instanceof Buyable) { - if (! is_null($qtyOrOptions) && ! is_int($nameOrQty)) { + elseif ($id instanceof Buyable) { + if (!is_null($qtyOrOptions) && !is_int($nameOrQty)) { 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'); } - + $cartItem = CartItem::fromBuyable($id, $nameOrQty ?: 1, $qtyOrOptions ?: new CartItemOptions([])); if ($id instanceof Model) { $cartItem->associate($id); } - + return $this->addCartItem($cartItem); } /* 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 */ if (is_array(head($id)) || head($id) instanceof Buyable) { return array_map(function (Buyable|iterable $item) { @@ -155,7 +155,7 @@ class Cart /* Treat the array itself as an instance */ else { $cartItem = CartItem::fromArray($id); - + return $this->addCartItem($cartItem); } } @@ -178,8 +178,8 @@ class Cart public function addCartItem(CartItem $item, bool $keepDiscount = false, bool $keepTax = false, bool $dispatchEvent = true): CartItem { $item->setInstance($this->currentInstance()); - - if (! $keepDiscount) { + + if (!$keepDiscount) { $item->setDiscount($this->discount); } @@ -363,7 +363,7 @@ class Cart if ($calculated instanceof Money) { return $calculated; } 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) { return $calculated; } else { - throw new \TypeError("Calculated discount is not an instance of Money"); + throw new \TypeError('Calculated discount is not an instance of Money'); } } @@ -397,10 +397,10 @@ class Cart if ($calculated instanceof Money) { return $calculated; } else { - throw new \TypeError("Calculated subtotal is not an instance of Money"); + throw new \TypeError('Calculated subtotal is not an instance of Money'); } } - + /** * Get the total tax of the items in the cart. */ @@ -427,7 +427,7 @@ class Cart if ($calculated instanceof Money) { return $calculated; } 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)) { return $calculated; } else { - throw new \TypeError("Calculated weight was not an integer"); + throw new \TypeError('Calculated weight was not an integer'); } } diff --git a/src/CartItem.php b/src/CartItem.php index 3559480..6bd14d8 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -8,9 +8,9 @@ use Illuminate\Contracts\Support\Jsonable; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Config; -use Money\Money; -use Money\Formatter\DecimalMoneyFormatter; use Money\Currencies\ISOCurrencies; +use Money\Formatter\DecimalMoneyFormatter; +use Money\Money; class CartItem implements Arrayable, Jsonable { @@ -118,7 +118,7 @@ class CartItem implements Arrayable, Jsonable * * @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); @@ -128,7 +128,7 @@ class CartItem implements Arrayable, Jsonable /** * Set the tax rate. */ - public function setTaxRate(float $taxRate) : self + public function setTaxRate(float $taxRate): self { $this->taxRate = $taxRate; @@ -138,7 +138,7 @@ class CartItem implements Arrayable, Jsonable /** * Set the discount rate. */ - public function setDiscount(float|Money $discount) : self + public function setDiscount(float|Money $discount): self { $this->discount = $discount; @@ -148,7 +148,7 @@ class CartItem implements Arrayable, Jsonable /** * Set cart instance. */ - public function setInstance(?string $instance) : self + public function setInstance(?string $instance): self { $this->instance = $instance; @@ -165,7 +165,7 @@ class CartItem implements Arrayable, Jsonable } /** - * This will is the price of the CartItem considering the set quantity. If you need the single + * This will is the price of the CartItem considering the set quantity. If you need the single * price just set the parameter to true. */ public function price(): Money @@ -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 { @@ -223,18 +223,20 @@ class CartItem implements Arrayable, Jsonable /** * 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([]); + 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. */ - public static function fromArray(array $attributes) : self + public static function fromArray(array $attributes): self { $options = new CartItemOptions(Arr::get($attributes, '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 */ - 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([]); + return new self($id, $name, $price, $qty, $weight, $options); } @@ -269,7 +272,7 @@ class CartItem implements Arrayable, Jsonable 'discount' => self::formatMoney($this->discount()), 'subtotal' => self::formatMoney($this->subtotal()), 'tax' => self::formatMoney($this->tax()), - 'total' => self::formatMoney($this->total()), + 'total' => self::formatMoney($this->total()), ]; } @@ -284,22 +287,22 @@ class CartItem implements Arrayable, Jsonable { return json_encode($this->toArray(), $options); } - + /** * 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); } - + /** * 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); - return md5($id . serialize($options)); + return md5($id.serialize($options)); } } diff --git a/src/Config/cart.php b/src/Config/cart.php index f3f2bc2..eb072dd 100644 --- a/src/Config/cart.php +++ b/src/Config/cart.php @@ -3,7 +3,7 @@ use Money\Money; return [ - + /* |-------------------------------------------------------------------------- | Rounding strategy diff --git a/src/ShoppingcartServiceProvider.php b/src/ShoppingcartServiceProvider.php index 7a92bd3..ecda57d 100644 --- a/src/ShoppingcartServiceProvider.php +++ b/src/ShoppingcartServiceProvider.php @@ -2,8 +2,6 @@ namespace Gloudemans\Shoppingcart; -use Illuminate\Auth\Events\Logout; -use Illuminate\Session\SessionManager; use Illuminate\Support\ServiceProvider; class ShoppingcartServiceProvider extends ServiceProvider diff --git a/tests/CartItemTest.php b/tests/CartItemTest.php index 88487d2..885d1d3 100644 --- a/tests/CartItemTest.php +++ b/tests/CartItemTest.php @@ -5,9 +5,9 @@ namespace Gloudemans\Tests\Shoppingcart; use Gloudemans\Shoppingcart\CartItem; use Gloudemans\Shoppingcart\CartItemOptions; use Gloudemans\Shoppingcart\ShoppingcartServiceProvider; -use Orchestra\Testbench\TestCase; -use Money\Money; use Money\Currency; +use Money\Money; +use Orchestra\Testbench\TestCase; class CartItemTest extends TestCase { diff --git a/tests/CartTest.php b/tests/CartTest.php index 2e75695..baa8f10 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -3,8 +3,6 @@ namespace Gloudemans\Tests\Shoppingcart; use Carbon\Carbon; -use Money\Money; -use Money\Currency; use Gloudemans\Shoppingcart\Cart; use Gloudemans\Shoppingcart\CartItem; use Gloudemans\Shoppingcart\CartItemOptions; @@ -15,6 +13,8 @@ use Gloudemans\Tests\Shoppingcart\Fixtures\Identifiable; use Gloudemans\Tests\Shoppingcart\Fixtures\ProductModel; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Event; +use Money\Currency; +use Money\Money; use Orchestra\Testbench\TestCase; class CartTest extends TestCase @@ -31,7 +31,7 @@ class CartTest extends TestCase protected function getPackageProviders($app) { return [ - ShoppingcartServiceProvider::class + ShoppingcartServiceProvider::class, ]; } @@ -525,7 +525,7 @@ class CartTest extends TestCase ])); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - + $this->assertEquals(new Money(1000, $cartItem->price->getCurrency()), $cartItem->price()); $this->assertEquals(new Money(1000, $cartItem->price->getCurrency()), $cartItem->subtotal()); $this->assertEquals(new Money(210, $cartItem->price->getCurrency()), $cartItem->tax()); @@ -547,7 +547,7 @@ class CartTest extends TestCase 'options' => [], 'discount' => '0.00', 'weight' => 0, - + ], '370d08585360f5c568b18d1f2e4ca1df' => [ 'rowId' => '370d08585360f5c568b18d1f2e4ca1df', @@ -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->discount()); $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()); } diff --git a/tests/Fixtures/BuyableProduct.php b/tests/Fixtures/BuyableProduct.php index 32dc098..9265aab 100644 --- a/tests/Fixtures/BuyableProduct.php +++ b/tests/Fixtures/BuyableProduct.php @@ -2,11 +2,11 @@ namespace Gloudemans\Tests\Shoppingcart\Fixtures; -use Gloudemans\Shoppingcart\Contracts\Buyable; use Gloudemans\Shoppingcart\CartItemOptions; +use Gloudemans\Shoppingcart\Contracts\Buyable; use Illuminate\Database\Eloquent\Model; -use Money\Money; use Money\Currency; +use Money\Money; class BuyableProduct extends Model implements Buyable { @@ -48,7 +48,7 @@ class BuyableProduct extends Model implements Buyable * * @return string */ - public function getBuyableDescription(CartItemOptions $options) : ?string + public function getBuyableDescription(CartItemOptions $options): ?string { return $this->name; } diff --git a/tests/Fixtures/ProductModel.php b/tests/Fixtures/ProductModel.php index 20c57f0..5e89dd2 100644 --- a/tests/Fixtures/ProductModel.php +++ b/tests/Fixtures/ProductModel.php @@ -8,7 +8,7 @@ class ProductModel extends Model { public $someValue = 'Some value'; - public function find($id) : self + public function find($id): self { return $this; }