| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 6 | | This work is published under the GNU AGPLv3 license with some | |
| 7 | | permitted exceptions and without any warranty. For full license | |
| 8 | | and copyright information, see https://civicrm.org/licensing | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * |
| 14 | * @package CRM |
| 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 16 | * $Id$ |
| 17 | * |
| 18 | * Utilities for rendering numbers as English. |
| 19 | * |
| 20 | * Note: This file may be used in a standalone environment. Please ensure it |
| 21 | * remains self-sufficient (without needing any external services). |
| 22 | */ |
| 23 | class CRM_Utils_EnglishNumber { |
| 24 | |
| 25 | protected static $lowNumbers = [ |
| 26 | 'Zero', |
| 27 | 'One', |
| 28 | 'Two', |
| 29 | 'Three', |
| 30 | 'Four', |
| 31 | 'Five', |
| 32 | 'Six', |
| 33 | 'Seven', |
| 34 | 'Eight', |
| 35 | 'Nine', |
| 36 | 'Ten', |
| 37 | 'Eleven', |
| 38 | 'Twelve', |
| 39 | 'Thirteen', |
| 40 | 'Fourteen', |
| 41 | 'Fifteen', |
| 42 | 'Sixteen', |
| 43 | 'Seventeen', |
| 44 | 'Eighteen', |
| 45 | 'Nineteen', |
| 46 | ]; |
| 47 | |
| 48 | protected static $intervalsOfTen = [ |
| 49 | 9 => 'Ninety', |
| 50 | 8 => 'Eighty', |
| 51 | 7 => 'Seventy', |
| 52 | 6 => 'Sixty', |
| 53 | 5 => 'Fifty', |
| 54 | 4 => 'Forty', |
| 55 | 3 => 'Thirty', |
| 56 | 2 => 'Twenty', |
| 57 | ]; |
| 58 | |
| 59 | /** |
| 60 | * @param int $num |
| 61 | * Ex: 12 or 54. |
| 62 | * @param mixed $default |
| 63 | * The default value to return if we cannot determine an English representation. |
| 64 | * If omitted or NULL, throws an exception. |
| 65 | * Tip: If you want to support high values as numerals, just pass the number again. |
| 66 | * @return string |
| 67 | * Ex: 'Twelve' or 'FiftyFour'. |
| 68 | */ |
| 69 | public static function toCamelCase($num, $default = NULL) { |
| 70 | if (isset(self::$lowNumbers[$num])) { |
| 71 | return self::$lowNumbers[$num]; |
| 72 | } |
| 73 | |
| 74 | $tens = (int) ($num / 10); |
| 75 | $last = $num % 10; |
| 76 | if (isset(self::$intervalsOfTen[$tens])) { |
| 77 | if ($last == 0) { |
| 78 | return self::$intervalsOfTen[$tens]; |
| 79 | } |
| 80 | else { |
| 81 | return self::$intervalsOfTen[$tens] . self::$lowNumbers[$last]; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if ($default === NULL) { |
| 86 | throw new \RuntimeException("Cannot convert number to English: " . (int) $num); |
| 87 | } |
| 88 | else { |
| 89 | return $default; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @param int $num |
| 95 | * Ex: 12 or 54. |
| 96 | * @param mixed $default |
| 97 | * The default value to return if we cannot determine an English representation. |
| 98 | * If omitted or NULL, throws an exception. |
| 99 | * Tip: If you want to support high values as numerals, just pass the number again. |
| 100 | * @return string |
| 101 | * Ex: 'twelve' or 'fifty-four'. |
| 102 | */ |
| 103 | public static function toHyphen($num, $default = NULL) { |
| 104 | if (isset(self::$lowNumbers[$num])) { |
| 105 | return strtolower(self::$lowNumbers[$num]); |
| 106 | } |
| 107 | |
| 108 | $tens = (int) ($num / 10); |
| 109 | $last = $num % 10; |
| 110 | if (isset(self::$intervalsOfTen[$tens])) { |
| 111 | if ($last == 0) { |
| 112 | return strtolower(self::$intervalsOfTen[$tens]); |
| 113 | } |
| 114 | else { |
| 115 | return strtolower(self::$intervalsOfTen[$tens]) . '-' . strtolower(self::$lowNumbers[$last]); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | if ($default === NULL) { |
| 120 | throw new \RuntimeException("Cannot convert number to English: " . (int) $num); |
| 121 | } |
| 122 | else { |
| 123 | return $default; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | } |