3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 * Utilities for rendering numbers as English.
18 * Note: This file may be used in a standalone environment. Please ensure it
19 * remains self-sufficient (without needing any external services).
21 class CRM_Utils_EnglishNumber
{
23 protected static $lowNumbers = [
46 protected static $intervalsOfTen = [
60 * @param mixed $default
61 * The default value to return if we cannot determine an English representation.
62 * If omitted or NULL, throws an exception.
63 * Tip: If you want to support high values as numerals, just pass the number again.
65 * Ex: 'Twelve' or 'FiftyFour'.
67 public static function toCamelCase($num, $default = NULL) {
68 if (isset(self
::$lowNumbers[$num])) {
69 return self
::$lowNumbers[$num];
72 $tens = (int) ($num / 10);
74 if (isset(self
::$intervalsOfTen[$tens])) {
76 return self
::$intervalsOfTen[$tens];
79 return self
::$intervalsOfTen[$tens] . self
::$lowNumbers[$last];
83 if ($default === NULL) {
84 throw new \
RuntimeException("Cannot convert number to English: " . (int) $num);
94 * @param mixed $default
95 * The default value to return if we cannot determine an English representation.
96 * If omitted or NULL, throws an exception.
97 * Tip: If you want to support high values as numerals, just pass the number again.
99 * Ex: 'twelve' or 'fifty-four'.
101 public static function toHyphen($num, $default = NULL) {
102 if (isset(self
::$lowNumbers[$num])) {
103 return strtolower(self
::$lowNumbers[$num]);
106 $tens = (int) ($num / 10);
108 if (isset(self
::$intervalsOfTen[$tens])) {
110 return strtolower(self
::$intervalsOfTen[$tens]);
113 return strtolower(self
::$intervalsOfTen[$tens]) . '-' . strtolower(self
::$lowNumbers[$last]);
117 if ($default === NULL) {
118 throw new \
RuntimeException("Cannot convert number to English: " . (int) $num);