Skip to content

Commit

Permalink
Merge pull request #202 from jorgejavierleon/config_number_format
Browse files Browse the repository at this point in the history
Set default number format in config file
  • Loading branch information
Crinsane authored Sep 5, 2016
2 parents 537f7c3 + 03c9fb1 commit 476f248
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 21 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ The method will automatically format the result, which you can tweak using the t
Cart::total($decimals, $decimalSeperator, $thousandSeperator);
```

You can set the default number format in the config file.

**If you're not using the Facade, but use dependency injection in your (for instance) Controller, you can also simply get the total property `$cart->total`**

### Cart::tax()
Expand All @@ -187,6 +189,8 @@ The method will automatically format the result, which you can tweak using the t
Cart::tax($decimals, $decimalSeperator, $thousandSeperator);
```

You can set the default number format in the config file.

**If you're not using the Facade, but use dependency injection in your (for instance) Controller, you can also simply get the tax property `$cart->tax`**

### Cart::subtotal()
Expand All @@ -203,6 +207,8 @@ The method will automatically format the result, which you can tweak using the t
Cart::subtotal($decimals, $decimalSeperator, $thousandSeperator);
```

You can set the default number format in the config file.

**If you're not using the Facade, but use dependency injection in your (for instance) Controller, you can also simply get the subtotal property `$cart->subtotal`**

### Cart::count()
Expand Down
20 changes: 20 additions & 0 deletions config/cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,24 @@

'destroy_on_logout' => false,

/*
|--------------------------------------------------------------------------
| Default number format
|--------------------------------------------------------------------------
|
| This defaults will be used for the formated numbers if you don't
| set them in the method call.
|
*/

'format' => [

'decimals' => 2,

'decimal_point' => '.',

'thousand_seperator' => ','

],

];
36 changes: 27 additions & 9 deletions src/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,15 +232,15 @@ public function count()
* @param string $thousandSeperator
* @return string
*/
public function total($decimals = 2, $decimalPoint = '.', $thousandSeperator = ',')
public function total($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
$content = $this->getContent();

$total = $content->reduce(function ($total, CartItem $cartItem) {
return $total + ($cartItem->qty * $cartItem->priceTax);
}, 0);

return number_format($total, $decimals, $decimalPoint, $thousandSeperator);
return $this->numberFormat($total, $decimals, $decimalPoint, $thousandSeperator);
}

/**
Expand All @@ -251,15 +251,15 @@ public function total($decimals = 2, $decimalPoint = '.', $thousandSeperator = '
* @param string $thousandSeperator
* @return float
*/
public function tax($decimals = 2, $decimalPoint = '.', $thousandSeperator = ',')
public function tax($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
$content = $this->getContent();

$tax = $content->reduce(function ($tax, CartItem $cartItem) {
return $tax + ($cartItem->qty * $cartItem->tax);
}, 0);

return number_format($tax, $decimals, $decimalPoint, $thousandSeperator);
return $this->numberFormat($tax, $decimals, $decimalPoint, $thousandSeperator);
}

/**
Expand All @@ -270,15 +270,15 @@ public function tax($decimals = 2, $decimalPoint = '.', $thousandSeperator = ','
* @param string $thousandSeperator
* @return float
*/
public function subtotal($decimals = 2, $decimalPoint = '.', $thousandSeperator = ',')
public function subtotal($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
$content = $this->getContent();

$subTotal = $content->reduce(function ($subTotal, CartItem $cartItem) {
return $subTotal + ($cartItem->qty * $cartItem->price);
}, 0);

return number_format($subTotal, $decimals, $decimalPoint, $thousandSeperator);
return $this->numberFormat($subTotal, $decimals, $decimalPoint, $thousandSeperator);
}

/**
Expand Down Expand Up @@ -407,15 +407,15 @@ public function restore($identifier)
public function __get($attribute)
{
if($attribute === 'total') {
return $this->total(2, '.', '');
return $this->total();
}

if($attribute === 'tax') {
return $this->tax(2, '.', '');
return $this->tax();
}

if($attribute === 'subtotal') {
return $this->subtotal(2, '.', '');
return $this->subtotal();
}

return null;
Expand Down Expand Up @@ -519,4 +519,22 @@ private function getConnectionName()

return is_null($connection) ? config('database.default') : $connection;
}

/**
* Get the Formated number
*
* @param $value
* @param $decimals
* @param $decimalPoint
* @param $thousandSeperator
* @return string
*/
private function numberFormat($value, $decimals, $decimalPoint, $thousandSeperator)
{
$decimals = $decimals ?: config('cart.format.decimals') ?: 2;
$decimalPoint = $decimalPoint ?: config('cart.format.decimal_point') ?: '.';
$thousandSeperator = $thousandSeperator ?: config('cart.format.thousand_seperator') ?: ',';

return number_format($value, $decimals, $decimalPoint, $thousandSeperator);
}
}
42 changes: 30 additions & 12 deletions src/CartItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ public function __construct($id, $name, $price, array $options = [])
* @param string $thousandSeperator
* @return string
*/
public function price($decimals = 2, $decimalPoint = '.', $thousandSeperator = ',')
public function price($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
return number_format($this->price, $decimals, $decimalPoint, $thousandSeperator);
return $this->numberFormat($this->price, $decimals, $decimalPoint, $thousandSeperator);
}

/**
Expand All @@ -112,9 +112,9 @@ public function price($decimals = 2, $decimalPoint = '.', $thousandSeperator = '
* @param string $thousandSeperator
* @return string
*/
public function priceTax($decimals = 2, $decimalPoint = '.', $thousandSeperator = ',')
public function priceTax($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
return number_format($this->priceTax, $decimals, $decimalPoint, $thousandSeperator);
return $this->numberFormat($this->priceTax, $decimals, $decimalPoint, $thousandSeperator);
}

/**
Expand All @@ -126,9 +126,9 @@ public function priceTax($decimals = 2, $decimalPoint = '.', $thousandSeperator
* @param string $thousandSeperator
* @return string
*/
public function subtotal($decimals = 2, $decimalPoint = '.', $thousandSeperator = ',')
public function subtotal($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
return number_format($this->subtotal, $decimals, $decimalPoint, $thousandSeperator);
return $this->numberFormat($this->subtotal, $decimals, $decimalPoint, $thousandSeperator);
}

/**
Expand All @@ -140,9 +140,9 @@ public function subtotal($decimals = 2, $decimalPoint = '.', $thousandSeperator
* @param string $thousandSeperator
* @return string
*/
public function total($decimals = 2, $decimalPoint = '.', $thousandSeperator = ',')
public function total($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
return number_format($this->total, $decimals, $decimalPoint, $thousandSeperator);
return $this->numberFormat($this->total, $decimals, $decimalPoint, $thousandSeperator);
}

/**
Expand All @@ -153,9 +153,9 @@ public function total($decimals = 2, $decimalPoint = '.', $thousandSeperator = '
* @param string $thousandSeperator
* @return string
*/
public function tax($decimals = 2, $decimalPoint = '.', $thousandSeperator = ',')
public function tax($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
return number_format($this->tax, $decimals, $decimalPoint, $thousandSeperator);
return $this->numberFormat($this->tax, $decimals, $decimalPoint, $thousandSeperator);
}

/**
Expand All @@ -166,9 +166,9 @@ public function tax($decimals = 2, $decimalPoint = '.', $thousandSeperator = ','
* @param string $thousandSeperator
* @return string
*/
public function taxTotal($decimals = 2, $decimalPoint = '.', $thousandSeperator = ',')
public function taxTotal($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
return number_format($this->taxTotal, $decimals, $decimalPoint, $thousandSeperator);
return $this->numberFormat($this->taxTotal, $decimals, $decimalPoint, $thousandSeperator);
}

/**
Expand Down Expand Up @@ -348,4 +348,22 @@ public function toArray()
'subtotal' => $this->subtotal
];
}

/**
* Get the Formated number
*
* @param $value
* @param $decimals
* @param $decimalPoint
* @param $thousandSeperator
* @return string
*/
private function numberFormat($value, $decimals, $decimalPoint, $thousandSeperator)
{
$decimals = $decimals ?: config('cart.format.decimals') ?: 2;
$decimalPoint = $decimalPoint ?: config('cart.format.decimal_point') ?: '.';
$thousandSeperator = $thousandSeperator ?: config('cart.format.thousand_seperator') ?: ',';

return number_format($value, $decimals, $decimalPoint, $thousandSeperator);
}
}
56 changes: 56 additions & 0 deletions tests/CartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,48 @@ public function it_can_return_formatted_subtotal()
$this->assertEquals('5.000,00', $cart->subtotal(2, ',', '.'));
}

/** @test */
public function it_can_return_cart_formated_numbers_by_config_values()
{
$this->setConfigFormat(2, ',', '.');

$cart = $this->getCart();

$item = $this->getBuyableMock(1, 'Some title', 1000.00);
$item2 = $this->getBuyableMock(2, 'Some title', 2000.00);

$cart->add($item, 1);
$cart->add($item2, 2);

$this->assertEquals('5.000,00', $cart->subtotal());
$this->assertEquals('1.050,00', $cart->tax());
$this->assertEquals('6.050,00', $cart->total());

$this->assertEquals('5.000,00', $cart->subtotal);
$this->assertEquals('1.050,00', $cart->tax);
$this->assertEquals('6.050,00', $cart->total);
}

/** @test */
public function it_can_return_cartItem_formated_numbers_by_config_values()
{
$this->setConfigFormat(2, ',', '.');

$cart = $this->getCart();
$item = $this->getBuyableMock(1, 'Some title', 2000.00);

$cart->add($item, 2);

$cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da');

$this->assertEquals('2.000,00', $cartItem->price());
$this->assertEquals('2.420,00', $cartItem->priceTax());
$this->assertEquals('4.000,00', $cartItem->subtotal());
$this->assertEquals('4.840,00', $cartItem->total());
$this->assertEquals('420,00', $cartItem->tax());
$this->assertEquals('840,00', $cartItem->taxTotal());
}

/** @test */
public function it_can_store_the_cart_in_a_database()
{
Expand Down Expand Up @@ -958,6 +1000,20 @@ private function getBuyableMock($id = 1, $name = 'Item name', $price = 10.00)

return $item;
}

/**
* Set the config number format
*
* @param $decimals
* @param $decimalPoint
* @param $thousandSeperator
*/
private function setConfigFormat($decimals, $decimalPoint, $thousandSeperator)
{
$this->app['config']->set('cart.format.decimals', $decimals);
$this->app['config']->set('cart.format.decimal_point', $decimalPoint);
$this->app['config']->set('cart.format.thousand_seperator', $thousandSeperator);
}
}

class ModelStub {
Expand Down

0 comments on commit 476f248

Please sign in to comment.