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);
126 * Convert an English-style number to an int.
128 * @param string $english
129 * Ex: 'TwentyTwo' or 'forty-four'
134 public static function toInt(string $english) {
136 $strBuf = strtolower(str_replace('-', '', $english));
138 foreach (self
::$intervalsOfTen as $num => $name) {
139 if (CRM_Utils_String
::startsWith($strBuf, strtolower($name))) {
140 $intBuf +
= 10 * $num;
141 $strBuf = substr($strBuf, strlen($name));
145 foreach (array_reverse(self
::$lowNumbers, TRUE) as $num => $name) {
146 if (CRM_Utils_String
::startsWith($strBuf, strtolower($name))) {
148 $strBuf = substr($strBuf, strlen($name));
153 if (!empty($strBuf)) {
154 throw new InvalidArgumentException("Failed to parse english number: $strBuf");
161 * Determine if a string looks like
163 * @param string $english
167 public static function isNumeric(string $english): bool {
172 return preg_quote(strtolower($w));
174 array_merge(array_values(self
::$lowNumbers), array_values(self
::$intervalsOfTen))
176 $pat = '/^(\-|' . implode('|', $words) . ')+$/';
178 return (bool) preg_match($pat, strtolower($english));