/zf/library/Zend/Crypt/Math/BigInteger.php

http://github.com/eryx/php-framework-benchmark · PHP · 117 lines · 41 code · 6 blank · 70 comment · 12 complexity · da0daadb467436a76b789e249b2172df MD5 · raw file

  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. * Support for arbitrary precision mathematics in PHP.
  24. *
  25. * Zend_Crypt_Math_BigInteger is a wrapper across three PHP extensions: bcmath, gmp
  26. * and big_int. Since each offer similar functionality, but availability of
  27. * each differs across installations of PHP, this wrapper attempts to select
  28. * the fastest option available and encapsulate a subset of its functionality
  29. * which all extensions share in common.
  30. *
  31. * This class requires one of the three extensions to be available. BCMATH
  32. * while the slowest, is available by default under Windows, and under Unix
  33. * if PHP is compiled with the flag "--enable-bcmath". GMP requires the gmp
  34. * library from http://www.swox.com/gmp/ and PHP compiled with the "--with-gmp"
  35. * flag. BIG_INT support is available from a big_int PHP library available from
  36. * only from PECL (a Windows port is not available).
  37. *
  38. * @category Zend
  39. * @package Zend_Crypt
  40. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. */
  43. class Zend_Crypt_Math_BigInteger
  44. {
  45. /**
  46. * Holds an instance of one of the three arbitrary precision wrappers.
  47. *
  48. * @var Zend_Crypt_Math_BigInteger_Interface
  49. */
  50. protected $_math = null;
  51. /**
  52. * Constructor; a Factory which detects a suitable PHP extension for
  53. * arbitrary precision math and instantiates the suitable wrapper
  54. * object.
  55. *
  56. * @param string $extension
  57. * @throws Zend_Crypt_Math_BigInteger_Exception
  58. */
  59. public function __construct($extension = null)
  60. {
  61. if ($extension !== null && !in_array($extension, array('bcmath', 'gmp', 'bigint'))) {
  62. require_once('Zend/Crypt/Math/BigInteger/Exception.php');
  63. throw new Zend_Crypt_Math_BigInteger_Exception('Invalid extension type; please use one of bcmath, gmp or bigint');
  64. }
  65. $this->_loadAdapter($extension);
  66. }
  67. /**
  68. * Redirect all public method calls to the wrapped extension object.
  69. *
  70. * @param string $methodName
  71. * @param array $args
  72. * @throws Zend_Crypt_Math_BigInteger_Exception
  73. */
  74. public function __call($methodName, $args)
  75. {
  76. if(!method_exists($this->_math, $methodName)) {
  77. require_once 'Zend/Crypt/Math/BigInteger/Exception.php';
  78. throw new Zend_Crypt_Math_BigInteger_Exception('invalid method call: ' . get_class($this->_math) . '::' . $methodName . '() does not exist');
  79. }
  80. return call_user_func_array(array($this->_math, $methodName), $args);
  81. }
  82. /**
  83. * @param string $extension
  84. * @throws Zend_Crypt_Math_BigInteger_Exception
  85. */
  86. protected function _loadAdapter($extension = null)
  87. {
  88. if ($extension === null) {
  89. if (extension_loaded('gmp')) {
  90. $extension = 'gmp';
  91. //} elseif (extension_loaded('big_int')) {
  92. // $extension = 'big_int';
  93. } else {
  94. $extension = 'bcmath';
  95. }
  96. }
  97. if($extension == 'gmp' && extension_loaded('gmp')) {
  98. require_once 'Zend/Crypt/Math/BigInteger/Gmp.php';
  99. $this->_math = new Zend_Crypt_Math_BigInteger_Gmp();
  100. //} elseif($extension == 'bigint' && extension_loaded('big_int')) {
  101. // require_once 'Zend/Crypt_Math/BigInteger/Bigint.php';
  102. // $this->_math = new Zend_Crypt_Math_BigInteger_Bigint();
  103. } elseif ($extension == 'bcmath' && extension_loaded('bcmath')) {
  104. require_once 'Zend/Crypt/Math/BigInteger/Bcmath.php';
  105. $this->_math = new Zend_Crypt_Math_BigInteger_Bcmath();
  106. } else {
  107. require_once 'Zend/Crypt/Math/BigInteger/Exception.php';
  108. throw new Zend_Crypt_Math_BigInteger_Exception($extension . ' big integer precision math support not detected');
  109. }
  110. }
  111. }