From 160a2e3bd4fc29eabdad0b0a82cda8aada5b9aee Mon Sep 17 00:00:00 2001 From: Rob Gloudemans Date: Mon, 30 Dec 2013 22:02:59 +0100 Subject: [PATCH] Removed addBatch method, the add method should be used. Also added events to the cart --- README.md | 12 +++++ src/Gloudemans/Shoppingcart/Cart.php | 45 +++++++++++++------ .../ShoppingcartServiceProvider.php | 4 +- 3 files changed, 46 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 5c9e2e3..b6ee4d9 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ Look at one of the following topics to learn more about LaravelShoppingcart * [Collections](#collections) * [Instances](#instances) * [Exceptions](#exceptions) +* [Events](#events) * [Example](#example) ## Usage @@ -241,6 +242,17 @@ The Cart package will throw exceptions if something goes wrong. This way it's ea | *ShoppingcartInvalidQtyException* | When a not numeric quantity is passed | | *ShoppingcartInvalidRowIDException* | When the rowId that got passed doesn't exists in the current cart | +## Events + +The cart also has events build in. There are five events available for you to listen for. + +| Event | Fired | +|----------------------------------------------------------------| +| cart.add($item) | When a single item is added | +| cart.batch($items) | When a batch if items is added | +| cart.update($rowId) | When an item in the cart is updated | +| cart.remove($rowId) | When an item is removed from the cart | +| cart.destroy() | When the cart is destroyed | ## Example diff --git a/src/Gloudemans/Shoppingcart/Cart.php b/src/Gloudemans/Shoppingcart/Cart.php index ae798bc..53da40c 100644 --- a/src/Gloudemans/Shoppingcart/Cart.php +++ b/src/Gloudemans/Shoppingcart/Cart.php @@ -11,6 +11,13 @@ class Cart { */ protected $session; + /** + * Event class instance + * + * @var Event + */ + protected $event; + /** * Current cart instance * @@ -22,10 +29,12 @@ class Cart { * Constructor * * @param Session $session Session class instance + * @param Event $event Event class instance */ - public function __construct($session) + public function __construct($session, $event) { $this->session = $session; + $this->event = $event; $this->instance = 'main'; } @@ -64,6 +73,9 @@ class Cart { // recursively call the add function if($this->is_multi($id)) { + // Fire the cart.batch event + $this->event->fire('cart.batch', $id); + foreach($id as $item) { $options = isset($item['options']) ? $item['options'] : array(); @@ -74,22 +86,17 @@ class Cart { } $options = isset($id['options']) ? $id['options'] : array(); + + // Fire the cart.add event + $this->event->fire('cart.add', array_merge($id, array('options' => $options))); + return $this->addRow($id['id'], $id['name'], $id['qty'], $id['price'], $options); } - return $this->addRow($id, $name, $qty, $price, $options); - } + // Fire the cart.add event + $this->event->fire('cart.add', compact('id', 'name', 'qty', 'price', 'options')); - /** - * Add multiple rows to the cart - * Maps to add() function - * Will probably be removed in future versions - * - * @param Array $items An array of items to add, use array keys corresponding to the 'add' method's parameters - */ - public function addBatch(Array $items) - { - return $this->add($items); + return $this->addRow($id, $name, $qty, $price, $options); } /** @@ -105,9 +112,15 @@ class Cart { if(is_array($attribute)) { + // Fire the cart.update event + $this->event->fire('cart.update', $rowId); + return $this->updateAttribute($rowId, $attribute); } + // Fire the cart.update event + $this->event->fire('cart.update', $rowId); + return $this->updateQty($rowId, $attribute); } @@ -123,6 +136,9 @@ class Cart { $cart = $this->getContent(); + // Fire the cart.remove event + $this->event->fire('cart.remove', $rowId); + $cart->forget($rowId); return $this->updateCart($cart); @@ -160,6 +176,9 @@ class Cart { */ public function destroy() { + // Fire the cart.destroy event + $this->event->fire('cart.destroy'); + return $this->updateCart(NULL); } diff --git a/src/Gloudemans/Shoppingcart/ShoppingcartServiceProvider.php b/src/Gloudemans/Shoppingcart/ShoppingcartServiceProvider.php index 6077f1e..0d5654f 100644 --- a/src/Gloudemans/Shoppingcart/ShoppingcartServiceProvider.php +++ b/src/Gloudemans/Shoppingcart/ShoppingcartServiceProvider.php @@ -11,11 +11,11 @@ class ShoppingcartServiceProvider extends ServiceProvider { */ public function register() { - $this->app['cart'] = $this->app->share(function($app) { $session = $app['session']; - return new Cart($session); + $event = $app['events']; + return new Cart($session, $event); }); } } \ No newline at end of file