Math_Integer
[ class tree: Math_Integer ] [ index: Math_Integer ] [ all elements ]

Source for file Integer.php

Documentation is available at Integer.php

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net>                |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Integer.php 303940 2010-10-02 13:53:19Z clockwerx $
  20. //
  21.  
  22. include_once 'PEAR.php';
  23.  
  24. define ('HAS_GMP'(boolean) extension_loaded('gmp'));
  25. define ('HAS_BCMATH'(boolean) extension_loaded('bcmath'));
  26.  
  27. define ('MATH_INTEGER_AUTO''auto');
  28. define ('MATH_INTEGER_STANDARD''standard');
  29. define ('MATH_INTEGER_BCMATH''bcmath');
  30. define ('MATH_INTEGER_GMP''gmp');
  31.  
  32. $_math_integer_types = array(
  33.                 MATH_INTEGER_STANDARD,
  34.                 MATH_INTEGER_BCMATH,
  35.                 MATH_INTEGER_GMP
  36.             );
  37.  
  38. class Math_Integer {/*{{{*/
  39.  
  40.     function &create($val$type=MATH_INTEGER_AUTO$truncate=false{/*{{{*/
  41.         if ($type == MATH_INTEGER_AUTO || $type == null
  42.             // decide what object to instantiate
  43.             $type = Math_Integer::_selectType();
  44.         elseif (in_array($type$GLOBALS['_math_integer_types'])) {
  45.             // make sure that the lib is available
  46.             if ($type == MATH_INTEGER_GMP && !HAS_GMP{
  47.                 return PEAR::raiseError('GMP libary missing, cannot create object');
  48.             elseif ($type == MATH_INTEGER_BCMATH && !HAS_BCMATH{
  49.                 return PEAR::raiseError('BCMATH libary missing, cannot create object');
  50.             }
  51.         else // wrong type requested
  52.             return PEAR::raiseError('Invalid type. Expecting one of: auto, '.
  53.                          implode(', '$GLOBALS['_math_integer_valid_types']));
  54.         }
  55.         $classFile = "Math/Integer/{$type}.php";
  56.         $className 'Math_Integer_'.ucfirst($type);
  57.         // convert to a string representing an integer
  58.         $value = Math_Integer::_toIntegerString($val$type$truncate);
  59.         if (PEAR::isError($value)) {
  60.             $value->addUserInfo(array(
  61.                         'class' => 'Math_Integer',
  62.                         'method' => 'create',
  63.                         'params' => array(
  64.                                 'val' => $val,
  65.                                 'type' => 'MATH_INTEGER_'.strtoupper($type),
  66.                                 'truncate' => ($truncate == false'false' 'true'
  67.                             )
  68.                     ));
  69.             return $value;
  70.         }
  71.         // load the appropriate class file and instantiate the object
  72.         if (include_once($classFile)) {
  73.             $object = new $className($value);
  74.             return $object;
  75.         else {
  76.             return PEAR::raiseError("Error: could not find $classFile. Cannot ".
  77.                                     "instantiate $className object");
  78.         }
  79.     }/*}}}*/
  80.  
  81.     function &createGMP($val$truncate=false{/*{{{*/
  82.         return Math_Integer::create($valMATH_INTEGER_GMP$truncate);
  83.     }/*}}}*/
  84.  
  85.     function &createBCMATH($val$truncate=false{/*{{{*/
  86.         return Math_Integer::create($valMATH_INTEGER_BCMATH$truncate);
  87.     }/*}}}*/
  88.  
  89.     function &createStandard($val$truncate=false{/*{{{*/
  90.         return Math_Integer::create($valMATH_INTEGER_STANDARD$truncate);
  91.     }/*}}}*/
  92.  
  93.     function _toIntegerString($val$type=MATH_INTEGER_AUTO$truncate=false{/*{{{*/
  94.         $integerRE '/^[[:digit:]]+$/';
  95.         $floatRE '/^([[:digit:]]+)\.[[:digit:]]+$/';
  96.         $hexRE '/^[[:xdigit:]]+$/';
  97.  
  98.         if (preg_match($integerRE$val)) // integer
  99.             return strval($val);
  100.         elseif (preg_match($hexRE$val)) // hexadecimal
  101.             // if automatic selection, set correct type
  102.             if ($type == MATH_INTEGER_AUTO{
  103.                 $type = Math_Integer::_selectType();
  104.             }
  105.             $len strlen(trim($val));
  106.             $value = 0;
  107.             if ($type == MATH_INTEGER_GMP{
  108.                 for ($i = 0; $i $len$i++{
  109.                     $c substr($val($len - 1 - $i)1);
  110.                     $value = gmp_add($valuegmp_mul(hexdec($c),gmp_pow(16,$i)));
  111.                 }
  112.                 return gmp_strval($value);
  113.             elseif ($type == MATH_INTEGER_BCMATH
  114.                 for ($i = 0; $i $len$i++{
  115.                     $c substr($val($len - 1 - $i)1);
  116.                     $value = bcadd($valuebcmul(hexdec($c),bcpow(16,$i)));
  117.                 }
  118.                 return $value;
  119.             else // return PEAR_Error for now
  120.                 return PEAR::raiseError('GMP or BCMATH support needed to '.
  121.                                         'process hexadecimal integers');
  122.             }
  123.         elseif (preg_match($floatRE$val$reg)) // float
  124.             if ($truncate{
  125.                 return strval($reg[1]);
  126.             else {
  127.                 return PEAR::raiseError('Not an integer. For truncation '.
  128.                             'of a floating point number set $truncate=true');
  129.             }
  130.         else {
  131.             return PEAR::raiseError("Invalid value: $val does not represent an integer");
  132.         }
  133.     }/*}}}*/
  134.  
  135.     function _selectType({/*{{{*/
  136.         if (HAS_GMP{
  137.             $selectedType MATH_INTEGER_GMP;
  138.         elseif (HAS_BCMATH{
  139.             $selectedType MATH_INTEGER_BCMATH;
  140.         else {
  141.             $selectedType MATH_INTEGER_STANDARD;
  142.         }
  143.         return $selectedType;
  144.     }/*}}}*/
  145.     
  146. }/* end of Math_Integer }}}*/
  147.  
  148. // vim: ts=4:sw=4:et:
  149. // vim6: fdl=1:
  150. ?>

Documentation generated on Mon, 11 Mar 2019 15:39:15 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.