upload
[pear] / Numbers / Words.php
1 <?php\r
2 /**\r
3  * Numbers_Words\r
4  *\r
5  * PHP version 4\r
6  *\r
7  * Copyright (c) 1997-2006 The PHP Group\r
8  *\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
16  *\r
17  * Authors: Piotr Klaban <makler@man.torun.pl>\r
18  *\r
19  * @category Numbers\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
25  */\r
26 \r
27 // {{{ Numbers_Words\r
28 \r
29 /**\r
30  * The Numbers_Words class provides method to convert arabic numerals to words.\r
31  *\r
32  * @category Numbers\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
37  * @since    PHP 4.2.3\r
38  * @access   public\r
39  */\r
40 class Numbers_Words\r
41 {\r
42     // {{{ toWords()\r
43 \r
44     /**\r
45      * Converts a number to its word representation\r
46      *\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
50      *\r
51      * @access public\r
52      * @author Piotr Klaban <makler@man.torun.pl>\r
53      * @since  PHP 4.2.3\r
54      * @return string  The corresponding word representation\r
55      */\r
56     function toWords($num, $locale = 'en_US')\r
57     {\r
58 \r
59         include_once "Numbers/Words/lang.${locale}.php";\r
60 \r
61         $classname = "Numbers_Words_${locale}";\r
62 \r
63         if (!class_exists($classname)) {\r
64             return Numbers_Words::raiseError("Unable to include the Numbers/Words/lang.${locale}.php file");\r
65         }\r
66 \r
67         $methods = get_class_methods($classname);\r
68 \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
71         }\r
72 \r
73         @$obj = new $classname;\r
74 \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
78         }\r
79 \r
80         return trim($obj->toWords($num));\r
81     }\r
82     // }}}\r
83 \r
84     // {{{ toCurrency()\r
85     /**\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
88      *\r
89      * @param float  $num      A float/integer/string number representing currency value\r
90      *\r
91      * @param string $locale   Language name abbreviation. Optional. Defaults to en_US.\r
92      *\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
97      *\r
98      * @return string  The corresponding word representation\r
99      *\r
100      * @access public\r
101      * @author Piotr Klaban <makler@man.torun.pl>\r
102      * @since  PHP 4.2.3\r
103      * @return string\r
104      */\r
105     function toCurrency($num, $locale = 'en_US', $int_curr = '')\r
106     {\r
107         $ret = $num;\r
108 \r
109         @include_once "Numbers/Words/lang.${locale}.php";\r
110 \r
111         $classname = "Numbers_Words_${locale}";\r
112 \r
113         if (!class_exists($classname)) {\r
114             return Numbers_Words::raiseError("Unable to include the Numbers/Words/lang.${locale}.php file");\r
115         }\r
116 \r
117         $methods = get_class_methods($classname);\r
118 \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
121         }\r
122 \r
123         @$obj = new $classname;\r
124 \r
125         if (strpos($num, '.') === false) {\r
126             return trim($obj->toCurrencyWords($int_curr, $num));\r
127         }\r
128 \r
129         $currency = explode('.', $num, 2);\r
130         /* add leading zero */\r
131         if (strlen($currency[1]) == 1) {\r
132             $currency[1] .= '0';\r
133         }\r
134 \r
135         return trim($obj->toCurrencyWords($int_curr, $currency[0], $currency[1]));\r
136     }\r
137     // }}}\r
138 \r
139     // {{{ getLocales()\r
140     /**\r
141      * Lists available locales for Numbers_Words\r
142      *\r
143      * @param mixed $locale string/array of strings $locale\r
144      *                 Optional searched language name abbreviation.\r
145      *                 Default: all available locales.\r
146      *\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
150      *\r
151      * @access public\r
152      * @static\r
153      * @return mixed[]\r
154      */\r
155     function getLocales($locale = null)\r
156     {\r
157         $ret = array();\r
158         if (isset($locale) && is_string($locale)) {\r
159             $locale = array($locale);\r
160         }\r
161 \r
162         $dname = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Words' . DIRECTORY_SEPARATOR;\r
163 \r
164         $dh = opendir($dname);\r
165 \r
166         if ($dh) {\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
172                     }\r
173                 }\r
174             }\r
175             closedir($dh);\r
176             sort($ret);\r
177         }\r
178 \r
179         return $ret;\r
180     }\r
181     // }}}\r
182 \r
183     // {{{ raiseError()\r
184     /**\r
185      * Trigger a PEAR error\r
186      *\r
187      * To improve performances, the PEAR.php file is included dynamically.\r
188      *\r
189      * @param string $msg error message\r
190      *\r
191      * @return PEAR_Error\r
192      */\r
193     function raiseError($msg)\r
194     {\r
195         include_once 'PEAR.php';\r
196         return PEAR::raiseError($msg);\r
197     }\r
198     // }}}\r
199 }\r
200 \r
201 // }}}\r
202 ?>\r