Skip to content

Commit

Permalink
Updating to use 7.4 and to use number formatter (#267)
Browse files Browse the repository at this point in the history
* Updating to use 7.4 and to use number formatter

* Apply fixes from StyleCI

[ci skip] [skip ci]
  • Loading branch information
lukepolo authored May 1, 2020
1 parent 696de54 commit 450b5fa
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 46 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ language: php
php:
- 7.2
- 7.3
- 7.4

addons:
code_climate:
Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
}
],
"require": {
"php": "~7.2 || ~7.3",
"illuminate/support": "~5.5.0|~5.7.0|~5.8.0|^6.0|^7.0",
"illuminate/session": "~5.5.0|~5.7.0|~5.8.0|^6.0|^7.0",
"illuminate/events": "~5.5.0|~5.7.0|~5.8.0|^6.0|^7.0"
"php": "~7.2 || ~7.3 || ~7.4",
"ext-intl" : "*",
"illuminate/support": "~5.5.0|~5.7.0|~5.8.0|^6.0|^7.0|^8.0",
"illuminate/session": "~5.5.0|~5.7.0|~5.8.0|^6.0|^7.0|^8.0",
"illuminate/events": "~5.5.0|~5.7.0|~5.8.0|^6.0|^7.0|^8.0"
},
"require-dev": {
"phpunit/phpunit": "^7.5",
Expand Down
3 changes: 1 addition & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
stopOnFailure="false">
<testsuites>
<testsuite name="Application Test Suite">
<directory>tests</directory>
Expand Down
4 changes: 2 additions & 2 deletions src/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Cart
public $coupons = [];
public $attributes = [];
public $multipleCoupons;
public $internationalFormat;
public $currencyCode;

/**
* Cart constructor.
Expand All @@ -28,6 +28,6 @@ public function __construct($instance = 'default')
$this->tax = config('laracart.tax');
$this->locale = config('laracart.locale');
$this->multipleCoupons = config('laracart.multiple_coupons');
$this->internationalFormat = config('laracart.international_format');
$this->currencyCode = config('laracart.currency_code');
}
}
4 changes: 2 additions & 2 deletions src/CartFee.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CartFee
public $amount;
public $taxable;
public $tax;
public $internationalFormat;
public $currencyCode;

/**
* CartFee constructor.
Expand Down Expand Up @@ -48,6 +48,6 @@ public function getAmount($format = true, $withTax = false)
$total += $this->tax * $total;
}

return LaraCart::formatMoney($total, $this->locale, $this->internationalFormat, $format);
return LaraCart::formatMoney($total, $this->locale, $this->currencyCode, $format);
}
}
16 changes: 8 additions & 8 deletions src/CartItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class CartItem
public $active = true;
public $subItems = [];
public $couponInfo = [];
public $internationalFormat;
public $currencyCode;

/**
* CartItem constructor.
Expand Down Expand Up @@ -201,7 +201,7 @@ public function price($format = true, $taxedItemsOnly = false, $withTax = false)
return LaraCart::formatMoney(
$total,
$this->locale,
$this->internationalFormat,
$this->currencyCode,
$format
);
}
Expand Down Expand Up @@ -232,7 +232,7 @@ public function subTotal($format = true, $withDiscount = true, $taxedItemsOnly =
$total += $this->tax(0, false, false, $withDiscount);
}

return LaraCart::formatMoney($total, $this->locale, $this->internationalFormat, $format);
return LaraCart::formatMoney($total, $this->locale, $this->currencyCode, $format);
}

/**
Expand All @@ -245,7 +245,7 @@ public function netTotal($format = true)
return LaraCart::formatMoney(
($this->price(false, false, true) * $this->qty) - $this->getDiscount(false) - $this->tax(false, true),
$this->locale,
$this->internationalFormat,
$this->currencyCode,
$format
);
}
Expand All @@ -271,7 +271,7 @@ public function subItemsTotal($format = true, $taxedItemsOnly = false, $withTax
$total += $this->tax * $total;
}

return LaraCart::formatMoney($total, $this->locale, $this->internationalFormat, $format);
return LaraCart::formatMoney($total, $this->locale, $this->currencyCode, $format);
}

/**
Expand Down Expand Up @@ -300,7 +300,7 @@ public function getDiscount($format = true)
return LaraCart::formatMoney(
$amount,
$this->locale,
$this->internationalFormat,
$this->currencyCode,
$format
);
}
Expand Down Expand Up @@ -358,7 +358,7 @@ public function tax($amountNotTaxable = 0, $grossTax = true, $rounded = false, $
return LaraCart::formatMoney(
($totalTax - $amountNotTaxable),
$this->locale,
$this->internationalFormat,
$this->currencyCode,
false
);
}
Expand All @@ -376,7 +376,7 @@ public function tax($amountNotTaxable = 0, $grossTax = true, $rounded = false, $
return LaraCart::formatMoney(
$totalTax,
$this->locale,
$this->internationalFormat,
$this->currencyCode,
false
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/CartSubItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CartSubItem
const ITEMS = 'items';

public $locale;
public $internationalFormat;
public $currencyCode;

private $itemHash;

Expand Down Expand Up @@ -68,7 +68,7 @@ public function price($format = true, $taxedItemsOnly = true)
}
}

return LaraCart::formatMoney($price, $this->locale, $this->internationalFormat, $format);
return LaraCart::formatMoney($price, $this->locale, $this->currencyCode, $format);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Contracts/LaraCartContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,16 +256,16 @@ public function subTotal($format = true, $withDiscount = true);
public function count($withItemQty = true);

/**
* Formats the number into a money format based on the locale and international formats.
* Formats the number into a money format based on the locale and currency formats.
*
* @param $number
* @param $locale
* @param $internationalFormat
* @param $currencyCode
* @param $format
*
* @return string
*/
public static function formatMoney($number, $locale = null, $internationalFormat = null, $format = true);
public static function formatMoney($number, $locale = null, $currencyCode = null, $format = true);

/**
* Gets all the fee totals.
Expand Down
6 changes: 3 additions & 3 deletions src/Coupons/Fixed.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,16 @@ public function forItem(CartItem $item)
* Displays the value in a money format.
*
* @param null $locale
* @param null $internationalFormat
* @param null $currencyCode
*
* @return string
*/
public function displayValue($locale = null, $internationalFormat = null, $format = true)
public function displayValue($locale = null, $currencyCode = null, $format = true)
{
return LaraCart::formatMoney(
$this->discount(),
$locale,
$internationalFormat,
$currencyCode,
$format
);
}
Expand Down
16 changes: 6 additions & 10 deletions src/LaraCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use LukePOLO\LaraCart\Contracts\CouponContract;
use LukePOLO\LaraCart\Contracts\LaraCartContract;
use LukePOLO\LaraCart\Exceptions\ModelNotFound;
use NumberFormatter;

/**
* Class LaraCart.
Expand Down Expand Up @@ -708,16 +709,16 @@ public function count($withItemQty = true)
}

/**
* Formats the number into a money format based on the locale and international formats.
* Formats the number into a money format based on the locale and currency formats.
*
* @param $number
* @param $locale
* @param $internationalFormat
* @param $currencyCode
* @param $format
*
* @return string
*/
public static function formatMoney($number, $locale = null, $internationalFormat = false, $format = true)
public static function formatMoney($number, $locale = null, $currencyCode = null, $format = true)
{
// When prices in cents needs to be formatted, divide by 100 to allow formatting in whole units
if (config('laracart.prices_in_cents', false) === true && $format) {
Expand All @@ -730,14 +731,9 @@ public static function formatMoney($number, $locale = null, $internationalFormat
}

if ($format) {
setlocale(LC_MONETARY, null);
setlocale(LC_MONETARY, empty($locale) ? config('laracart.locale', 'en_US.UTF-8') : $locale);
$moneyFormatter = new NumberFormatter(empty($locale) ? config('laracart.locale', 'en_US.UTF-8') : $locale, NumberFormatter::CURRENCY);

if (empty($internationalFormat) === true) {
$internationalFormat = config('laracart.international_format', false);
}

$number = money_format($internationalFormat ? '%i' : '%n', $number);
$number = $moneyFormatter->formatCurrency($number, empty($currencyCode) ? config('laracart.currency_code', 'USD') : $currencyCode);
}

return $number;
Expand Down
6 changes: 4 additions & 2 deletions src/config/laracart.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@

/*
|--------------------------------------------------------------------------
| If true, displays the international format rather than the national format
| The currency code changes how you see the actual amounts.
|--------------------------------------------------------------------------
| This is the list of all valid currency codes
| https://www2.1010data.com/documentationcenter/prod/1010dataReferenceManual/DataTypesAndFormats/currencyUnitCodes.html
|
*/
'international_format' => false,
'currency_code' => 'USD',

/*
|--------------------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions tests/Coupons/Fixed.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,16 @@ public function forItem(CartItem $item)
* Displays the value in a money format.
*
* @param null $locale
* @param null $internationalFormat
* @param null $currencyCode
*
* @return string
*/
public function displayValue($locale = null, $internationalFormat = null)
public function displayValue($locale = null, $currencyCode = null)
{
return LaraCart::formatMoney(
$this->discount(),
$locale,
$internationalFormat
$currencyCode
);
}
}
4 changes: 2 additions & 2 deletions tests/LaraCartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function testGetInstances()
public function testFormatMoney()
{
$this->assertEquals('$25.00', $this->laracart->formatMoney('25.00'));
$this->assertEquals('USD 25.00', $this->laracart->formatMoney('25.00', null, true));
$this->assertEquals('25.00', $this->laracart->formatMoney('25.00', null, 'EUR'));
$this->assertEquals('25.00', $this->laracart->formatMoney('25.00', null, null, false));

$this->assertEquals('$25.56', $this->laracart->formatMoney('25.555'));
Expand All @@ -72,7 +72,7 @@ public function testFormatMoneyPricesInCents()
$this->app['config']->set('laracart.prices_in_cents', true);

$this->assertEquals('$25.00', $this->laracart->formatMoney(2500));
$this->assertEquals('USD 25.00', $this->laracart->formatMoney(2500, null, true));
$this->assertEquals('CA$25.00', $this->laracart->formatMoney(2500, null, 'CAD'));
$this->assertEquals(2500, $this->laracart->formatMoney(2500, null, null, false));

$this->assertEquals('$25.01', $this->laracart->formatMoney(2500.55));
Expand Down
5 changes: 2 additions & 3 deletions tests/Models/TestItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace LukePOLO\LaraCart\Tests\Models;

use Illuminate\Database\Eloquent\Model;
use Mockery;

/**
* Class TestItem.
Expand Down Expand Up @@ -32,7 +31,7 @@ public function find($id)
return;
}

return Mockery::mock(new static());
return new static();
}

/**
Expand All @@ -48,6 +47,6 @@ public static function with($relations)
$relations = func_get_args();
}

return Mockery::mock(new static());
return new static();
}
}

0 comments on commit 450b5fa

Please sign in to comment.