From 741608a997c8e9f2fa7605aba5d07a926960df71 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 8 Mar 2021 14:28:44 +1300 Subject: [PATCH] dev/translation#65 call brickmoney number formatting, where supported The main blocker to switching to brick money was actually the valueFormat parameter - now that it cannot be passed in we can switch to using the function that will prefer the brickmoney function (but fall back to the old one). It doesn't achieve the goal of making better use of brick money --- CRM/Utils/Money.php | 31 ++++++++++++++++++++++--------- composer.lock | 45 +++++++++++++++++++++++++++++---------------- 2 files changed, 51 insertions(+), 25 deletions(-) diff --git a/CRM/Utils/Money.php b/CRM/Utils/Money.php index c6a39f03cc..acf785d201 100644 --- a/CRM/Utils/Money.php +++ b/CRM/Utils/Money.php @@ -85,8 +85,7 @@ class CRM_Utils_Money { if ($currency === ' ') { CRM_Core_Error::deprecatedWarning('Passing empty currency to CRM_Utils_Money::format is deprecated if you need it for display without currency call CRM_Utils_Money::formatLocaleNumericRounded'); } - - $amount = self::formatNumericByFormat($amount); + $amount = self::formatUSLocaleNumericRounded($amount, 2); // If it contains tags, means that HTML was passed and the // amount is already converted properly, // so don't mess with it again. @@ -184,17 +183,19 @@ class CRM_Utils_Money { * * It's not totally clear when it changes the $amount value but has historical usage. * - * @param string $amount + * @param string|float $amount * @param int $numberOfPlaces * * @return string */ - protected static function formatLocaleNumericRounded($amount, $numberOfPlaces) { - if (!extension_loaded('intl')) { + protected static function formatUSLocaleNumericRounded($amount, int $numberOfPlaces): string { + if (!extension_loaded('intl') || !is_numeric($amount)) { + // @todo - we should not attempt to format non-numeric strings. For now + // these will not fail but will give notices on php 7.4 self::missingIntlNotice(); return self::formatNumericByFormat($amount, '%!.' . $numberOfPlaces . 'i'); } - $money = Money::of($amount, CRM_Core_Config::singleton()->defaultCurrency, new CustomContext($numberOfPlaces), RoundingMode::CEILING); + $money = Money::of($amount, CRM_Core_Config::singleton()->defaultCurrency, new CustomContext($numberOfPlaces), RoundingMode::HALF_UP); // @todo - we specify en_US here because we don't want this function to do // currency replacement at the moment because // formatLocaleNumericRoundedByPrecision is doing it and if it @@ -234,7 +235,7 @@ class CRM_Utils_Money { * Formatted amount. */ public static function formatLocaleNumericRoundedByPrecision($amount, $precision) { - $amount = self::formatLocaleNumericRounded($amount, $precision); + $amount = self::formatUSLocaleNumericRounded($amount, $precision); return self::replaceCurrencySeparators($amount); } @@ -253,8 +254,8 @@ class CRM_Utils_Money { * Formatted amount. */ public static function formatLocaleNumericRoundedByOptionalPrecision($amount, $precision) { - $decimalPlaces = strlen(substr($amount, strpos($amount, '.') + 1)); - $amount = self::formatLocaleNumericRounded($amount, $precision > $decimalPlaces ? $decimalPlaces : $precision); + $decimalPlaces = self::getDecimalPlacesForAmount((string) $amount); + $amount = self::formatUSLocaleNumericRounded($amount, $precision > $decimalPlaces ? $decimalPlaces : $precision); return self::replaceCurrencySeparators($amount); } @@ -316,4 +317,16 @@ class CRM_Utils_Money { CRM_Core_Session::singleton()->setStatus(ts('As this system does not include the PHP intl extension, CiviCRM has fallen back onto a slightly less accurate and deprecated method to format money'), ts('Missing PHP INTL extension')); } + /** + * Get the number of characters after the decimal point. + * + * @param string $amount + * + * @return int + */ + protected static function getDecimalPlacesForAmount(string $amount): int { + $decimalPlaces = strlen(substr($amount, strpos($amount, '.') + 1)); + return $decimalPlaces; + } + } diff --git a/composer.lock b/composer.lock index b8e248fbf5..eaa1bde94d 100644 --- a/composer.lock +++ b/composer.lock @@ -49,26 +49,26 @@ }, { "name": "brick/math", - "version": "0.8.15", + "version": "0.9.2", "source": { "type": "git", "url": "https://github.com/brick/math.git", - "reference": "9b08d412b9da9455b210459ff71414de7e6241cd" + "reference": "dff976c2f3487d42c1db75a3b180e2b9f0e72ce0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/brick/math/zipball/9b08d412b9da9455b210459ff71414de7e6241cd", - "reference": "9b08d412b9da9455b210459ff71414de7e6241cd", + "url": "https://api.github.com/repos/brick/math/zipball/dff976c2f3487d42c1db75a3b180e2b9f0e72ce0", + "reference": "dff976c2f3487d42c1db75a3b180e2b9f0e72ce0", "shasum": "" }, "require": { "ext-json": "*", - "php": "^7.1|^8.0" + "php": "^7.1 || ^8.0" }, "require-dev": { "php-coveralls/php-coveralls": "^2.2", - "phpunit/phpunit": "^7.5.15|^8.5", - "vimeo/psalm": "^3.5" + "phpunit/phpunit": "^7.5.15 || ^8.5 || ^9.0", + "vimeo/psalm": "4.3.2" }, "type": "library", "autoload": { @@ -91,32 +91,39 @@ "brick", "math" ], - "time": "2020-04-15T15:59:35+00:00" + "funding": [ + { + "url": "https://tidelift.com/funding/github/packagist/brick/math", + "type": "tidelift" + } + ], + "time": "2021-01-20T22:51:39+00:00" }, { "name": "brick/money", - "version": "0.4.5", + "version": "0.5.1", "source": { "type": "git", "url": "https://github.com/brick/money.git", - "reference": "91f2b5bc35646f172b038e46bb496ad18db59c3c" + "reference": "c6f2883c8a759bf7f77c79aaa6004af6d6c0afaf" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/brick/money/zipball/91f2b5bc35646f172b038e46bb496ad18db59c3c", - "reference": "91f2b5bc35646f172b038e46bb496ad18db59c3c", + "url": "https://api.github.com/repos/brick/money/zipball/c6f2883c8a759bf7f77c79aaa6004af6d6c0afaf", + "reference": "c6f2883c8a759bf7f77c79aaa6004af6d6c0afaf", "shasum": "" }, "require": { - "brick/math": "~0.7.3 || ~0.8.0", - "php": "^7.1|^8.0" + "brick/math": "~0.7.3 || ~0.8.0 || ~0.9.0", + "php": "^7.1 || ^8.0" }, "require-dev": { "brick/varexporter": "~0.2.1", "ext-dom": "*", "ext-pdo": "*", "php-coveralls/php-coveralls": "^2.2", - "phpunit/phpunit": "^7.5.15" + "phpunit/phpunit": "^7.5.15 || ^8.0 || ^9.0", + "vimeo/psalm": "4.3.2" }, "suggest": { "ext-intl": "Required to format Money objects" @@ -137,7 +144,13 @@ "currency", "money" ], - "time": "2020-05-31T14:17:02+00:00" + "funding": [ + { + "url": "https://tidelift.com/funding/github/packagist/brick/money", + "type": "tidelift" + } + ], + "time": "2021-02-10T14:14:29+00:00" }, { "name": "cache/integration-tests", -- 2.25.1