Advertisement
Guest User

Untitled

a guest
Jan 5th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.66 KB | None | 0 0
  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.01 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_01.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.01 http://www.php.net/license/3_01.txt
  23.  * @version  SVN: $Id$
  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.01 http://www.php.net/license/3_01.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.     // {{{ properties
  43.  
  44.     /**
  45.      * Default Locale name
  46.      * @var string
  47.      * @access public
  48.      */
  49.     var $locale = '';
  50.  
  51.     // }}}
  52.     // {{{ toWords()
  53.  
  54.     /**
  55.      * Converts a number to its word representation
  56.      *
  57.      * @param integer $num     An integer between -infinity and infinity inclusive :)
  58.      *                         that should be converted to a words representation
  59.      * @param string  $locale  Language name abbreviation. Optional. Defaults to
  60.      *                         current loaded driver or en_US if any.
  61.      * @param array   $options Specific driver options
  62.      *
  63.      * @access public
  64.      * @author Piotr Klaban <makler@man.torun.pl>
  65.      * @since  PHP 4.2.3
  66.      * @return string  The corresponding word representation
  67.      */
  68.     public static function toWords($num, $locale = NULL, $options = array())
  69.     {
  70.         if (!$locale) {
  71.             $locale = WORDS_LANG;
  72.         }
  73.  
  74.         if (empty($locale)) {
  75.             $locale = 'en_US';
  76.         }
  77.  
  78.         require_once APPPATH . "libraries/Numbers/Words/lang.${locale}.php";
  79.  
  80.         $classname = "Numbers_Words_${locale}";
  81.  
  82.         if (!class_exists($classname)) {
  83.             return Numbers_Words::raiseError("Unable to include the Numbers/Words/lang.${locale}.php file");
  84.         }
  85.  
  86.         $methods = get_class_methods($classname);
  87.  
  88.         if (!in_array('_toWords', $methods) && !in_array('_towords', $methods)) {
  89.             return Numbers_Words::raiseError("Unable to find _toWords method in '$classname' class");
  90.         }
  91.  
  92.         if (!is_int($num)) {
  93.             // cast (sanitize) to int without losing precision
  94.             $num = preg_replace('/^[^\d]*?(-?)[ \t\n]*?(\d+)([^\d].*?)?$/', '$1$2', $num);
  95.         }
  96.  
  97.         //$truth_table  = ($classname == get_class($this)) ? 'T' : 'F';
  98.         $truth_table  = ($classname == get_called_class()) ? 'T' : 'F';
  99.         $truth_table .= (empty($options)) ? 'T' : 'F';
  100.  
  101.         switch ($truth_table) {
  102.  
  103.         /**
  104.          * We are a language driver
  105.          */
  106.         case 'TT':
  107.             return trim($this->_toWords($num));
  108.             break;
  109.  
  110.         /**
  111.          * We are a language driver with custom options
  112.          */
  113.         case 'TF':
  114.             return trim($this->_toWords($num, $options));
  115.             break;
  116.  
  117.         /**
  118.          * We are the parent class
  119.          */
  120.         case 'FT':
  121.             @$obj = new $classname;
  122.             return trim($obj->_toWords($num));
  123.             break;
  124.  
  125.         /**
  126.          * We are the parent class and should pass driver options
  127.          */
  128.         case 'FF':
  129.             @$obj = new $classname;
  130.             return trim($obj->_toWords($num, $options));
  131.             break;
  132.  
  133.         }
  134.  
  135.     }
  136.     // }}}
  137.  
  138.     // {{{ toCurrency()
  139.     /**
  140.      * Converts a currency value to word representation (1.02 => one dollar two cents)
  141.      * If the number has not any fraction part, the "cents" number is omitted.
  142.      *
  143.      * @param float  $num      A float/integer/string number representing currency value
  144.      *
  145.      * @param string $locale   Language name abbreviation. Optional. Defaults to en_US.
  146.      *
  147.      * @param string $int_curr International currency symbol
  148.      *                 as defined by the ISO 4217 standard (three characters).
  149.      *                 E.g. 'EUR', 'USD', 'PLN'. Optional.
  150.      *                 Defaults to $def_currency defined in the language class.
  151.      *
  152.      * @return string  The corresponding word representation
  153.      *
  154.      * @access public
  155.      * @author Piotr Klaban <makler@man.torun.pl>
  156.      * @since  PHP 4.2.3
  157.      * @return string
  158.      */
  159.     function toCurrency($num, $locale = 'en_US', $int_curr = '')
  160.     {
  161.         $ret = $num;
  162.  
  163.         @include_once APPPATH . "libraries/Numbers/Words/lang.${locale}.php";
  164.  
  165.         $classname = "Numbers_Words_${locale}";
  166.  
  167.         if (!class_exists($classname)) {
  168.             return Numbers_Words::raiseError("Unable to include the Numbers/Words/lang.${locale}.php file");
  169.         }
  170.  
  171.         $methods = get_class_methods($classname);
  172.  
  173.         if (!in_array('toCurrencyWords', $methods) && !in_array('tocurrencywords', $methods)) {
  174.             return Numbers_Words::raiseError("Unable to find toCurrencyWords method in '$classname' class");
  175.         }
  176.  
  177.         @$obj = new $classname;
  178.  
  179.         // round if a float is passed, use Math_BigInteger otherwise
  180.         if (is_float($num)) {
  181.             $num = round($num, 2);
  182.         }
  183.  
  184.         if (strpos($num, '.') === false) {
  185.             return trim($obj->toCurrencyWords($int_curr, $num));
  186.         }
  187.  
  188.         $currency = explode('.', $num, 2);
  189.  
  190.         $len = strlen($currency[1]);
  191.  
  192.         if ($len == 1) {
  193.             // add leading zero
  194.             $currency[1] .= '0';
  195.         } elseif ($len > 2) {
  196.             // get the 3rd digit after the comma
  197.             $round_digit = substr($currency[1], 2, 1);
  198.            
  199.             // cut everything after the 2nd digit
  200.             $currency[1] = substr($currency[1], 0, 2);
  201.            
  202.             if ($round_digit >= 5) {
  203.                 // round up without losing precision
  204.                 include_once "Math/BigInteger.php";
  205.  
  206.                 $int = new Math_BigInteger(join($currency));
  207.                 $int = $int->add(new Math_BigInteger(1));
  208.                 $int_str = $int->toString();
  209.  
  210.                 $currency[0] = substr($int_str, 0, -2);
  211.                 $currency[1] = substr($int_str, -2);
  212.  
  213.                 // check if the rounded decimal part became zero
  214.                 if ($currency[1] == '00') {
  215.                     $currency[1] = false;
  216.                 }
  217.             }
  218.         }
  219.  
  220.         return trim($obj->toCurrencyWords($int_curr, $currency[0], $currency[1]));
  221.     }
  222.     // }}}
  223.  
  224.     // {{{ getLocales()
  225.     /**
  226.      * Lists available locales for Numbers_Words
  227.      *
  228.      * @param mixed $locale string/array of strings $locale
  229.      *                 Optional searched language name abbreviation.
  230.      *                 Default: all available locales.
  231.      *
  232.      * @return array   The available locales (optionaly only the requested ones)
  233.      * @author Piotr Klaban <makler@man.torun.pl>
  234.      * @author Bertrand Gugger, bertrand at toggg dot com
  235.      *
  236.      * @access public
  237.      * @static
  238.      * @return mixed[]
  239.      */
  240.     function getLocales($locale = null)
  241.     {
  242.         $ret = array();
  243.         if (isset($locale) && is_string($locale)) {
  244.             $locale = array($locale);
  245.         }
  246.  
  247.         $dname = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Words' . DIRECTORY_SEPARATOR;
  248.  
  249.         $dh = opendir($dname);
  250.  
  251.         if ($dh) {
  252.             while ($fname = readdir($dh)) {
  253.                 if (preg_match('#^lang\.([a-z_]+)\.php$#i', $fname, $matches)) {
  254.                     if (is_file($dname . $fname) && is_readable($dname . $fname) &&
  255.                         (!isset($locale) || in_array($matches[1], $locale))) {
  256.                         $ret[] = $matches[1];
  257.                     }
  258.                 }
  259.             }
  260.             closedir($dh);
  261.             sort($ret);
  262.         }
  263.  
  264.         return $ret;
  265.     }
  266.     // }}}
  267.  
  268.     // {{{ raiseError()
  269.     /**
  270.      * Trigger a PEAR error
  271.      *
  272.      * To improve performances, the PEAR.php file is included dynamically.
  273.      *
  274.      * @param string $msg error message
  275.      *
  276.      * @return PEAR_Error
  277.      */
  278.     function raiseError($msg)
  279.     {
  280.         include_once 'PEAR.php';
  281.         return PEAR::raiseError($msg);
  282.     }
  283.     // }}}
  284. }
  285.  
  286. // }}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement