Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failing tests on edge based on number formatting by using the Num… #16215

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions CRM/Utils/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,12 @@ public static function format($amount, $currency = NULL, $format = NULL, $onlyNu
}

if ($onlyNumber) {
if ($valueFormat === '!%i') {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the default value maybe we should add some deprecation notice for other formats?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if that is the case then yes probably

$amount = self::formatNumericByNumberFormatter($amount);
}
// money_format() exists only in certain PHP install (CRM-650)
if (is_numeric($amount) and function_exists('money_format')) {
$amount = money_format($valueFormat, $amount);
$amount = self::formatNumericByFormat($amount, $valueFormat);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some standardisation / re-use of functions

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should if [..] function_exists('money_format') also be removed?

}
return $amount;
}
Expand All @@ -86,7 +89,12 @@ public static function format($amount, $currency = NULL, $format = NULL, $onlyNu
throw new CRM_Core_Exception("Invalid currency \"{$currency}\"");
}

$amount = self::formatNumericByFormat($amount, $valueFormat);
if ($valueFormat === '!%i') {
$amount = self::formatNumericByNumberFormatter($amount);
}
else {
$amount = self::formatNumericByFormat($amount, $valueFormat);
}
// If it contains tags, means that HTML was passed and the
// amount is already converted properly,
// so don't mess with it again.
Expand Down Expand Up @@ -176,7 +184,11 @@ public static function equals($value1, $value2, $currency) {
* @return string
*/
protected static function formatLocaleNumeric($amount) {
return self::formatNumericByFormat($amount, CRM_Core_Config::singleton()->moneyvalueformat);
$moneyValueFormat = CRM_Core_Config::singleton()->moneyvalueformat;
if ($moneyValueFormat === '%!i') {
return self::formatNumericByNumberFormatter($amount);
}
return self::formatNumericByFormat($amount, $moneyValueFormat);
}

/**
Expand Down Expand Up @@ -265,4 +277,16 @@ protected static function formatNumericByFormat($amount, $valueFormat) {
return $amount;
}

/**
* Format numeric part of currency by using the NumberFormatter class
* @param string $amount
* @param int $numberFormatterType
*
* @return string
*/
protected static function formatNumericByNumberFormatter($amount, $numberFormatterType = NumberFormatter::DECIMAL) {
$formatter = new NumberFormatter('en_US', $numberFormatterType);
return $formatter->format($amount);
}

}