From 450b5facc729a9fc0ee01e574767a39e6f888873 Mon Sep 17 00:00:00 2001 From: Luke Policinski Date: Fri, 1 May 2020 11:28:33 -0400 Subject: [PATCH] Updating to use 7.4 and to use number formatter (#267) * Updating to use 7.4 and to use number formatter * Apply fixes from StyleCI [ci skip] [skip ci] --- .travis.yml | 1 + composer.json | 9 +++++---- phpunit.xml | 3 +-- src/Cart.php | 4 ++-- src/CartFee.php | 4 ++-- src/CartItem.php | 16 ++++++++-------- src/CartSubItem.php | 4 ++-- src/Contracts/LaraCartContract.php | 6 +++--- src/Coupons/Fixed.php | 6 +++--- src/LaraCart.php | 16 ++++++---------- src/config/laracart.php | 6 ++++-- tests/Coupons/Fixed.php | 6 +++--- tests/LaraCartTest.php | 4 ++-- tests/Models/TestItem.php | 5 ++--- 14 files changed, 44 insertions(+), 46 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2594f94..b0accc0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,7 @@ language: php php: - 7.2 - 7.3 + - 7.4 addons: code_climate: diff --git a/composer.json b/composer.json index 3415eb9..04d048f 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/phpunit.xml b/phpunit.xml index 8db264f..023ef85 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -7,8 +7,7 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" - stopOnFailure="false" - syntaxCheck="false"> + stopOnFailure="false"> tests diff --git a/src/Cart.php b/src/Cart.php index 3c7e6ae..ab1fb2f 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -15,7 +15,7 @@ class Cart public $coupons = []; public $attributes = []; public $multipleCoupons; - public $internationalFormat; + public $currencyCode; /** * Cart constructor. @@ -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'); } } diff --git a/src/CartFee.php b/src/CartFee.php index 7c4e1af..d267188 100644 --- a/src/CartFee.php +++ b/src/CartFee.php @@ -15,7 +15,7 @@ class CartFee public $amount; public $taxable; public $tax; - public $internationalFormat; + public $currencyCode; /** * CartFee constructor. @@ -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); } } diff --git a/src/CartItem.php b/src/CartItem.php index 6a50f9c..004857d 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -40,7 +40,7 @@ class CartItem public $active = true; public $subItems = []; public $couponInfo = []; - public $internationalFormat; + public $currencyCode; /** * CartItem constructor. @@ -201,7 +201,7 @@ public function price($format = true, $taxedItemsOnly = false, $withTax = false) return LaraCart::formatMoney( $total, $this->locale, - $this->internationalFormat, + $this->currencyCode, $format ); } @@ -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); } /** @@ -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 ); } @@ -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); } /** @@ -300,7 +300,7 @@ public function getDiscount($format = true) return LaraCart::formatMoney( $amount, $this->locale, - $this->internationalFormat, + $this->currencyCode, $format ); } @@ -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 ); } @@ -376,7 +376,7 @@ public function tax($amountNotTaxable = 0, $grossTax = true, $rounded = false, $ return LaraCart::formatMoney( $totalTax, $this->locale, - $this->internationalFormat, + $this->currencyCode, false ); } diff --git a/src/CartSubItem.php b/src/CartSubItem.php index 450b7c8..2c982e2 100644 --- a/src/CartSubItem.php +++ b/src/CartSubItem.php @@ -19,7 +19,7 @@ class CartSubItem const ITEMS = 'items'; public $locale; - public $internationalFormat; + public $currencyCode; private $itemHash; @@ -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); } /** diff --git a/src/Contracts/LaraCartContract.php b/src/Contracts/LaraCartContract.php index 1613668..35a0fc1 100644 --- a/src/Contracts/LaraCartContract.php +++ b/src/Contracts/LaraCartContract.php @@ -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. diff --git a/src/Coupons/Fixed.php b/src/Coupons/Fixed.php index 46c7b60..6d3d805 100644 --- a/src/Coupons/Fixed.php +++ b/src/Coupons/Fixed.php @@ -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 ); } diff --git a/src/LaraCart.php b/src/LaraCart.php index 095618e..0883426 100644 --- a/src/LaraCart.php +++ b/src/LaraCart.php @@ -10,6 +10,7 @@ use LukePOLO\LaraCart\Contracts\CouponContract; use LukePOLO\LaraCart\Contracts\LaraCartContract; use LukePOLO\LaraCart\Exceptions\ModelNotFound; +use NumberFormatter; /** * Class LaraCart. @@ -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) { @@ -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; diff --git a/src/config/laracart.php b/src/config/laracart.php index b1dd87b..6454335 100644 --- a/src/config/laracart.php +++ b/src/config/laracart.php @@ -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', /* |-------------------------------------------------------------------------- diff --git a/tests/Coupons/Fixed.php b/tests/Coupons/Fixed.php index 95014f3..2dd10d5 100644 --- a/tests/Coupons/Fixed.php +++ b/tests/Coupons/Fixed.php @@ -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 ); } } diff --git a/tests/LaraCartTest.php b/tests/LaraCartTest.php index f72ba36..227f403 100644 --- a/tests/LaraCartTest.php +++ b/tests/LaraCartTest.php @@ -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')); @@ -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)); diff --git a/tests/Models/TestItem.php b/tests/Models/TestItem.php index 608ad4e..9f091b7 100644 --- a/tests/Models/TestItem.php +++ b/tests/Models/TestItem.php @@ -3,7 +3,6 @@ namespace LukePOLO\LaraCart\Tests\Models; use Illuminate\Database\Eloquent\Model; -use Mockery; /** * Class TestItem. @@ -32,7 +31,7 @@ public function find($id) return; } - return Mockery::mock(new static()); + return new static(); } /** @@ -48,6 +47,6 @@ public static function with($relations) $relations = func_get_args(); } - return Mockery::mock(new static()); + return new static(); } }