From: Coleman Watts Date: Sat, 8 Jan 2022 22:10:02 +0000 (-0500) Subject: Fix input type for smarty number formatting X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=c1c02f438999fc647696656f5f2e347c9ea597ec;p=civicrm-core.git Fix input type for smarty number formatting Some smarty templates pass NULL to crmMoney, so it needs to handle that possibilty --- diff --git a/CRM/Core/Smarty/plugins/modifier.crmMoney.php b/CRM/Core/Smarty/plugins/modifier.crmMoney.php index cc40306789..ec5a6a93b6 100644 --- a/CRM/Core/Smarty/plugins/modifier.crmMoney.php +++ b/CRM/Core/Smarty/plugins/modifier.crmMoney.php @@ -18,7 +18,7 @@ /** * Format the given monetary amount (and currency) for display * - * @param float $amount + * @param string|int|float $amount * The monetary amount up for display. * @param string|null $currency * The (optional) currency. diff --git a/Civi/Core/Format.php b/Civi/Core/Format.php index 52d85865a4..2ea33946ad 100644 --- a/Civi/Core/Format.php +++ b/Civi/Core/Format.php @@ -23,7 +23,7 @@ class Format { /** * Get formatted money * - * @param string $amount + * @param string|int|float $amount * @param string|null $currency * Currency, defaults to site currency if not provided. * @param string|null $locale @@ -33,7 +33,15 @@ class Format { * @noinspection PhpDocMissingThrowsInspection * @noinspection PhpUnhandledExceptionInspection */ - public function money(string $amount, ?string $currency = NULL, ?string $locale = NULL): string { + public function money($amount, ?string $currency = NULL, ?string $locale = NULL): string { + // Empty value => empty string + if (is_null($amount) || $amount === '' || $amount === FALSE) { + return ''; + } + // Verify the amount is a number or numeric string/object + elseif ($amount === TRUE || !is_numeric((string) $amount)) { + throw new \CRM_Core_Exception('Invalid value for type money'); + } if (!$currency) { $currency = Civi::settings()->get('defaultCurrency'); }