| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Numbers_Words
|
|
|
4 |
*
|
|
|
5 |
* PHP version 4
|
|
|
6 |
*
|
|
|
7 |
* Copyright (c) 1997-2006 The PHP Group
|
|
|
8 |
*
|
|
|
9 |
* This source file is subject to version 3.0 of the PHP license,
|
|
|
10 |
* that is bundled with this package in the file LICENSE, and is
|
|
|
11 |
* available at through the world-wide-web at
|
|
|
12 |
* http://www.php.net/license/3_0.txt.
|
|
|
13 |
* If you did not receive a copy of the PHP license and are unable to
|
|
|
14 |
* obtain it through the world-wide-web, please send a note to
|
|
|
15 |
* license@php.net so we can mail you a copy immediately.
|
|
|
16 |
*
|
|
|
17 |
* Authors: Piotr Klaban <makler@man.torun.pl>
|
|
|
18 |
*
|
|
|
19 |
* @category Numbers
|
|
|
20 |
* @package Numbers_Words
|
|
|
21 |
* @author Piotr Klaban <makler@man.torun.pl>
|
|
|
22 |
* @license PHP 3.0 http://www.php.net/license/3_0.txt
|
|
|
23 |
* @version CVS: $Id: Words.php 295090 2010-02-15 06:38:34Z clockwerx $
|
|
|
24 |
* @link http://pear.php.net/package/Numbers_Words
|
|
|
25 |
*/
|
|
|
26 |
|
|
|
27 |
// {{{ Numbers_Words
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* The Numbers_Words class provides method to convert arabic numerals to words.
|
|
|
31 |
*
|
|
|
32 |
* @category Numbers
|
|
|
33 |
* @package Numbers_Words
|
|
|
34 |
* @author Piotr Klaban <makler@man.torun.pl>
|
|
|
35 |
* @license PHP 3.0 http://www.php.net/license/3_0.txt
|
|
|
36 |
* @link http://pear.php.net/package/Numbers_Words
|
|
|
37 |
* @since PHP 4.2.3
|
|
|
38 |
* @access public
|
|
|
39 |
*/
|
|
|
40 |
class Numbers_Words
|
|
|
41 |
{
|
|
|
42 |
// {{{ toWords()
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Converts a number to its word representation
|
|
|
46 |
*
|
|
|
47 |
* @param integer $num An integer between -infinity and infinity inclusive :)
|
|
|
48 |
* that should be converted to a words representation
|
|
|
49 |
* @param string $locale Language name abbreviation. Optional. Defaults to en_US.
|
|
|
50 |
*
|
|
|
51 |
* @access public
|
|
|
52 |
* @author Piotr Klaban <makler@man.torun.pl>
|
|
|
53 |
* @since PHP 4.2.3
|
|
|
54 |
* @return string The corresponding word representation
|
|
|
55 |
*/
|
|
|
56 |
function toWords($num, $locale = 'en_US')
|
|
|
57 |
{
|
|
|
58 |
|
|
|
59 |
include_once "Numbers/Words/lang.${locale}.php";
|
|
|
60 |
|
|
|
61 |
$classname = "Numbers_Words_${locale}";
|
|
|
62 |
|
|
|
63 |
if (!class_exists($classname)) {
|
|
|
64 |
return Numbers_Words::raiseError("Unable to include the Numbers/Words/lang.${locale}.php file");
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
$methods = get_class_methods($classname);
|
|
|
68 |
|
|
|
69 |
if (!in_array('toWords', $methods) && !in_array('towords', $methods)) {
|
|
|
70 |
return Numbers_Words::raiseError("Unable to find toWords method in '$classname' class");
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
@$obj = new $classname;
|
|
|
74 |
|
|
|
75 |
if (!is_int($num)) {
|
|
|
76 |
// cast (sanitize) to int without losing precision
|
|
|
77 |
$num = preg_replace('/^[^\d]*?(-?)[ \t\n]*?(\d+)([^\d].*?)?$/', '$1$2', $num);
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
return trim($obj->toWords($num));
|
|
|
81 |
}
|
|
|
82 |
// }}}
|
|
|
83 |
|
|
|
84 |
// {{{ toCurrency()
|
|
|
85 |
/**
|
|
|
86 |
* Converts a currency value to word representation (1.02 => one dollar two cents)
|
|
|
87 |
* If the number has not any fraction part, the "cents" number is omitted.
|
|
|
88 |
*
|
|
|
89 |
* @param float $num A float/integer/string number representing currency value
|
|
|
90 |
*
|
|
|
91 |
* @param string $locale Language name abbreviation. Optional. Defaults to en_US.
|
|
|
92 |
*
|
|
|
93 |
* @param string $int_curr International currency symbol
|
|
|
94 |
* as defined by the ISO 4217 standard (three characters).
|
|
|
95 |
* E.g. 'EUR', 'USD', 'PLN'. Optional.
|
|
|
96 |
* Defaults to $def_currency defined in the language class.
|
|
|
97 |
*
|
|
|
98 |
* @return string The corresponding word representation
|
|
|
99 |
*
|
|
|
100 |
* @access public
|
|
|
101 |
* @author Piotr Klaban <makler@man.torun.pl>
|
|
|
102 |
* @since PHP 4.2.3
|
|
|
103 |
* @return string
|
|
|
104 |
*/
|
|
|
105 |
function toCurrency($num, $locale = 'en_US', $int_curr = '')
|
|
|
106 |
{
|
|
|
107 |
$ret = $num;
|
|
|
108 |
|
|
|
109 |
@include_once "Numbers/Words/lang.${locale}.php";
|
|
|
110 |
|
|
|
111 |
$classname = "Numbers_Words_${locale}";
|
|
|
112 |
|
|
|
113 |
if (!class_exists($classname)) {
|
|
|
114 |
return Numbers_Words::raiseError("Unable to include the Numbers/Words/lang.${locale}.php file");
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
$methods = get_class_methods($classname);
|
|
|
118 |
|
|
|
119 |
if (!in_array('toCurrencyWords', $methods) && !in_array('tocurrencywords', $methods)) {
|
|
|
120 |
return Numbers_Words::raiseError("Unable to find toCurrencyWords method in '$classname' class");
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
@$obj = new $classname;
|
|
|
124 |
|
|
|
125 |
// round if a float is passed, use Math_BigInteger otherwise
|
|
|
126 |
if (is_float($num)) {
|
|
|
127 |
$num = round($num, 2);
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
if (strpos($num, '.') === false) {
|
|
|
131 |
return trim($obj->toCurrencyWords($int_curr, $num));
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
$currency = explode('.', $num, 2);
|
|
|
135 |
|
|
|
136 |
$len = strlen($currency[1]);
|
|
|
137 |
|
|
|
138 |
if ($len == 1) {
|
|
|
139 |
// add leading zero
|
|
|
140 |
$currency[1] .= '0';
|
|
|
141 |
} elseif ($len > 2) {
|
|
|
142 |
// get the 3rd digit after the comma
|
|
|
143 |
$round_digit = substr($currency[1], 2, 1);
|
|
|
144 |
|
|
|
145 |
// cut everything after the 2nd digit
|
|
|
146 |
$currency[1] = substr($currency[1], 0, 2);
|
|
|
147 |
|
|
|
148 |
if ($round_digit >= 5) {
|
|
|
149 |
// round up without losing precision
|
|
|
150 |
include_once "Math/BigInteger.php";
|
|
|
151 |
|
|
|
152 |
$int = new Math_BigInteger(join($currency));
|
|
|
153 |
$int = $int->add(new Math_BigInteger(1));
|
|
|
154 |
$int_str = $int->toString();
|
|
|
155 |
|
|
|
156 |
$currency[0] = substr($int_str, 0, -2);
|
|
|
157 |
$currency[1] = substr($int_str, -2);
|
|
|
158 |
|
|
|
159 |
// check if the rounded decimal part became zero
|
|
|
160 |
if ($currency[1] == '00') {
|
|
|
161 |
$currency[1] = false;
|
|
|
162 |
}
|
|
|
163 |
}
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
return trim($obj->toCurrencyWords($int_curr, $currency[0], $currency[1]));
|
|
|
167 |
}
|
|
|
168 |
// }}}
|
|
|
169 |
|
|
|
170 |
// {{{ getLocales()
|
|
|
171 |
/**
|
|
|
172 |
* Lists available locales for Numbers_Words
|
|
|
173 |
*
|
|
|
174 |
* @param mixed $locale string/array of strings $locale
|
|
|
175 |
* Optional searched language name abbreviation.
|
|
|
176 |
* Default: all available locales.
|
|
|
177 |
*
|
|
|
178 |
* @return array The available locales (optionaly only the requested ones)
|
|
|
179 |
* @author Piotr Klaban <makler@man.torun.pl>
|
|
|
180 |
* @author Bertrand Gugger, bertrand at toggg dot com
|
|
|
181 |
*
|
|
|
182 |
* @access public
|
|
|
183 |
* @static
|
|
|
184 |
* @return mixed[]
|
|
|
185 |
*/
|
|
|
186 |
function getLocales($locale = null)
|
|
|
187 |
{
|
|
|
188 |
$ret = array();
|
|
|
189 |
if (isset($locale) && is_string($locale)) {
|
|
|
190 |
$locale = array($locale);
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
$dname = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Words' . DIRECTORY_SEPARATOR;
|
|
|
194 |
|
|
|
195 |
$dh = opendir($dname);
|
|
|
196 |
|
|
|
197 |
if ($dh) {
|
|
|
198 |
while ($fname = readdir($dh)) {
|
|
|
199 |
if (preg_match('#^lang\.([a-z_]+)\.php$#i', $fname, $matches)) {
|
|
|
200 |
if (is_file($dname . $fname) && is_readable($dname . $fname) &&
|
|
|
201 |
(!isset($locale) || in_array($matches[1], $locale))) {
|
|
|
202 |
$ret[] = $matches[1];
|
|
|
203 |
}
|
|
|
204 |
}
|
|
|
205 |
}
|
|
|
206 |
closedir($dh);
|
|
|
207 |
sort($ret);
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
return $ret;
|
|
|
211 |
}
|
|
|
212 |
// }}}
|
|
|
213 |
|
|
|
214 |
// {{{ raiseError()
|
|
|
215 |
/**
|
|
|
216 |
* Trigger a PEAR error
|
|
|
217 |
*
|
|
|
218 |
* To improve performances, the PEAR.php file is included dynamically.
|
|
|
219 |
*
|
|
|
220 |
* @param string $msg error message
|
|
|
221 |
*
|
|
|
222 |
* @return PEAR_Error
|
|
|
223 |
*/
|
|
|
224 |
function raiseError($msg)
|
|
|
225 |
{
|
|
|
226 |
include_once 'PEAR.php';
|
|
|
227 |
return PEAR::raiseError($msg);
|
|
|
228 |
}
|
|
|
229 |
// }}}
|
|
|
230 |
}
|
|
|
231 |
|
|
|
232 |
// }}}
|
|
|
233 |
?>
|