7 * Copyright (c) 1997-2006 The PHP Group
\r
9 * This source file is subject to version 3.0 of the PHP license,
\r
10 * that is bundled with this package in the file LICENSE, and is
\r
11 * available at through the world-wide-web at
\r
12 * http://www.php.net/license/3_0.txt.
\r
13 * If you did not receive a copy of the PHP license and are unable to
\r
14 * obtain it through the world-wide-web, please send a note to
\r
15 * license@php.net so we can mail you a copy immediately.
\r
17 * Authors: Piotr Klaban <makler@man.torun.pl>
\r
20 * @package Numbers_Words
\r
21 * @author Piotr Klaban <makler@man.torun.pl>
\r
22 * @license PHP 3.0 http://www.php.net/license/3_0.txt
\r
23 * @version CVS: $Id: Words.php,v 1.11 2009/03/14 19:24:34 kouber Exp $
\r
24 * @link http://pear.php.net/package/Numbers_Words
\r
27 // {{{ Numbers_Words
\r
30 * The Numbers_Words class provides method to convert arabic numerals to words.
\r
33 * @package Numbers_Words
\r
34 * @author Piotr Klaban <makler@man.torun.pl>
\r
35 * @license PHP 3.0 http://www.php.net/license/3_0.txt
\r
36 * @link http://pear.php.net/package/Numbers_Words
\r
45 * Converts a number to its word representation
\r
47 * @param integer $num An integer between -infinity and infinity inclusive :)
\r
48 * that should be converted to a words representation
\r
49 * @param string $locale Language name abbreviation. Optional. Defaults to en_US.
\r
52 * @author Piotr Klaban <makler@man.torun.pl>
\r
54 * @return string The corresponding word representation
\r
56 function toWords($num, $locale = 'en_US')
\r
59 include_once "Numbers/Words/lang.${locale}.php";
\r
61 $classname = "Numbers_Words_${locale}";
\r
63 if (!class_exists($classname)) {
\r
64 return Numbers_Words::raiseError("Unable to include the Numbers/Words/lang.${locale}.php file");
\r
67 $methods = get_class_methods($classname);
\r
69 if (!in_array('toWords', $methods) && !in_array('towords', $methods)) {
\r
70 return Numbers_Words::raiseError("Unable to find toWords method in '$classname' class");
\r
73 @$obj = new $classname;
\r
75 if (!is_int($num)) {
\r
76 // cast (sanitize) to int without losing precision
\r
77 $num = preg_replace('/^[^\d]*?(-?)[ \t\n]*?(\d+)([^\d].*?)?$/', '$1$2', $num);
\r
80 return trim($obj->toWords($num));
\r
86 * Converts a currency value to word representation (1.02 => one dollar two cents)
\r
87 * If the number has not any fraction part, the "cents" number is omitted.
\r
89 * @param float $num A float/integer/string number representing currency value
\r
91 * @param string $locale Language name abbreviation. Optional. Defaults to en_US.
\r
93 * @param string $int_curr International currency symbol
\r
94 * as defined by the ISO 4217 standard (three characters).
\r
95 * E.g. 'EUR', 'USD', 'PLN'. Optional.
\r
96 * Defaults to $def_currency defined in the language class.
\r
98 * @return string The corresponding word representation
\r
101 * @author Piotr Klaban <makler@man.torun.pl>
\r
105 function toCurrency($num, $locale = 'en_US', $int_curr = '')
\r
109 @include_once "Numbers/Words/lang.${locale}.php";
\r
111 $classname = "Numbers_Words_${locale}";
\r
113 if (!class_exists($classname)) {
\r
114 return Numbers_Words::raiseError("Unable to include the Numbers/Words/lang.${locale}.php file");
\r
117 $methods = get_class_methods($classname);
\r
119 if (!in_array('toCurrencyWords', $methods) && !in_array('tocurrencywords', $methods)) {
\r
120 return Numbers_Words::raiseError("Unable to find toCurrencyWords method in '$classname' class");
\r
123 @$obj = new $classname;
\r
125 if (strpos($num, '.') === false) {
\r
126 return trim($obj->toCurrencyWords($int_curr, $num));
\r
129 $currency = explode('.', $num, 2);
\r
130 /* add leading zero */
\r
131 if (strlen($currency[1]) == 1) {
\r
132 $currency[1] .= '0';
\r
135 return trim($obj->toCurrencyWords($int_curr, $currency[0], $currency[1]));
\r
139 // {{{ getLocales()
\r
141 * Lists available locales for Numbers_Words
\r
143 * @param mixed $locale string/array of strings $locale
\r
144 * Optional searched language name abbreviation.
\r
145 * Default: all available locales.
\r
147 * @return array The available locales (optionaly only the requested ones)
\r
148 * @author Piotr Klaban <makler@man.torun.pl>
\r
149 * @author Bertrand Gugger, bertrand at toggg dot com
\r
155 function getLocales($locale = null)
\r
158 if (isset($locale) && is_string($locale)) {
\r
159 $locale = array($locale);
\r
162 $dname = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Words' . DIRECTORY_SEPARATOR;
\r
164 $dh = opendir($dname);
\r
167 while ($fname = readdir($dh)) {
\r
168 if (preg_match('#^lang\.([a-z_]+)\.php$#i', $fname, $matches)) {
\r
169 if (is_file($dname . $fname) && is_readable($dname . $fname) &&
\r
170 (!isset($locale) || in_array($matches[1], $locale))) {
\r
171 $ret[] = $matches[1];
\r
183 // {{{ raiseError()
\r
185 * Trigger a PEAR error
\r
187 * To improve performances, the PEAR.php file is included dynamically.
\r
189 * @param string $msg error message
\r
191 * @return PEAR_Error
\r
193 function raiseError($msg)
\r
195 include_once 'PEAR.php';
\r
196 return PEAR::raiseError($msg);
\r