Skip to content

Commit 03c9fb1

Browse files
Set default number format in config file
1 parent 71cb7ce commit 03c9fb1

File tree

5 files changed

+139
-21
lines changed

5 files changed

+139
-21
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ The method will automatically format the result, which you can tweak using the t
171171
Cart::total($decimals, $decimalSeperator, $thousandSeperator);
172172
```
173173

174+
You can set the default number format in the config file.
175+
174176
**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`**
175177

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

192+
You can set the default number format in the config file.
193+
190194
**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`**
191195

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

210+
You can set the default number format in the config file.
211+
206212
**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`**
207213

208214
### Cart::count()

config/cart.php

+20
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,24 @@
4444

4545
'destroy_on_logout' => false,
4646

47+
/*
48+
|--------------------------------------------------------------------------
49+
| Default number format
50+
|--------------------------------------------------------------------------
51+
|
52+
| This defaults will be used for the formated numbers if you don't
53+
| set them in the method call.
54+
|
55+
*/
56+
57+
'format' => [
58+
59+
'decimals' => 2,
60+
61+
'decimal_point' => '.',
62+
63+
'thousand_seperator' => ','
64+
65+
],
66+
4767
];

src/Cart.php

+27-9
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,15 @@ public function count()
232232
* @param string $thousandSeperator
233233
* @return string
234234
*/
235-
public function total($decimals = 2, $decimalPoint = '.', $thousandSeperator = ',')
235+
public function total($decimals = null, $decimalPoint = null, $thousandSeperator = null)
236236
{
237237
$content = $this->getContent();
238238

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

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

246246
/**
@@ -251,15 +251,15 @@ public function total($decimals = 2, $decimalPoint = '.', $thousandSeperator = '
251251
* @param string $thousandSeperator
252252
* @return float
253253
*/
254-
public function tax($decimals = 2, $decimalPoint = '.', $thousandSeperator = ',')
254+
public function tax($decimals = null, $decimalPoint = null, $thousandSeperator = null)
255255
{
256256
$content = $this->getContent();
257257

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

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

265265
/**
@@ -270,15 +270,15 @@ public function tax($decimals = 2, $decimalPoint = '.', $thousandSeperator = ','
270270
* @param string $thousandSeperator
271271
* @return float
272272
*/
273-
public function subtotal($decimals = 2, $decimalPoint = '.', $thousandSeperator = ',')
273+
public function subtotal($decimals = null, $decimalPoint = null, $thousandSeperator = null)
274274
{
275275
$content = $this->getContent();
276276

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

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

284284
/**
@@ -407,15 +407,15 @@ public function restore($identifier)
407407
public function __get($attribute)
408408
{
409409
if($attribute === 'total') {
410-
return $this->total(2, '.', '');
410+
return $this->total();
411411
}
412412

413413
if($attribute === 'tax') {
414-
return $this->tax(2, '.', '');
414+
return $this->tax();
415415
}
416416

417417
if($attribute === 'subtotal') {
418-
return $this->subtotal(2, '.', '');
418+
return $this->subtotal();
419419
}
420420

421421
return null;
@@ -519,4 +519,22 @@ private function getConnectionName()
519519

520520
return is_null($connection) ? config('database.default') : $connection;
521521
}
522+
523+
/**
524+
* Get the Formated number
525+
*
526+
* @param $value
527+
* @param $decimals
528+
* @param $decimalPoint
529+
* @param $thousandSeperator
530+
* @return string
531+
*/
532+
private function numberFormat($value, $decimals, $decimalPoint, $thousandSeperator)
533+
{
534+
$decimals = $decimals ?: config('cart.format.decimals') ?: 2;
535+
$decimalPoint = $decimalPoint ?: config('cart.format.decimal_point') ?: '.';
536+
$thousandSeperator = $thousandSeperator ?: config('cart.format.thousand_seperator') ?: ',';
537+
538+
return number_format($value, $decimals, $decimalPoint, $thousandSeperator);
539+
}
522540
}

src/CartItem.php

+30-12
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ public function __construct($id, $name, $price, array $options = [])
9999
* @param string $thousandSeperator
100100
* @return string
101101
*/
102-
public function price($decimals = 2, $decimalPoint = '.', $thousandSeperator = ',')
102+
public function price($decimals = null, $decimalPoint = null, $thousandSeperator = null)
103103
{
104-
return number_format($this->price, $decimals, $decimalPoint, $thousandSeperator);
104+
return $this->numberFormat($this->price, $decimals, $decimalPoint, $thousandSeperator);
105105
}
106106

107107
/**
@@ -112,9 +112,9 @@ public function price($decimals = 2, $decimalPoint = '.', $thousandSeperator = '
112112
* @param string $thousandSeperator
113113
* @return string
114114
*/
115-
public function priceTax($decimals = 2, $decimalPoint = '.', $thousandSeperator = ',')
115+
public function priceTax($decimals = null, $decimalPoint = null, $thousandSeperator = null)
116116
{
117-
return number_format($this->priceTax, $decimals, $decimalPoint, $thousandSeperator);
117+
return $this->numberFormat($this->priceTax, $decimals, $decimalPoint, $thousandSeperator);
118118
}
119119

120120
/**
@@ -126,9 +126,9 @@ public function priceTax($decimals = 2, $decimalPoint = '.', $thousandSeperator
126126
* @param string $thousandSeperator
127127
* @return string
128128
*/
129-
public function subtotal($decimals = 2, $decimalPoint = '.', $thousandSeperator = ',')
129+
public function subtotal($decimals = null, $decimalPoint = null, $thousandSeperator = null)
130130
{
131-
return number_format($this->subtotal, $decimals, $decimalPoint, $thousandSeperator);
131+
return $this->numberFormat($this->subtotal, $decimals, $decimalPoint, $thousandSeperator);
132132
}
133133

134134
/**
@@ -140,9 +140,9 @@ public function subtotal($decimals = 2, $decimalPoint = '.', $thousandSeperator
140140
* @param string $thousandSeperator
141141
* @return string
142142
*/
143-
public function total($decimals = 2, $decimalPoint = '.', $thousandSeperator = ',')
143+
public function total($decimals = null, $decimalPoint = null, $thousandSeperator = null)
144144
{
145-
return number_format($this->total, $decimals, $decimalPoint, $thousandSeperator);
145+
return $this->numberFormat($this->total, $decimals, $decimalPoint, $thousandSeperator);
146146
}
147147

148148
/**
@@ -153,9 +153,9 @@ public function total($decimals = 2, $decimalPoint = '.', $thousandSeperator = '
153153
* @param string $thousandSeperator
154154
* @return string
155155
*/
156-
public function tax($decimals = 2, $decimalPoint = '.', $thousandSeperator = ',')
156+
public function tax($decimals = null, $decimalPoint = null, $thousandSeperator = null)
157157
{
158-
return number_format($this->tax, $decimals, $decimalPoint, $thousandSeperator);
158+
return $this->numberFormat($this->tax, $decimals, $decimalPoint, $thousandSeperator);
159159
}
160160

161161
/**
@@ -166,9 +166,9 @@ public function tax($decimals = 2, $decimalPoint = '.', $thousandSeperator = ','
166166
* @param string $thousandSeperator
167167
* @return string
168168
*/
169-
public function taxTotal($decimals = 2, $decimalPoint = '.', $thousandSeperator = ',')
169+
public function taxTotal($decimals = null, $decimalPoint = null, $thousandSeperator = null)
170170
{
171-
return number_format($this->taxTotal, $decimals, $decimalPoint, $thousandSeperator);
171+
return $this->numberFormat($this->taxTotal, $decimals, $decimalPoint, $thousandSeperator);
172172
}
173173

174174
/**
@@ -348,4 +348,22 @@ public function toArray()
348348
'subtotal' => $this->subtotal
349349
];
350350
}
351+
352+
/**
353+
* Get the Formated number
354+
*
355+
* @param $value
356+
* @param $decimals
357+
* @param $decimalPoint
358+
* @param $thousandSeperator
359+
* @return string
360+
*/
361+
private function numberFormat($value, $decimals, $decimalPoint, $thousandSeperator)
362+
{
363+
$decimals = $decimals ?: config('cart.format.decimals') ?: 2;
364+
$decimalPoint = $decimalPoint ?: config('cart.format.decimal_point') ?: '.';
365+
$thousandSeperator = $thousandSeperator ?: config('cart.format.thousand_seperator') ?: ',';
366+
367+
return number_format($value, $decimals, $decimalPoint, $thousandSeperator);
368+
}
351369
}

tests/CartTest.php

+56
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,48 @@ public function it_can_return_formatted_subtotal()
792792
$this->assertEquals('5.000,00', $cart->subtotal(2, ',', '.'));
793793
}
794794

