7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
24 * Support for arbitrary precision mathematics in PHP.
26 * Zend_Crypt_Math_BigInteger is a wrapper across three PHP extensions: bcmath, gmp
27 * and big_int. Since each offer similar functionality, but availability of
28 * each differs across installations of PHP, this wrapper attempts to select
29 * the fastest option available and encapsulate a subset of its functionality
30 * which all extensions share in common.
32 * This class requires one of the three extensions to be available. BCMATH
33 * while the slowest, is available by default under Windows, and under Unix
34 * if PHP is compiled with the flag "--enable-bcmath". GMP requires the gmp
35 * library from http://www.swox.com/gmp/ and PHP compiled with the "--with-gmp"
36 * flag. BIG_INT support is available from a big_int PHP library available from
37 * only from PECL (a Windows port is not available).
41 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
42 * @license http://framework.zend.com/license/new-bsd New BSD License
44 class Zend_Crypt_Math_BigInteger
48 * Holds an instance of one of the three arbitrary precision wrappers.
50 * @var Zend_Crypt_Math_BigInteger_Interface
52 protected $_math = null;
55 * Constructor; a Factory which detects a suitable PHP extension for
56 * arbitrary precision math and instantiates the suitable wrapper
59 * @param string $extension
60 * @throws Zend_Crypt_Math_BigInteger_Exception
62 public function __construct($extension = null)
64 if ($extension !== null && !in_array($extension, array('bcmath', 'gmp', 'bigint'))) {
65 require_once('Zend/Crypt/Math/BigInteger/Exception.php');
66 throw new Zend_Crypt_Math_BigInteger_Exception('Invalid extension type; please use one of bcmath, gmp or bigint');
68 $this->_loadAdapter($extension);
72 * Redirect all public method calls to the wrapped extension object.
74 * @param string $methodName
76 * @throws Zend_Crypt_Math_BigInteger_Exception
78 public function __call($methodName, $args)
80 if(!method_exists($this->_math, $methodName)) {
81 require_once 'Zend/Crypt/Math/BigInteger/Exception.php';
82 throw new Zend_Crypt_Math_BigInteger_Exception('invalid method call: ' . get_class($this->_math) . '::' . $methodName . '() does not exist');
84 return call_user_func_array(array($this->_math, $methodName), $args);
88 * @param string $extension
89 * @throws Zend_Crypt_Math_BigInteger_Exception
91 protected function _loadAdapter($extension = null)
93 if ($extension === null) {
94 if (extension_loaded('gmp')) {
96 //} elseif (extension_loaded('big_int')) {
97 // $extension = 'big_int';
99 $extension = 'bcmath';
102 if($extension == 'gmp' && extension_loaded('gmp')) {
103 require_once 'Zend/Crypt/Math/BigInteger/Gmp.php';
104 $this->_math = new Zend_Crypt_Math_BigInteger_Gmp();
105 //} elseif($extension == 'bigint' && extension_loaded('big_int')) {
106 // require_once 'Zend/Crypt_Math/BigInteger/Bigint.php';
107 // $this->_math = new Zend_Crypt_Math_BigInteger_Bigint();
108 } elseif ($extension == 'bcmath') {
109 require_once 'Zend/Crypt/Math/BigInteger/Bcmath.php';
110 $this->_math = new Zend_Crypt_Math_BigInteger_Bcmath();
112 require_once 'Zend/Crypt/Math/BigInteger/Exception.php';
113 throw new Zend_Crypt_Math_BigInteger_Exception($extension . ' big integer precision math support not detected');