diff --git a/src/CartItem.php b/src/CartItem.php index 3532768..4e2e328 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -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'); } diff --git a/tests/CartItemTest.php b/tests/CartItemTest.php new file mode 100644 index 0000000..63e7acb --- /dev/null +++ b/tests/CartItemTest.php @@ -0,0 +1,53 @@ + '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()); + } +} \ No newline at end of file diff --git a/tests/CartTest.php b/tests/CartTest.php index 360ad7d..5745909 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -66,7 +66,6 @@ class CartTest extends TestCase }); } - /** @test */ public function it_has_a_default_instance() { @@ -489,7 +488,7 @@ class CartTest extends TestCase 'price' => 10.00, 'tax' => 2.10, 'subtotal' => 10.0, - 'options' => new CartItemOptions, + 'options' => [], ], '370d08585360f5c568b18d1f2e4ca1df' => [ 'rowId' => '370d08585360f5c568b18d1f2e4ca1df', @@ -499,7 +498,7 @@ class CartTest extends TestCase 'price' => 10.00, 'tax' => 2.10, 'subtotal' => 10.0, - 'options' => new CartItemOptions, + 'options' => [], ] ], $content->toArray()); }