Commit | Line | Data |
---|---|---|
0ae8b1af TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
0ae8b1af | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
0ae8b1af TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
0ae8b1af TO |
32 | * $Id$ |
33 | * | |
34 | * Utilities for rendering numbers as English. | |
35 | * | |
36 | * Note: This file may be used in a standalone environment. Please ensure it | |
37 | * remains self-sufficient (without needing any external services). | |
38 | */ | |
39 | class CRM_Utils_EnglishNumber { | |
40 | ||
be2fb01f | 41 | protected static $lowNumbers = [ |
0ae8b1af TO |
42 | 'Zero', |
43 | 'One', | |
44 | 'Two', | |
45 | 'Three', | |
46 | 'Four', | |
47 | 'Five', | |
48 | 'Six', | |
49 | 'Seven', | |
50 | 'Eight', | |
51 | 'Nine', | |
52 | 'Ten', | |
53 | 'Eleven', | |
54 | 'Twelve', | |
55 | 'Thirteen', | |
56 | 'Fourteen', | |
57 | 'Fifteen', | |
58 | 'Sixteen', | |
59 | 'Seventeen', | |
60 | 'Eighteen', | |
61 | 'Nineteen', | |
be2fb01f | 62 | ]; |
0ae8b1af | 63 | |
be2fb01f | 64 | protected static $intervalsOfTen = [ |
0ae8b1af TO |
65 | 9 => 'Ninety', |
66 | 8 => 'Eighty', | |
67 | 7 => 'Seventy', | |
68 | 6 => 'Sixty', | |
69 | 5 => 'Fifty', | |
70 | 4 => 'Forty', | |
71 | 3 => 'Thirty', | |
72 | 2 => 'Twenty', | |
be2fb01f | 73 | ]; |
0ae8b1af TO |
74 | |
75 | /** | |
76 | * @param int $num | |
77 | * Ex: 12 or 54. | |
78 | * @param mixed $default | |
79 | * The default value to return if we cannot determine an English representation. | |
80 | * If omitted or NULL, throws an exception. | |
81 | * Tip: If you want to support high values as numerals, just pass the number again. | |
82 | * @return string | |
83 | * Ex: 'Twelve' or 'FiftyFour'. | |
84 | */ | |
85 | public static function toCamelCase($num, $default = NULL) { | |
86 | if (isset(self::$lowNumbers[$num])) { | |
87 | return self::$lowNumbers[$num]; | |
88 | } | |
89 | ||
90 | $tens = (int) ($num / 10); | |
91 | $last = $num % 10; | |
92 | if (isset(self::$intervalsOfTen[$tens])) { | |
93 | if ($last == 0) { | |
94 | return self::$intervalsOfTen[$tens]; | |
95 | } | |
96 | else { | |
97 | return self::$intervalsOfTen[$tens] . self::$lowNumbers[$last]; | |
98 | } | |
99 | } | |
100 | ||
101 | if ($default === NULL) { | |
102 | throw new \RuntimeException("Cannot convert number to English: " . (int) $num); | |
103 | } | |
104 | else { | |
105 | return $default; | |
106 | } | |
107 | } | |
108 | ||
109 | /** | |
110 | * @param int $num | |
111 | * Ex: 12 or 54. | |
112 | * @param mixed $default | |
113 | * The default value to return if we cannot determine an English representation. | |
114 | * If omitted or NULL, throws an exception. | |
115 | * Tip: If you want to support high values as numerals, just pass the number again. | |
116 | * @return string | |
117 | * Ex: 'twelve' or 'fifty-four'. | |
118 | */ | |
119 | public static function toHyphen($num, $default = NULL) { | |
120 | if (isset(self::$lowNumbers[$num])) { | |
121 | return strtolower(self::$lowNumbers[$num]); | |
122 | } | |
123 | ||
124 | $tens = (int) ($num / 10); | |
125 | $last = $num % 10; | |
126 | if (isset(self::$intervalsOfTen[$tens])) { | |
127 | if ($last == 0) { | |
128 | return strtolower(self::$intervalsOfTen[$tens]); | |
129 | } | |
130 | else { | |
131 | return strtolower(self::$intervalsOfTen[$tens]) . '-' . strtolower(self::$lowNumbers[$last]); | |
132 | } | |
133 | } | |
134 | ||
135 | if ($default === NULL) { | |
136 | throw new \RuntimeException("Cannot convert number to English: " . (int) $num); | |
137 | } | |
138 | else { | |
139 | return $default; | |
140 | } | |
141 | } | |
142 | ||
143 | } |