Subversion Repositories Applications.annuaire

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
296 aurelien 1
<?php
2
/* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */
3
//
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
//
20
// $Id: Words.php,v 1.4 2006/06/13 11:29:35 makler Exp $
21
//
22
 
23
/**
24
 * The Numbers_Words class provides method to convert arabic numerals to
25
 * words (also with currency name).
26
 *
27
 * @author Piotr Klaban <makler@man.torun.pl>
28
 * @package Numbers_Words
29
 */
30
 
31
// {{{ Numbers_Words
32
 
33
/**
34
 * The Numbers_Words class provides method to convert arabic numerals to words.
35
 *
36
 * @access public
37
 * @author Piotr Klaban <makler@man.torun.pl>
38
 * @since  PHP 4.2.3
39
 * @package Numbers_Words
40
 */
41
class Numbers_Words
42
{
43
    // {{{ toWords()
44
 
45
    /**
46
     * Converts a number to its word representation
47
     *
48
     * @param  integer $num   An integer between -infinity and infinity inclusive :)
49
     *                        that should be converted to a words representation
50
     *
51
     * @param  string  $locale Language name abbreviation. Optional. Defaults to en_US.
52
     *
53
     * @return string  The corresponding word representation
54
     *
55
     * @access public
56
     * @author Piotr Klaban <makler@man.torun.pl>
57
     * @since  PHP 4.2.3
58
     */
59
    function toWords($num, $locale = 'en_US') {
60
 
61
        include_once("lang.${locale}.php");
62
 
63
        $classname = "Numbers_Words_${locale}";
64
 
65
        if (!class_exists($classname)) {
66
            return Numbers_Words::raiseError("Unable to include the Numbers/Words/lang.${locale}.php file");
67
        }
68
 
69
        $methods = get_class_methods($classname);
70
 
71
        if (!in_array('toWords', $methods) && !in_array('towords', $methods)) {
72
            return Numbers_Words::raiseError("Unable to find toWords method in '$classname' class");
73
        }
74
 
75
        $obj = new $classname;
76
 
77
        return trim($obj->toWords($num));
78
    }
79
    // }}}
80
    // {{{ toCurrency()
81
    /**
82
     * Converts a currency value to word representation (1.02 => one dollar two cents)
83
     * If the number has not any fraction part, the "cents" number is omitted.
84
     *
85
     * @param  float   $num   A float/integer/string number representing currency value
86
     *
87
     * @param  string  $locale Language name abbreviation. Optional. Defaults to en_US.
88
     *
89
     * @param  string  $int_curr International currency symbol
90
     *                 as defined by the ISO 4217 standard (three characters).
91
     *                 E.g. 'EUR', 'USD', 'PLN'. Optional.
92
     *                 Defaults to $def_currency defined in the language class.
93
     *
94
     * @return string  The corresponding word representation
95
     *
96
     * @access public
97
     * @author Piotr Klaban <makler@man.torun.pl>
98
     * @since  PHP 4.2.3
99
     */
100
    function toCurrency($num, $locale = 'en_US', $int_curr = '') {
101
        $ret = $num;
102
 
103
        @include_once("Numbers/Words/lang.${locale}.php");
104
 
105
        $classname = "Numbers_Words_${locale}";
106
 
107
        if (!class_exists($classname)) {
108
            return Numbers_Words::raiseError("Unable to include the Numbers/Words/lang.${locale}.php file");
109
        }
110
 
111
        $methods = get_class_methods($classname);
112
 
113
        if (!in_array('toCurrencyWords', $methods) && !in_array('tocurrencywords', $methods)) {
114
            return Numbers_Words::raiseError("Unable to find toCurrencyWords method in '$classname' class");
115
        }
116
 
117
        $obj = new $classname;
118
 
119
        if (strpos($num, '.') === false)
120
        {
121
          $ret      = trim($obj->toCurrencyWords($int_curr, $num));
122
        } else {
123
            $currency = explode('.', $num, 2);
124
            /* add leading zero */
125
            if (strlen($currency[1]) == 1) {
126
                $currency[1] .= '0';
127
            }
128
            $ret      = trim($obj->toCurrencyWords($int_curr, $currency[0], $currency[1]));
129
        }
130
        return $ret;
131
    }
132
    // }}}
133
    // {{{ getLocales()
134
    /**
135
     * Lists available locales for Numbers_Words
136
     *
137
     * @param  string  $int_curr International currency symbol
138
     * @param  mixed   string/array of strings $locale
139
     *                 Optional searched language name abbreviation.
140
     *                 Default: all available locales.
141
     *
142
     * @return array   The available locales (optionaly only the requested ones)
143
     * @author Piotr Klaban <makler@man.torun.pl>
144
     * @author Bertrand Gugger, bertrand at toggg dot com
145
     *
146
     * @access public
147
     * @static
148
     */
149
    function getLocales($locale = null) {
150
        $ret = array();
151
       	if (isset($locale) && is_string($locale)) {
152
       	    $locale = array($locale);
153
        }
154
        $dname = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Words' . DIRECTORY_SEPARATOR;
155
        $dh=opendir($dname);
156
        if ($dh) {
157
            while ($fname = readdir($dh)) {
158
                if (preg_match('#^lang\.([a-z_]+)\.php$#i', $fname, $matches)) {
159
                    if (is_file($dname . $fname) && is_readable($dname . $fname) &&
160
                        (!isset($locale) || in_array($matches[1], $locale))) {
161
                        $ret[] = $matches[1];
162
                    }
163
                }
164
            }
165
            closedir($dh);
166
            sort($ret);
167
        }
168
        return $ret;
169
    }
170
    // }}}
171
    // {{{ raiseError()
172
   /**
173
    * Trigger a PEAR error
174
    *
175
    * To improve performances, the PEAR.php file is included dynamically.
176
    *
177
    * @param string error message
178
    */
179
    function raiseError($msg)
180
    {
181
        include_once('PEAR.php');
182
        return PEAR::raiseError($msg);
183
    }
184
    // }}}
185
}
186
 
187
// }}}
188
?>