[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/fgrosse/phpasn1/lib/Utility/ -> BigInteger.php (source)

   1  <?php
   2  /*
   3   * This file is part of the PHPASN1 library.
   4   *
   5   * For the full copyright and license information, please view the LICENSE
   6   * file that was distributed with this source code.
   7   */
   8  
   9  namespace FG\Utility;
  10  
  11  /**
  12   * Class BigInteger
  13   * Utility class to remove dependence on a single large number library. Not intended for external use, this class only
  14   * implements the functionality needed throughout this project.
  15   *
  16   * Instances are immutable, all operations return a new instance with the result.
  17   *
  18   * @package FG\Utility
  19   * @internal
  20   */
  21  abstract class BigInteger
  22  {
  23      /**
  24       * Force a preference on the underlying big number implementation, useful for testing.
  25       * @var string|null
  26       */
  27      private static $_prefer;
  28  
  29      public static function setPrefer($prefer = null)
  30      {
  31          self::$_prefer = $prefer;
  32      }
  33  
  34      /**
  35       * Create a BigInteger instance based off the base 10 string or an integer.
  36       * @param string|int $val
  37       * @return BigInteger
  38       * @throws \InvalidArgumentException
  39       */
  40      public static function create($val)
  41      {
  42          if (self::$_prefer) {
  43              switch (self::$_prefer) {
  44                  case 'gmp':
  45                      $ret = new BigIntegerGmp();
  46                      break;
  47                  case 'bcmath':
  48                      $ret = new BigIntegerBcmath();
  49                      break;
  50                  default:
  51                      throw new \UnexpectedValueException('Unknown number implementation: ' . self::$_prefer);
  52              }
  53          }
  54          else {
  55              // autodetect
  56              if (function_exists('gmp_add')) {
  57                  $ret = new BigIntegerGmp();
  58              }
  59              elseif (function_exists('bcadd')) {
  60                  $ret = new BigIntegerBcmath();
  61              } else {
  62                  throw new \RuntimeException('Requires GMP or bcmath extension.');
  63              }
  64          }
  65  
  66          if (is_int($val)) {
  67              $ret->_fromInteger($val);
  68          }
  69          else {
  70              // convert to string, if not already one
  71              $val = (string)$val;
  72  
  73              // validate string
  74              if (!preg_match('/^-?[0-9]+$/', $val)) {
  75                  throw new \InvalidArgumentException('Expects a string representation of an integer.');
  76              }
  77              $ret->_fromString($val);
  78          }
  79  
  80          return $ret;
  81      }
  82  
  83      /**
  84       * BigInteger constructor.
  85       * Prevent directly instantiating object, use BigInteger::create instead.
  86       */
  87      protected function __construct()
  88      {
  89  
  90      }
  91  
  92      /**
  93       * Subclasses must provide clone functionality.
  94       * @return BigInteger
  95       */
  96      abstract public function __clone();
  97  
  98      /**
  99       * Assign the instance value from base 10 string.
 100       * @param string $str
 101       */
 102      abstract protected function _fromString($str);
 103  
 104      /**
 105       * Assign the instance value from an integer type.
 106       * @param int $integer
 107       */
 108      abstract protected function _fromInteger($integer);
 109  
 110      /**
 111       * Must provide string implementation that returns base 10 number.
 112       * @return string
 113       */
 114      abstract public function __toString();
 115  
 116      /* INFORMATIONAL FUNCTIONS */
 117  
 118      /**
 119       * Return integer, if possible. Throws an exception if the number can not be represented as a native integer.
 120       * @return int
 121       * @throws \OverflowException
 122       */
 123      abstract public function toInteger();
 124  
 125      /**
 126       * Is represented integer negative?
 127       * @return bool
 128       */
 129      abstract public function isNegative();
 130  
 131      /**
 132       * Compare the integer with $number, returns a negative integer if $this is less than number, returns 0 if $this is
 133       * equal to number and returns a positive integer if $this is greater than number.
 134       * @param BigInteger|string|int $number
 135       * @return int
 136       */
 137      abstract public function compare($number);
 138  
 139      /* MODIFY */
 140  
 141      /**
 142       * Add another integer $b and returns the result.
 143       * @param BigInteger|string|int $b
 144       * @return BigInteger
 145       */
 146      abstract public function add($b);
 147  
 148      /**
 149       * Subtract $b from $this and returns the result.
 150       * @param BigInteger|string|int $b
 151       * @return BigInteger
 152       */
 153      abstract public function subtract($b);
 154  
 155      /**
 156       * Multiply value.
 157       * @param BigInteger|string|int $b
 158       * @return BigInteger
 159       */
 160      abstract public function multiply($b);
 161  
 162      /**
 163       * The value $this modulus $b.
 164       * @param BigInteger|string|int $b
 165       * @return BigInteger
 166       */
 167      abstract public function modulus($b);
 168  
 169      /**
 170       * Raise $this to the power of $b and returns the result.
 171       * @param BigInteger|string|int $b
 172       * @return BigInteger
 173       */
 174      abstract public function toPower($b);
 175  
 176      /**
 177       * Shift the value to the right by a set number of bits and returns the result.
 178       * @param int $bits
 179       * @return BigInteger
 180       */
 181      abstract public function shiftRight($bits = 8);
 182  
 183      /**
 184       * Shift the value to the left by a set number of bits and returns the result.
 185       * @param int $bits
 186       * @return BigInteger
 187       */
 188      abstract public function shiftLeft($bits = 8);
 189  
 190      /**
 191       * Returns the absolute value.
 192       * @return BigInteger
 193       */
 194      abstract public function absoluteValue();
 195  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer