mirror of
https://github.com/kevin-DL/LaravelShoppingcart.git
synced 2026-01-22 15:15:27 +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:
@@ -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 {}
|
||||
Reference in New Issue
Block a user