Made CartItem implement Jsonable, so it can be cast to json by laravel when building a response

This commit is contained in:
Rob Gloudemans
2017-05-21 16:38:33 +02:00
parent eca90e3287
commit 39de887e09
3 changed files with 79 additions and 14 deletions

View File

@@ -4,8 +4,9 @@ namespace Gloudemans\Shoppingcart;
use Illuminate\Contracts\Support\Arrayable;
use Gloudemans\Shoppingcart\Contracts\Buyable;
use Illuminate\Contracts\Support\Jsonable;
class CartItem implements Arrayable
class CartItem implements Arrayable, Jsonable
{
/**
* The rowID of the cart item.
@@ -41,7 +42,6 @@ class CartItem implements Arrayable
* @var float
*/
public $price;
/**
* The options for this cart item.
@@ -347,30 +347,43 @@ class CartItem implements Arrayable
'name' => $this->name,
'qty' => $this->qty,
'price' => $this->price,
'options' => $this->options,
'options' => $this->options->toArray(),
'tax' => $this->tax,
'subtotal' => $this->subtotal
];
}
/**
* Get the Formated number
* Convert the object to its JSON representation.
*
* @param $value
* @param $decimals
* @param $decimalPoint
* @param $thousandSeperator
* @param int $options
* @return string
*/
public function toJson($options = 0)
{
return json_encode($this->toArray(), $options);
}
/**
* Get the formatted number.
*
* @param float $value
* @param int $decimals
* @param string $decimalPoint
* @param string $thousandSeperator
* @return string
*/
private function numberFormat($value, $decimals, $decimalPoint, $thousandSeperator)
{
if(is_null($decimals)){
if (is_null($decimals)){
$decimals = is_null(config('cart.format.decimals')) ? 2 : config('cart.format.decimals');
}
if(is_null($decimalPoint)){
if (is_null($decimalPoint)){
$decimalPoint = is_null(config('cart.format.decimal_point')) ? '.' : config('cart.format.decimal_point');
}
if(is_null($thousandSeperator)){
if (is_null($thousandSeperator)){
$thousandSeperator = is_null(config('cart.format.thousand_seperator')) ? ',' : config('cart.format.thousand_seperator');
}