mirror of
https://github.com/kevin-DL/LaravelShoppingcart.git
synced 2026-01-23 23:51:25 +00:00
Made CartItem implement Jsonable, so it can be cast to json by laravel when building a response
This commit is contained in:
@@ -4,8 +4,9 @@ namespace Gloudemans\Shoppingcart;
|
|||||||
|
|
||||||
use Illuminate\Contracts\Support\Arrayable;
|
use Illuminate\Contracts\Support\Arrayable;
|
||||||
use Gloudemans\Shoppingcart\Contracts\Buyable;
|
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.
|
* The rowID of the cart item.
|
||||||
@@ -42,7 +43,6 @@ class CartItem implements Arrayable
|
|||||||
*/
|
*/
|
||||||
public $price;
|
public $price;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The options for this cart item.
|
* The options for this cart item.
|
||||||
*
|
*
|
||||||
@@ -347,30 +347,43 @@ class CartItem implements Arrayable
|
|||||||
'name' => $this->name,
|
'name' => $this->name,
|
||||||
'qty' => $this->qty,
|
'qty' => $this->qty,
|
||||||
'price' => $this->price,
|
'price' => $this->price,
|
||||||
'options' => $this->options,
|
'options' => $this->options->toArray(),
|
||||||
'tax' => $this->tax,
|
'tax' => $this->tax,
|
||||||
'subtotal' => $this->subtotal
|
'subtotal' => $this->subtotal
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the Formated number
|
* Convert the object to its JSON representation.
|
||||||
*
|
*
|
||||||
* @param $value
|
* @param int $options
|
||||||
* @param $decimals
|
* @return string
|
||||||
* @param $decimalPoint
|
*/
|
||||||
* @param $thousandSeperator
|
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
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function numberFormat($value, $decimals, $decimalPoint, $thousandSeperator)
|
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');
|
$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');
|
$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');
|
$thousandSeperator = is_null(config('cart.format.thousand_seperator')) ? ',' : config('cart.format.thousand_seperator');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
53
tests/CartItemTest.php
Normal file
53
tests/CartItemTest.php
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Gloudemans\Tests\Shoppingcart;
|
||||||
|
|
||||||
|
use Orchestra\Testbench\TestCase;
|
||||||
|
use Gloudemans\Shoppingcart\CartItem;
|
||||||
|
use Gloudemans\Shoppingcart\ShoppingcartServiceProvider;
|
||||||
|
|
||||||
|
class CartItemTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Set the package service provider.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Foundation\Application $app
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getPackageProviders($app)
|
||||||
|
{
|
||||||
|
return [ShoppingcartServiceProvider::class];
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function it_can_be_cast_to_an_array()
|
||||||
|
{
|
||||||
|
$cartItem = new CartItem(1, 'Some item', 10.00, ['size' => 'XL', 'color' => 'red']);
|
||||||
|
|
||||||
|
$this->assertEquals([
|
||||||
|
'id' => 1,
|
||||||
|
'name' => 'Some item',
|
||||||
|
'price' => 10.00,
|
||||||
|
'rowId' => '07d5da5550494c62daf9993cf954303f',
|
||||||
|
'qty' => null,
|
||||||
|
'options' => [
|
||||||
|
'size' => 'XL',
|
||||||
|
'color' => 'red'
|
||||||
|
],
|
||||||
|
'tax' => 0,
|
||||||
|
'subtotal' => 0,
|
||||||
|
], $cartItem->toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function it_can_be_cast_to_json()
|
||||||
|
{
|
||||||
|
$cartItem = new CartItem(1, 'Some item', 10.00, ['size' => 'XL', 'color' => 'red']);
|
||||||
|
|
||||||
|
$this->assertJson($cartItem->toJson());
|
||||||
|
|
||||||
|
$json = '{"rowId":"07d5da5550494c62daf9993cf954303f","id":1,"name":"Some item","qty":null,"price":10,"options":{"size":"XL","color":"red"},"tax":0,"subtotal":0}';
|
||||||
|
|
||||||
|
$this->assertEquals($json, $cartItem->toJson());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -66,7 +66,6 @@ class CartTest extends TestCase
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function it_has_a_default_instance()
|
public function it_has_a_default_instance()
|
||||||
{
|
{
|
||||||
@@ -489,7 +488,7 @@ class CartTest extends TestCase
|
|||||||
'price' => 10.00,
|
'price' => 10.00,
|
||||||
'tax' => 2.10,
|
'tax' => 2.10,
|
||||||
'subtotal' => 10.0,
|
'subtotal' => 10.0,
|
||||||
'options' => new CartItemOptions,
|
'options' => [],
|
||||||
],
|
],
|
||||||
'370d08585360f5c568b18d1f2e4ca1df' => [
|
'370d08585360f5c568b18d1f2e4ca1df' => [
|
||||||
'rowId' => '370d08585360f5c568b18d1f2e4ca1df',
|
'rowId' => '370d08585360f5c568b18d1f2e4ca1df',
|
||||||
@@ -499,7 +498,7 @@ class CartTest extends TestCase
|
|||||||
'price' => 10.00,
|
'price' => 10.00,
|
||||||
'tax' => 2.10,
|
'tax' => 2.10,
|
||||||
'subtotal' => 10.0,
|
'subtotal' => 10.0,
|
||||||
'options' => new CartItemOptions,
|
'options' => [],
|
||||||
]
|
]
|
||||||
], $content->toArray());
|
], $content->toArray());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user