pero1203 4cec48f127 Merge branch 'master' into release/produkcija
# Conflicts:
#	.gitignore
#	vendor/autoload.php
#	vendor/composer/ClassLoader.php
#	vendor/composer/LICENSE
#	vendor/composer/autoload_classmap.php
#	vendor/composer/autoload_files.php
#	vendor/composer/autoload_psr4.php
#	vendor/composer/autoload_real.php
#	vendor/composer/autoload_static.php
#	vendor/composer/installed.json
#	vendor/fgrosse/phpasn1/CHANGELOG.md
#	vendor/fgrosse/phpasn1/composer.json
#	vendor/fgrosse/phpasn1/lib/Utility/BigInteger.php
#	vendor/geoip2/geoip2/CHANGELOG.md
#	vendor/geoip2/geoip2/composer.json
#	vendor/geoip2/geoip2/src/Database/Reader.php
#	vendor/geoip2/geoip2/src/Exception/AddressNotFoundException.php
#	vendor/geoip2/geoip2/src/Exception/AuthenticationException.php
#	vendor/geoip2/geoip2/src/Exception/GeoIp2Exception.php
#	vendor/geoip2/geoip2/src/Exception/HttpException.php
#	vendor/geoip2/geoip2/src/Exception/InvalidRequestException.php
#	vendor/geoip2/geoip2/src/Exception/OutOfQueriesException.php
#	vendor/geoip2/geoip2/src/Model/AbstractModel.php
#	vendor/geoip2/geoip2/src/Model/AnonymousIp.php
#	vendor/geoip2/geoip2/src/Model/Asn.php
#	vendor/geoip2/geoip2/src/Model/City.php
#	vendor/geoip2/geoip2/src/Model/ConnectionType.php
#	vendor/geoip2/geoip2/src/Model/Country.php
#	vendor/geoip2/geoip2/src/Model/Domain.php
#	vendor/geoip2/geoip2/src/Model/Enterprise.php
#	vendor/geoip2/geoip2/src/Model/Insights.php
#	vendor/geoip2/geoip2/src/Model/Isp.php
#	vendor/geoip2/geoip2/src/ProviderInterface.php
#	vendor/geoip2/geoip2/src/Record/AbstractPlaceRecord.php
#	vendor/geoip2/geoip2/src/Record/AbstractRecord.php
#	vendor/geoip2/geoip2/src/Record/City.php
#	vendor/geoip2/geoip2/src/Record/Continent.php
#	vendor/geoip2/geoip2/src/Record/Country.php
#	vendor/geoip2/geoip2/src/Record/Location.php
#	vendor/geoip2/geoip2/src/Record/MaxMind.php
#	vendor/geoip2/geoip2/src/Record/Postal.php
#	vendor/geoip2/geoip2/src/Record/RepresentedCountry.php
#	vendor/geoip2/geoip2/src/Record/Subdivision.php
#	vendor/geoip2/geoip2/src/Record/Traits.php
#	vendor/geoip2/geoip2/src/Util.php
#	vendor/geoip2/geoip2/src/WebService/Client.php
#	vendor/guzzlehttp/promises/CHANGELOG.md
#	vendor/guzzlehttp/promises/README.md
#	vendor/guzzlehttp/promises/composer.json
#	vendor/guzzlehttp/promises/src/AggregateException.php
#	vendor/guzzlehttp/promises/src/CancellationException.php
#	vendor/guzzlehttp/promises/src/Coroutine.php
#	vendor/guzzlehttp/promises/src/EachPromise.php
#	vendor/guzzlehttp/promises/src/FulfilledPromise.php
#	vendor/guzzlehttp/promises/src/Promise.php
#	vendor/guzzlehttp/promises/src/PromiseInterface.php
#	vendor/guzzlehttp/promises/src/PromisorInterface.php
#	vendor/guzzlehttp/promises/src/RejectedPromise.php
#	vendor/guzzlehttp/promises/src/RejectionException.php
#	vendor/guzzlehttp/promises/src/TaskQueue.php
#	vendor/guzzlehttp/promises/src/TaskQueueInterface.php
#	vendor/guzzlehttp/promises/src/functions.php
#	vendor/guzzlehttp/psr7/CHANGELOG.md
#	vendor/guzzlehttp/psr7/README.md
#	vendor/guzzlehttp/psr7/composer.json
#	vendor/guzzlehttp/psr7/src/AppendStream.php
#	vendor/guzzlehttp/psr7/src/BufferStream.php
#	vendor/guzzlehttp/psr7/src/CachingStream.php
#	vendor/guzzlehttp/psr7/src/DroppingStream.php
#	vendor/guzzlehttp/psr7/src/FnStream.php
#	vendor/guzzlehttp/psr7/src/InflateStream.php
#	vendor/guzzlehttp/psr7/src/LazyOpenStream.php
#	vendor/guzzlehttp/psr7/src/LimitStream.php
#	vendor/guzzlehttp/psr7/src/MessageTrait.php
#	vendor/guzzlehttp/psr7/src/MultipartStream.php
#	vendor/guzzlehttp/psr7/src/NoSeekStream.php
#	vendor/guzzlehttp/psr7/src/PumpStream.php
#	vendor/guzzlehttp/psr7/src/Request.php
#	vendor/guzzlehttp/psr7/src/Response.php
#	vendor/guzzlehttp/psr7/src/ServerRequest.php
#	vendor/guzzlehttp/psr7/src/Stream.php
#	vendor/guzzlehttp/psr7/src/StreamDecoratorTrait.php
#	vendor/guzzlehttp/psr7/src/StreamWrapper.php
#	vendor/guzzlehttp/psr7/src/UploadedFile.php
#	vendor/guzzlehttp/psr7/src/Uri.php
#	vendor/guzzlehttp/psr7/src/UriNormalizer.php
#	vendor/guzzlehttp/psr7/src/UriResolver.php
#	vendor/guzzlehttp/psr7/src/functions.php
#	vendor/maxmind-db/reader/CHANGELOG.md
#	vendor/maxmind-db/reader/composer.json
#	vendor/maxmind-db/reader/ext/maxminddb.c
#	vendor/maxmind-db/reader/ext/php_maxminddb.h
#	vendor/maxmind-db/reader/package.xml
#	vendor/maxmind/web-service-common/CHANGELOG.md
#	vendor/maxmind/web-service-common/README.md
#	vendor/maxmind/web-service-common/composer.json
#	vendor/maxmind/web-service-common/src/Exception/AuthenticationException.php
#	vendor/maxmind/web-service-common/src/Exception/HttpException.php
#	vendor/maxmind/web-service-common/src/Exception/InsufficientFundsException.php
#	vendor/maxmind/web-service-common/src/Exception/InvalidInputException.php
#	vendor/maxmind/web-service-common/src/Exception/InvalidRequestException.php
#	vendor/maxmind/web-service-common/src/Exception/IpAddressNotFoundException.php
#	vendor/maxmind/web-service-common/src/Exception/PermissionRequiredException.php
#	vendor/maxmind/web-service-common/src/Exception/WebServiceException.php
#	vendor/maxmind/web-service-common/src/WebService/Client.php
#	vendor/maxmind/web-service-common/src/WebService/Http/CurlRequest.php
#	vendor/maxmind/web-service-common/src/WebService/Http/Request.php
#	vendor/maxmind/web-service-common/src/WebService/Http/RequestFactory.php
#	vendor/paragonie/random_compat/composer.json
#	vendor/paragonie/sodium_compat/.gitignore
#	vendor/paragonie/sodium_compat/composer.json
#	vendor/paragonie/sodium_compat/psalm-above-3.xml
#	vendor/paragonie/sodium_compat/src/Core/Ed25519.php
#	vendor/paragonie/sodium_compat/src/Core/SipHash.php
#	vendor/paragonie/sodium_compat/src/Core/Util.php
#	vendor/paragonie/sodium_compat/src/Core32/Ed25519.php
#	vendor/paragonie/sodium_compat/src/File.php
#	vendor/paragonie/sodium_compat/src/PHP52/SplFixedArray.php
#	vendor/phpmailer/phpmailer/README.md
#	vendor/phpmailer/phpmailer/VERSION
#	vendor/phpmailer/phpmailer/composer.json
#	vendor/phpmailer/phpmailer/get_oauth_token.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-af.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-ar.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-az.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-ba.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-be.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-bg.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-ca.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-ch.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-cs.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-da.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-de.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-el.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-eo.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-es.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-et.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-fa.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-fi.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-fo.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-fr.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-gl.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-he.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-hi.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-hr.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-hu.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-hy.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-id.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-it.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-ja.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-ka.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-ko.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-lt.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-lv.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-mg.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-ms.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-nb.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-nl.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-pl.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-pt.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-pt_br.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-ro.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-ru.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-sk.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-sl.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-sr.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-sv.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-tl.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-tr.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-uk.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-vi.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-zh.php
#	vendor/phpmailer/phpmailer/language/phpmailer.lang-zh_cn.php
#	vendor/phpmailer/phpmailer/src/Exception.php
#	vendor/phpmailer/phpmailer/src/OAuth.php
#	vendor/phpmailer/phpmailer/src/PHPMailer.php
#	vendor/phpmailer/phpmailer/src/POP3.php
#	vendor/phpmailer/phpmailer/src/SMTP.php
#	vendor/spomky-labs/base64url/composer.json
#	vendor/stripe/stripe-php/CHANGELOG.md
#	vendor/stripe/stripe-php/VERSION
#	vendor/stripe/stripe-php/composer.json
#	vendor/stripe/stripe-php/init.php
#	vendor/stripe/stripe-php/lib/Account.php
#	vendor/stripe/stripe-php/lib/ApiRequestor.php
#	vendor/stripe/stripe-php/lib/ApplicationFeeRefund.php
#	vendor/stripe/stripe-php/lib/Balance.php
#	vendor/stripe/stripe-php/lib/BalanceTransaction.php
#	vendor/stripe/stripe-php/lib/BaseStripeClient.php
#	vendor/stripe/stripe-php/lib/BitcoinReceiver.php
#	vendor/stripe/stripe-php/lib/Card.php
#	vendor/stripe/stripe-php/lib/Charge.php
#	vendor/stripe/stripe-php/lib/Checkout/Session.php
#	vendor/stripe/stripe-php/lib/Coupon.php
#	vendor/stripe/stripe-php/lib/CreditNote.php
#	vendor/stripe/stripe-php/lib/CreditNoteLineItem.php
#	vendor/stripe/stripe-php/lib/Customer.php
#	vendor/stripe/stripe-php/lib/ErrorObject.php
#	vendor/stripe/stripe-php/lib/Event.php
#	vendor/stripe/stripe-php/lib/HttpClient/CurlClient.php
#	vendor/stripe/stripe-php/lib/Invoice.php
#	vendor/stripe/stripe-php/lib/InvoiceItem.php
#	vendor/stripe/stripe-php/lib/InvoiceLineItem.php
#	vendor/stripe/stripe-php/lib/Order.php
#	vendor/stripe/stripe-php/lib/PaymentIntent.php
#	vendor/stripe/stripe-php/lib/PaymentMethod.php
#	vendor/stripe/stripe-php/lib/Payout.php
#	vendor/stripe/stripe-php/lib/Plan.php
#	vendor/stripe/stripe-php/lib/PromotionCode.php
#	vendor/stripe/stripe-php/lib/Refund.php
#	vendor/stripe/stripe-php/lib/Service/CoreServiceFactory.php
#	vendor/stripe/stripe-php/lib/Service/CustomerService.php
#	vendor/stripe/stripe-php/lib/Service/InvoiceService.php
#	vendor/stripe/stripe-php/lib/Service/PayoutService.php
#	vendor/stripe/stripe-php/lib/Service/SubscriptionScheduleService.php
#	vendor/stripe/stripe-php/lib/Service/SubscriptionService.php
#	vendor/stripe/stripe-php/lib/SetupIntent.php
#	vendor/stripe/stripe-php/lib/Source.php
#	vendor/stripe/stripe-php/lib/Stripe.php
#	vendor/stripe/stripe-php/lib/StripeClient.php
#	vendor/stripe/stripe-php/lib/StripeObject.php
#	vendor/stripe/stripe-php/lib/Subscription.php
#	vendor/stripe/stripe-php/lib/TaxId.php
#	vendor/stripe/stripe-php/lib/TaxRate.php
#	vendor/stripe/stripe-php/lib/TransferReversal.php
#	vendor/stripe/stripe-php/lib/Util/ObjectTypes.php
#	vendor/stripe/stripe-php/lib/Util/Util.php
#	vendor/symfony/polyfill-intl-idn/Idn.php
#	vendor/symfony/polyfill-intl-idn/composer.json
#	vendor/symfony/polyfill-intl-normalizer/bootstrap.php
#	vendor/symfony/polyfill-intl-normalizer/composer.json
#	vendor/symfony/polyfill-php72/Php72.php
#	vendor/symfony/polyfill-php72/bootstrap.php
#	vendor/symfony/polyfill-php72/composer.json
2020-12-05 13:00:23 +01:00

