/zf/library/Zend/Crypt/Math/BigInteger.php
PHP | 117 lines | 41 code | 6 blank | 70 comment | 12 complexity | da0daadb467436a76b789e249b2172df MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, Apache-2.0, LGPL-2.1, LGPL-3.0, BSD-2-Clause
1<?php 2/** 3 * Zend Framework 4 * 5 * LICENSE 6 * 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. 14 * 15 * @category Zend 16 * @package Zend_Crypt 17 * @subpackage Math 18 * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) 19 * @license http://framework.zend.com/license/new-bsd New BSD License 20 * @version $Id: BigInteger.php 23775 2011-03-01 17:25:24Z ralph $ 21 */ 22 23/** 24 * Support for arbitrary precision mathematics in PHP. 25 * 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. 31 * 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). 38 * 39 * @category Zend 40 * @package Zend_Crypt 41 * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) 42 * @license http://framework.zend.com/license/new-bsd New BSD License 43 */ 44class Zend_Crypt_Math_BigInteger 45{ 46 47 /** 48 * Holds an instance of one of the three arbitrary precision wrappers. 49 * 50 * @var Zend_Crypt_Math_BigInteger_Interface 51 */ 52 protected $_math = null; 53 54 /** 55 * Constructor; a Factory which detects a suitable PHP extension for 56 * arbitrary precision math and instantiates the suitable wrapper 57 * object. 58 * 59 * @param string $extension 60 * @throws Zend_Crypt_Math_BigInteger_Exception 61 */ 62 public function __construct($extension = null) 63 { 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'); 67 } 68 $this->_loadAdapter($extension); 69 } 70 71 /** 72 * Redirect all public method calls to the wrapped extension object. 73 * 74 * @param string $methodName 75 * @param array $args 76 * @throws Zend_Crypt_Math_BigInteger_Exception 77 */ 78 public function __call($methodName, $args) 79 { 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'); 83 } 84 return call_user_func_array(array($this->_math, $methodName), $args); 85 } 86 87 /** 88 * @param string $extension 89 * @throws Zend_Crypt_Math_BigInteger_Exception 90 */ 91 protected function _loadAdapter($extension = null) 92 { 93 if ($extension === null) { 94 if (extension_loaded('gmp')) { 95 $extension = 'gmp'; 96 //} elseif (extension_loaded('big_int')) { 97 // $extension = 'big_int'; 98 } else { 99 $extension = 'bcmath'; 100 } 101 } 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' && extension_loaded('bcmath')) { 109 require_once 'Zend/Crypt/Math/BigInteger/Bcmath.php'; 110 $this->_math = new Zend_Crypt_Math_BigInteger_Bcmath(); 111 } else { 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'); 114 } 115 } 116 117}