Merge in 5.20
[civicrm-core.git] / CRM / Utils / EnglishNumber.php
CommitLineData
0ae8b1af
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
0ae8b1af 5 | |
bc77d7c0
TO
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 |
0ae8b1af
TO
9 +--------------------------------------------------------------------+
10 */
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
0ae8b1af
TO
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 */
23class CRM_Utils_EnglishNumber {
24
be2fb01f 25 protected static $lowNumbers = [
0ae8b1af
TO
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',
be2fb01f 46 ];
0ae8b1af 47
be2fb01f 48 protected static $intervalsOfTen = [
0ae8b1af
TO
49 9 => 'Ninety',
50 8 => 'Eighty',
51 7 => 'Seventy',
52 6 => 'Sixty',
53 5 => 'Fifty',
54 4 => 'Forty',
55 3 => 'Thirty',
56 2 => 'Twenty',
be2fb01f 57 ];
0ae8b1af
TO
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}