mirror of
https://github.com/kevin-DL/LaravelShoppingcart.git
synced 2026-01-20 22:35:14 +00:00
Added a few exceptions that will be thrown at the time of errors. Makes for easier debugging. Also updated README accordingly
This commit is contained in:
14
README.md
14
README.md
@@ -17,7 +17,7 @@ Now all you have to do is add the service provider of the package and alias the
|
||||
|
||||
Add a new line to the `service providers` array:
|
||||
|
||||
'\Gloudemans\Shoppingcart\ShoppingcartServiceProvider'
|
||||
'Gloudemans\Shoppingcart\ShoppingcartServiceProvider'
|
||||
|
||||
And finally add a new line to the `aliases` array:
|
||||
|
||||
@@ -221,6 +221,18 @@ N.B. Keep in mind that the cart stays in the last set instance for as long as yo
|
||||
|
||||
N.B.2 The default cart instance is called `main`, so when you're not using instances,`Cart::content();` is the same as `Cart::instance('main')->content()`.
|
||||
|
||||
## Exceptions
|
||||
The Cart package will throw exceptions if something goes wrong. This way it's easier to debug your code using the Cart package or to handle the error based on the type of exceptions. The Cart packages can throw the following exceptions:
|
||||
|
||||
| Exception | Reason |
|
||||
| ------------------------------------- | ------------------------------------------------------------------------ |
|
||||
| *ShoppingcartInstanceException* | When no instance is passed to the instance() method |
|
||||
| *ShoppingcartInvalidItemException* | When a new product misses one of it's arguments (id, name, qty, price) |
|
||||
| *ShoppingcartInvalidPriceException* | When a not numeric price is passed |
|
||||
| *ShoppingcartInvalidQtyException* | When a not numeric quantity is passed |
|
||||
| *ShoppingcartInvalidRowIDException* | When the rowId that got passed doesn't exists in the current cart |
|
||||
|
||||
|
||||
## Example
|
||||
|
||||
Below is a little example of how to list the cart content in a table:
|
||||
|
||||
@@ -36,8 +36,10 @@ class Cart {
|
||||
* @param string $instance Cart instance name
|
||||
* @return Cart
|
||||
*/
|
||||
public function instance($instance)
|
||||
public function instance($instance = null)
|
||||
{
|
||||
if(empty($instance)) throw new Exceptions\ShoppingcartInstanceException;
|
||||
|
||||
$this->instance = $instance;
|
||||
|
||||
// Return self so the method is chainable
|
||||
@@ -99,6 +101,8 @@ class Cart {
|
||||
*/
|
||||
public function update($rowId, $attribute)
|
||||
{
|
||||
if( ! $this->hasRowId($rowId)) throw new Exceptions\ShoppingcartInvalidRowIDException;
|
||||
|
||||
if(is_array($attribute))
|
||||
{
|
||||
return $this->updateAttribute($rowId, $attribute);
|
||||
@@ -115,6 +119,8 @@ class Cart {
|
||||
*/
|
||||
public function remove($rowId)
|
||||
{
|
||||
if( ! $this->hasRowId($rowId)) throw new Exceptions\ShoppingcartInvalidRowIDException;
|
||||
|
||||
$cart = $this->getContent();
|
||||
|
||||
$cart->forget($rowId);
|
||||
@@ -237,6 +243,21 @@ class Cart {
|
||||
*/
|
||||
protected function addRow($id, $name, $qty, $price, Array $options = array())
|
||||
{
|
||||
if(empty($id) || empty($name) || empty($qty) || empty($price))
|
||||
{
|
||||
throw new Exceptions\ShoppingcartInvalidItemException;
|
||||
}
|
||||
|
||||
if( ! is_numeric($qty))
|
||||
{
|
||||
throw new Exceptions\ShoppingcartInvalidQtyException;
|
||||
}
|
||||
|
||||
if( ! is_numeric($price))
|
||||
{
|
||||
throw new Exceptions\ShoppingcartInvalidPriceException;
|
||||
}
|
||||
|
||||
$cart = $this->getContent();
|
||||
|
||||
$rowId = $this->generateRowId($id, $options);
|
||||
@@ -268,6 +289,17 @@ class Cart {
|
||||
return md5($id . serialize($options));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a rowid exists in the current cart instance
|
||||
*
|
||||
* @param string $id Unique ID of the item
|
||||
* @return boolean
|
||||
*/
|
||||
protected function hasRowId($rowId)
|
||||
{
|
||||
return $this->getContent()->has($rowId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the cart
|
||||
*
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
<?php namespace Gloudemans\Shoppingcart\Exceptions;
|
||||
|
||||
class ShoppingcartInstanceException extends \Exception {}
|
||||
@@ -0,0 +1,3 @@
|
||||
<?php namespace Gloudemans\Shoppingcart\Exceptions;
|
||||
|
||||
class ShoppingcartInvalidItemException extends \Exception {}
|
||||
@@ -0,0 +1,3 @@
|
||||
<?php namespace Gloudemans\Shoppingcart\Exceptions;
|
||||
|
||||
class ShoppingcartInvalidPriceException extends \Exception {}
|
||||
@@ -0,0 +1,3 @@
|
||||
<?php namespace Gloudemans\Shoppingcart\Exceptions;
|
||||
|
||||
class ShoppingcartInvalidQtyException extends \Exception {}
|
||||
@@ -0,0 +1,3 @@
|
||||
<?php namespace Gloudemans\Shoppingcart\Exceptions;
|
||||
|
||||
class ShoppingcartInvalidRowIDException extends \Exception {}
|
||||
@@ -233,4 +233,46 @@ class CartTest extends TestCase {
|
||||
$this->assertEquals($search, array($row1));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Gloudemans\Shoppingcart\Exceptions\ShoppingcartInstanceException
|
||||
*/
|
||||
public function testShoppingcartInstanceException()
|
||||
{
|
||||
Cart::instance();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Gloudemans\Shoppingcart\Exceptions\ShoppingcartInvalidItemException
|
||||
*/
|
||||
public function testShoppingcartInvalidItemException()
|
||||
{
|
||||
Cart::add(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Gloudemans\Shoppingcart\Exceptions\ShoppingcartInvalidQtyException
|
||||
*/
|
||||
public function testShoppingcartInvalidQtyException()
|
||||
{
|
||||
Cart::add(1, 'Product 1', 'nonnumeric', 10.00);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Gloudemans\Shoppingcart\Exceptions\ShoppingcartInvalidPriceException
|
||||
*/
|
||||
public function testShoppingcartInvalidPriceException()
|
||||
{
|
||||
Cart::add(1, 'Product 1', 1, 'nonnumeric');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Gloudemans\Shoppingcart\Exceptions\ShoppingcartInvalidRowIDException
|
||||
*/
|
||||
public function testShoppingcartInvalidRowIDException()
|
||||
{
|
||||
Cart::add(1, 'Product 1', 1, 10.00);
|
||||
|
||||
Cart::update('nonexistingrowid', 2);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user