mrprompt / shopping-cart
A reusable shopping cart implementation
dev-master
2018-12-30 23:32 UTC
Requires
- php: ~7.1
Requires (Dev)
- pdepend/pdepend: ^2.5
- phing/phing: ^2.16
- phploc/phploc: ^4.0
- phpmd/phpmd: ^2.6
- phpunit/phpunit: ^7.4
- sebastian/phpcpd: ^4.1
- squizlabs/php_codesniffer: ^3.3
This package is auto-updated.
Last update: 2024-11-29 05:44:53 UTC
README
A reusable shopping cart implementation.
Install
composer require mrprompt/shopping-cart
Using
Initializing a cart with items
You can initializes a cart passing an array of items. An item is a instance of Item
class
or an implementation of ItemInterface
.
use \MrPrompt\ShoppingCart\Cart; use \MrPrompt\ShoppingCart\Item; $items = [ new Item(uniqid()), new Item(uniqid(), 1.00), new Item(uniqid(), 5.00, 10), ]; $cartId = uniqid(); $cart = new Cart($cartId, $items);
Add items to cart
You can add items to an existent cart passing an instance of Item
.
use \MrPrompt\ShoppingCart\Item; $itemId = uniqid(); $itemPrice = 1.99; $itemQuantity = 30; $item = new Item($itemId, $itemPrice, $itemQuantity); $cartId = uniqid(); $cart = new Cart($cartId); $cart->addItem($item);
Remove an item from cart
Pass an instance of Item
- previously added to cart obvously - to remove it.
use \MrPrompt\ShoppingCart\Item; $itemId = uniqid(); $itemPrice = 1.99; $itemQuantity = 30; $item = new Item($itemId, $itemPrice, $itemQuantity); $cartId = uniqid(); $cart = new Cart([ $item ]); $cart->removeItem($item);
Cleanup the cart
To remove all items from cart, you can use the cleanUp
method:
use \MrPrompt\ShoppingCart\Cart; use \MrPrompt\ShoppingCart\Item; $itemId = uniqid(); $itemPrice = 1.99; $itemQuantity = 30; $item = new Item($itemId, $itemPrice, $itemQuantity); $cartId = uniqid(); $cart = new Cart($cartId, [ $item ]); echo $cart->count(); // === 1 $cart->cleanUp(); echo $cart->count(); // === 0
Check if cart is empty
To check if the cart is empty, you can use isEmpty
method:
use \MrPrompt\ShoppingCart\Cart; use \MrPrompt\ShoppingCart\Item; $itemId = uniqid(); $itemPrice = 1.99; $itemQuantity = 30; $item = new Item($itemId, $itemPrice, $itemQuantity); $cartId = uniqid(); $cart = new Cart($cartId, [ $item ]); echo $cart->isEmpty(); // === false $cart->cleanUp(); echo $cart->isEmpty(); // === true
Testing
phpunit --coverage-text --testdox