Source for file Number.class.php

Documentation is available at Number.class.php

  1. <?php
  2. /**
  3.  * Gumbo Library Framework
  4.  *
  5.  * LICENSE
  6.  * This library is being released under the terms of the New BSD License.  A
  7.  * copy of the license is packaged with the software (LICENSE.txt).  If no
  8.  * copy is found, a copy of the license template can be found at:
  9.  * http://www.opensource.org/licenses/bsd-license.php
  10.  * 
  11.  * @category Gumbo
  12.  * @package Number
  13.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  14.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  15.  * @author Michael Luster <mluster79@yahoo.com>
  16.  * @link http://sourceforge.net/projects/phpgumbo
  17.  * @version 1.0.0
  18.  */
  19.  
  20. /**
  21.  * Number Class
  22.  * 
  23.  * The current implementation only provides static methods, performing simple
  24.  * operations dealing with Numbers.  This class will be changed in the future
  25.  * into something more interesting.  For now, it performs simple tasks.
  26.  *
  27.  * @category Gumbo
  28.  * @package Number
  29.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  30.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  31.  * @author Michael Luster <mluster79@yahoo.com>
  32.  * @link http://sourceforge.net/projects/phpgumbo
  33.  * @desc Number Class
  34.  * @version 1.0.0
  35.  */
  36.  
  37. class Gumbo_Number {
  38.     
  39.     /**
  40.      * Returns the suffix for a given number
  41.      * @param int $num 
  42.      * @return string 
  43.      * @throws Gumbo_Exception
  44.      */
  45.     public static function getSuffix ($num{
  46.         $suffix null;
  47.         try {
  48.             // verify precondition
  49.             if (!is_int ($num)) {
  50.                 throw new Gumbo_Exception ("Invalid Argument 'num:int' => {$num}:gettype ($num));
  51.             }
  52.             
  53.             // retrieve last digit
  54.             if (strlen ($num1{
  55.                 $num substr ($numstrlen ($num1);
  56.             }
  57.             
  58.             $suffix false;
  59.             switch ($num{
  60.                 case $suffix "th"break;
  61.                 case $suffix "st"break;
  62.                 case $suffix "nd"break;
  63.                 case $suffix "rd"break;
  64.                 case $suffix "th"break;
  65.                 case $suffix "th"break;
  66.                 case $suffix "th"break;
  67.                 case $suffix "th"break;
  68.                 case $suffix "th"break;
  69.                 case $suffix "th"break;
  70.             }
  71.         catch (Gumbo_Exception $e{
  72.             $e->setFunction (__METHOD__);
  73.             gumbo_trigger ($e);
  74.         }
  75.         return $suffix;
  76.     }
  77.     
  78.     
  79.     
  80.     /**
  81.      * Returns a randomly generated number x digits long
  82.      * @param int $digits number of digits
  83.      * @return int 
  84.      * @throws Gumbo_Exception
  85.      */
  86.     public static function random ($digits=1{
  87.         try {
  88.             if (!is_numeric ($digits)) {
  89.                 throw new Gumbo_Exception ("Invalid Argument 'digits:int' => {$digits}:gettype ($digits));
  90.             }
  91.             if ($digits 1{
  92.                 throw new Gumbo_Exception ("Argument Must Be Greater Than One: {$digits}");
  93.             }
  94.         catch (Gumbo_Exception $e{
  95.             $e->setFunction (__METHOD__);
  96.             gumbo_trigger ($e);
  97.             $digits 1;
  98.         }
  99.         
  100.         $digits = (int) $digits;
  101.         
  102.         $pool "0123456789";
  103.         $len strlen ($pool1;
  104.         $num 0;
  105.         
  106.         for ($x 0;$x $digits;$x++{
  107.             $num .= $pool {mt_rand (0$len)};
  108.         }
  109.         
  110.         return (int) $num;
  111.     }
  112.     
  113.     
  114.     
  115.     /**
  116.      * Returns a randomly generated number
  117.      * @param int $min 
  118.      * @param int $max 
  119.      * @return int 
  120.      * @throws Gumbo_Exception
  121.      */
  122.     public static function rand ($min=null$max=null{
  123.         try {
  124.             if (!is_null ($min&& !is_numeric ($min)) {
  125.                 throw new Gumbo_Exception ("Invalid Argument 'min:int|null' => {$min}:gettype ($min));
  126.             }
  127.             if (!is_null ($max&& !is_numeric ($max)) {
  128.                 throw new Gumbo_Exception ("Invalid Argument 'max:int|null' => {$max}:gettype ($max));
  129.             }
  130.             
  131.             return mt_rand ($min$max);
  132.         catch (Gumbo_Exception $e{
  133.             $e->setFunction (__METHOD__);
  134.             gumbo_trigger ($e);
  135.         }
  136.         return 0;
  137.     }
  138.     
  139. }
  140.  
  141. ?>