HEX
Server: Apache
System: Linux srv13.cpanelhost.cl 3.10.0-962.3.2.lve1.5.38.el7.x86_64 #1 SMP Thu Jun 18 05:28:41 EDT 2020 x86_64
User: cca63905 (4205)
PHP: 7.3.20
Disabled: NONE
Upload Files
File: //proc/self/root/proc/self/root/home/cca63905/www/nueva/vendor/prestashop/decimal/src/Builder.php
<?php
/**
 * This file is part of the PrestaShop\Decimal package
 *
 * @author    PrestaShop SA <contact@prestashop.com>
 * @license   https://opensource.org/licenses/MIT MIT License
 */

namespace PrestaShop\Decimal;

use PrestaShop\Decimal\DecimalNumber;


/**
 * Builds Number instances
 */
class Builder
{

    /**
     * Pattern for most numbers
     */
    const NUMBER_PATTERN = "/^(?<sign>[-+])?(?<integerPart>\d+)?(?:\.(?<fractionalPart>\d+)(?<exponentPart>[eE](?<exponentSign>[-+])(?<exponent>\d+))?)?$/";

    /**
     * Pattern for integer numbers in scientific notation (rare but supported by spec)
     */
    const INT_EXPONENTIAL_PATTERN = "/^(?<sign>[-+])?(?<integerPart>\d+)(?<exponentPart>[eE](?<exponentSign>[-+])(?<exponent>\d+))$/";

    /**
     * Builds a Number from a string
     *
     * @param string $number
     *
     * @return DecimalNumber
     */
    public static function parseNumber($number)
    {
        if (!self::itLooksLikeANumber($number, $numberParts)) {
            throw new \InvalidArgumentException(
                sprintf('"%s" cannot be interpreted as a number', print_r($number, true))
            );
        }

        $integerPart = '';
        if (array_key_exists('integerPart', $numberParts)) {
            // extract the integer part and remove leading zeroes
            $integerPart = ltrim($numberParts['integerPart'], '0');
        }

        $fractionalPart = '';
        if (array_key_exists('fractionalPart', $numberParts)) {
            // extract the fractional part and remove trailing zeroes
            $fractionalPart = rtrim($numberParts['fractionalPart'], '0');
        }

        $fractionalDigits = strlen($fractionalPart);
        $coefficient = $integerPart . $fractionalPart;

        // when coefficient is '0' or a sequence of '0'
        if ('' === $coefficient) {
            $coefficient = '0';
        }

        // when the number has been provided in scientific notation
        if (array_key_exists('exponentPart', $numberParts)) {
            $givenExponent = (int) ($numberParts['exponentSign'] . $numberParts['exponent']);

            // we simply add or subtract fractional digits from the given exponent (depending if it's positive or negative)
            $fractionalDigits -= $givenExponent;
            if ($fractionalDigits < 0) {
                // if the resulting fractional digits is negative, it means there is no fractional part anymore
                // we need to add trailing zeroes as needed
                $coefficient = str_pad($coefficient, strlen($coefficient) - $fractionalDigits, '0');

                // there's no fractional part anymore
                $fractionalDigits = 0;
            }
        }

        return new DecimalNumber($numberParts['sign'] . $coefficient, $fractionalDigits);
    }

    /**
     * @param string $number
     * @param array $numberParts
     *
     * @return bool
     */
    private static function itLooksLikeANumber($number, &$numberParts)
    {
        return (
            strlen((string) $number) > 0
            && (
                preg_match(self::NUMBER_PATTERN, $number, $numberParts)
                || preg_match(self::INT_EXPONENTIAL_PATTERN, $number, $numberParts)
            )
        );
    }

}