795+
/** @test */
796+
public function it_can_return_cart_formated_numbers_by_config_values()
797+
{
798+
$this->setConfigFormat(2, ',', '.');
799+
800+
$cart = $this->getCart();
801+
802+
$item = $this->getBuyableMock(1, 'Some title', 1000.00);
803+
$item2 = $this->getBuyableMock(2, 'Some title', 2000.00);
804+
805+
$cart->add($item, 1);
806+
$cart->add($item2, 2);
807+
808+
$this->assertEquals('5.000,00', $cart->subtotal());
809+
$this->assertEquals('1.050,00', $cart->tax());
810+
$this->assertEquals('6.050,00', $cart->total());
811+
812+
$this->assertEquals('5.000,00', $cart->subtotal);
813+
$this->assertEquals('1.050,00', $cart->tax);
814+
$this->assertEquals('6.050,00', $cart->total);
815+
}
816+
817+
/** @test */
818+
public function it_can_return_cartItem_formated_numbers_by_config_values()
819+
{
820+
$this->setConfigFormat(2, ',', '.');
821+
822+
$cart = $this->getCart();
823+
$item = $this->getBuyableMock(1, 'Some title', 2000.00);
824+
825+
$cart->add($item, 2);
826+
827+
$cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da');
828+
829+
$this->assertEquals('2.000,00', $cartItem->price());
830+
$this->assertEquals('2.420,00', $cartItem->priceTax());
831+
$this->assertEquals('4.000,00', $cartItem->subtotal());
832+
$this->assertEquals('4.840,00', $cartItem->total());
833+
$this->assertEquals('420,00', $cartItem->tax());
834+
$this->assertEquals('840,00', $cartItem->taxTotal());
835+
}
836+
795837
/** @test */
796838
public function it_can_store_the_cart_in_a_database()
797839
{
@@ -958,6 +1000,20 @@ private function getBuyableMock($id = 1, $name = 'Item name', $price = 10.00)
9581000

9591001
return $item;
9601002
}
1003+
1004+
/**
1005+
* Set the config number format
1006+
*
1007+
* @param $decimals
1008+
* @param $decimalPoint
1009+
* @param $thousandSeperator
1010+
*/
1011+
private function setConfigFormat($decimals, $decimalPoint, $thousandSeperator)
1012+
{
1013+
$this->app['config']->set('cart.format.decimals', $decimals);
1014+
$this->app['config']->set('cart.format.decimal_point', $decimalPoint);
1015+
$this->app['config']->set('cart.format.thousand_seperator', $thousandSeperator);
1016+
}
9611017
}
9621018

9631019
class ModelStub {

0 commit comments

Comments
 (0)