207 lines
5.4 KiB
PHP

<?php
/*
* This file is part of the PHPASN1 library.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FG\Utility;
/**
* Class BigInteger
* Utility class to remove dependence on a single large number library. Not intended for external use, this class only
* implements the functionality needed throughout this project.
*
* Instances are immutable, all operations return a new instance with the result.
*
* @package FG\Utility
* @internal
*/
abstract class BigInteger
{
/**
* Force a preference on the underlying big number implementation, useful for testing.
* @var string|null
*/
private static $_prefer;
public static function setPrefer($prefer = null)
{
self::$_prefer = $prefer;
}
/**
* Create a BigInteger instance based off the base 10 string or an integer.
* @param string|int $val
* @return BigInteger
* @throws \InvalidArgumentException
*/
public static function create($val)
{
if (self::$_prefer) {
switch (self::$_prefer) {
case 'gmp':
$ret = new BigIntegerGmp();
break;
case 'bcmath':
$ret = new BigIntegerBcmath();
break;
default:
throw new \UnexpectedValueException('Unknown number implementation: ' . self::$_prefer);
}
}
else {
// autodetect
<<<<<<< HEAD
if (extension_loaded('gmp')) {
$ret = new BigIntegerGmp();
}
elseif (extension_loaded('bcmath')) {
$ret = new BigIntegerBcmath();
}
else {
// TODO: potentially offer pure php implementation?
=======
if (function_exists('gmp_add')) {
$ret = new BigIntegerGmp();
}
elseif (function_exists('bcadd')) {
$ret = new BigIntegerBcmath();
} else {
>>>>>>> master
throw new \RuntimeException('Requires GMP or bcmath extension.');
}
}
if (is_int($val)) {
$ret->_fromInteger($val);
}
else {
// convert to string, if not already one
$val = (string)$val;
// validate string
if (!preg_match('/^-?[0-9]+$/', $val)) {
throw new \InvalidArgumentException('Expects a string representation of an integer.');
}
$ret->_fromString($val);
}
return $ret;
}
/**
* BigInteger constructor.
* Prevent directly instantiating object, use BigInteger::create instead.
*/
protected function __construct()
{
}
/**
* Subclasses must provide clone functionality.
* @return BigInteger
*/
abstract public function __clone();
/**
* Assign the instance value from base 10 string.
* @param string $str
*/
abstract protected function _fromString($str);
/**
* Assign the instance value from an integer type.
* @param int $integer
*/
abstract protected function _fromInteger($integer);
/**
* Must provide string implementation that returns base 10 number.
* @return string
*/
abstract public function __toString();
/* INFORMATIONAL FUNCTIONS */
/**
* Return integer, if possible. Throws an exception if the number can not be represented as a native integer.
* @return int
* @throws \OverflowException
*/
abstract public function toInteger();
/**
* Is represented integer negative?
* @return bool
*/
abstract public function isNegative();
/**
* Compare the integer with $number, returns a negative integer if $this is less than number, returns 0 if $this is
* equal to number and returns a positive integer if $this is greater than number.
* @param BigInteger|string|int $number
* @return int
*/
abstract public function compare($number);
/* MODIFY */
/**
* Add another integer $b and returns the result.
* @param BigInteger|string|int $b
* @return BigInteger
*/
abstract public function add($b);
/**
* Subtract $b from $this and returns the result.
* @param BigInteger|string|int $b
* @return BigInteger
*/
abstract public function subtract($b);
/**
* Multiply value.
* @param BigInteger|string|int $b
* @return BigInteger
*/
abstract public function multiply($b);
/**
* The value $this modulus $b.
* @param BigInteger|string|int $b
* @return BigInteger
*/
abstract public function modulus($b);
/**
* Raise $this to the power of $b and returns the result.
* @param BigInteger|string|int $b
* @return BigInteger
*/
abstract public function toPower($b);
/**
* Shift the value to the right by a set number of bits and returns the result.
* @param int $bits
* @return BigInteger
*/
abstract public function shiftRight($bits = 8);
/**
* Shift the value to the left by a set number of bits and returns the result.
* @param int $bits
* @return BigInteger
*/
abstract public function shiftLeft($bits = 8);
/**
* Returns the absolute value.
* @return BigInteger
*/
abstract public function absoluteValue();
}