Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. BigNumber.php

function BigNumber::_of

@psalm-pure

1 call to BigNumber::_of()
BigNumber::of in vendor/brick/math/src/BigNumber.php
Creates a BigNumber of the given value.

File

vendor/brick/math/src/BigNumber.php, line 76

Class

BigNumber
Common interface for arbitrary-precision rational numbers.

Namespace

Brick\Math

Code

private static function _of(BigNumber|int|float|string $value) : BigNumber {
    if ($value instanceof BigNumber) {
        return $value;
    }
    if (\is_int($value)) {
        return new BigInteger((string) $value);
    }
    if (is_float($value)) {
        $value = (string) $value;
    }
    if (str_contains($value, '/')) {
        // Rational number
        if (\preg_match(self::PARSE_REGEXP_RATIONAL, $value, $matches, PREG_UNMATCHED_AS_NULL) !== 1) {
            throw NumberFormatException::invalidFormat($value);
        }
        $sign = $matches['sign'];
        $numerator = $matches['numerator'];
        $denominator = $matches['denominator'];
        assert($numerator !== null);
        assert($denominator !== null);
        $numerator = self::cleanUp($sign, $numerator);
        $denominator = self::cleanUp(null, $denominator);
        if ($denominator === '0') {
            throw DivisionByZeroException::denominatorMustNotBeZero();
        }
        return new BigRational(new BigInteger($numerator), new BigInteger($denominator), false);
    }
    else {
        // Integer or decimal number
        if (\preg_match(self::PARSE_REGEXP_NUMERICAL, $value, $matches, PREG_UNMATCHED_AS_NULL) !== 1) {
            throw NumberFormatException::invalidFormat($value);
        }
        $sign = $matches['sign'];
        $point = $matches['point'];
        $integral = $matches['integral'];
        $fractional = $matches['fractional'];
        $exponent = $matches['exponent'];
        if ($integral === null && $fractional === null) {
            throw NumberFormatException::invalidFormat($value);
        }
        if ($integral === null) {
            $integral = '0';
        }
        if ($point !== null || $exponent !== null) {
            $fractional = $fractional ?? '';
            $exponent = $exponent !== null ? (int) $exponent : 0;
            if ($exponent === PHP_INT_MIN || $exponent === PHP_INT_MAX) {
                throw new NumberFormatException('Exponent too large.');
            }
            $unscaledValue = self::cleanUp($sign, $integral . $fractional);
            $scale = \strlen($fractional) - $exponent;
            if ($scale < 0) {
                if ($unscaledValue !== '0') {
                    $unscaledValue .= \str_repeat('0', -$scale);
                }
                $scale = 0;
            }
            return new BigDecimal($unscaledValue, $scale);
        }
        $integral = self::cleanUp($sign, $integral);
        return new BigInteger($integral);
    }
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal