From 0ae8b1afb8adbdc1fb83f86f0444ca51949ef76b Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 27 Feb 2018 14:23:34 -0800 Subject: [PATCH] CRM_Upgrade_Form - Move number/label mapping to CRM_Utils_EnglishNumber --- CRM/Upgrade/Form.php | 20 +---- CRM/Utils/EnglishNumber.php | 143 ++++++++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+), 19 deletions(-) create mode 100644 CRM/Utils/EnglishNumber.php diff --git a/CRM/Upgrade/Form.php b/CRM/Upgrade/Form.php index b0b7a3d852..8dc18bf484 100644 --- a/CRM/Upgrade/Form.php +++ b/CRM/Upgrade/Form.php @@ -68,24 +68,6 @@ class CRM_Upgrade_Form extends CRM_Core_Form { */ public $locales; - /** - * Number to string mapper. - * - * @var array - */ - static $_numberMap = array( - 0 => 'Zero', - 1 => 'One', - 2 => 'Two', - 3 => 'Three', - 4 => 'Four', - 5 => 'Five', - 6 => 'Six', - 7 => 'Seven', - 8 => 'Eight', - 9 => 'Nine', - ); - /** * Constructor for the basic form page. * @@ -137,7 +119,7 @@ class CRM_Upgrade_Form extends CRM_Core_Form { static $incrementalPhpObject = array(); $versionParts = explode('.', $version); - $versionName = self::$_numberMap[$versionParts[0]] . self::$_numberMap[$versionParts[1]]; + $versionName = CRM_Utils_EnglishNumber::toCamelCase($versionParts[0]) . CRM_Utils_EnglishNumber::toCamelCase($versionParts[1]); if (!array_key_exists($versionName, $incrementalPhpObject)) { $className = "CRM_Upgrade_Incremental_php_{$versionName}"; diff --git a/CRM/Utils/EnglishNumber.php b/CRM/Utils/EnglishNumber.php new file mode 100644 index 0000000000..3673eceaf6 --- /dev/null +++ b/CRM/Utils/EnglishNumber.php @@ -0,0 +1,143 @@ + 'Ninety', + 8 => 'Eighty', + 7 => 'Seventy', + 6 => 'Sixty', + 5 => 'Fifty', + 4 => 'Forty', + 3 => 'Thirty', + 2 => 'Twenty', + ); + + /** + * @param int $num + * Ex: 12 or 54. + * @param mixed $default + * The default value to return if we cannot determine an English representation. + * If omitted or NULL, throws an exception. + * Tip: If you want to support high values as numerals, just pass the number again. + * @return string + * Ex: 'Twelve' or 'FiftyFour'. + */ + public static function toCamelCase($num, $default = NULL) { + if (isset(self::$lowNumbers[$num])) { + return self::$lowNumbers[$num]; + } + + $tens = (int) ($num / 10); + $last = $num % 10; + if (isset(self::$intervalsOfTen[$tens])) { + if ($last == 0) { + return self::$intervalsOfTen[$tens]; + } + else { + return self::$intervalsOfTen[$tens] . self::$lowNumbers[$last]; + } + } + + if ($default === NULL) { + throw new \RuntimeException("Cannot convert number to English: " . (int) $num); + } + else { + return $default; + } + } + + /** + * @param int $num + * Ex: 12 or 54. + * @param mixed $default + * The default value to return if we cannot determine an English representation. + * If omitted or NULL, throws an exception. + * Tip: If you want to support high values as numerals, just pass the number again. + * @return string + * Ex: 'twelve' or 'fifty-four'. + */ + public static function toHyphen($num, $default = NULL) { + if (isset(self::$lowNumbers[$num])) { + return strtolower(self::$lowNumbers[$num]); + } + + $tens = (int) ($num / 10); + $last = $num % 10; + if (isset(self::$intervalsOfTen[$tens])) { + if ($last == 0) { + return strtolower(self::$intervalsOfTen[$tens]); + } + else { + return strtolower(self::$intervalsOfTen[$tens]) . '-' . strtolower(self::$lowNumbers[$last]); + } + } + + if ($default === NULL) { + throw new \RuntimeException("Cannot convert number to English: " . (int) $num); + } + else { + return $default; + } + } + +} -- 2.25.1