HEX
Server: Apache
System: Linux vps-3158868-x.dattaweb.com 3.10.0-1160.119.1.el7.x86_64 #1 SMP Tue Jun 4 14:43:51 UTC 2024 x86_64
User: emerlux (1185)
PHP: 8.3.1
Disabled: system, shell, exec, system_exec, shell_exec, mysql_pconnect, passthru, popen, proc_open, proc_close, proc_nice, proc_terminate, proc_get_status, escapeshellarg, escapeshellcmd, eval
Upload Files
File: /home/emerlux/public_html/wp-content/plugins/woocommerce-mercadopago/src/Helpers/Numbers.php
<?php

namespace MercadoPago\Woocommerce\Helpers;

if (!defined('ABSPATH')) {
    exit;
}

class Numbers
{
    /**
     * Format value
     *
     * @param float $value
     * @param int $decimals
     * @param string $separator
     *
     * @return float
     */
    public static function format(float $value, int $decimals = 2, string $separator = '.'): float
    {
        return (float) number_format($value, $decimals, $separator, '');
    }

    /**
     * makes the variable a safe float
     *
     * @param mixed $value
     *
     * @return float
     */
    public static function makesValueSafe($value): float
    {
        if (is_string($value) && strlen($value) > 0 && !is_numeric($value[0])) {
            $fixedValue = self::removeNonNumericPrefix($value);
            return floatval($fixedValue);
        }
        return floatval($value);
    }

    public static function removeNonNumericPrefix($str)
    {
        return preg_replace("/[^0-9,.]/", "", $str);
    }



    /**
     * Format value with currency symbol
     *
     * @param string $currencySymbol
     * @param float $value
     * @param int $decimals
     *
     * @return string
     */
    public static function formatWithCurrencySymbol(string $currencySymbol, float $value, int $decimals = 2): string
    {
        return $currencySymbol . ' ' . number_format($value, $decimals, ',', '');
    }

    /**
     * Number format value
     *
     * @param string $currency
     * @param float $value
     * @param float $ratio
     *
     * @return float
     */
    public static function calculateByCurrency(string $currency, float $value, float $ratio): float
    {
        if ($currency === 'COP' || $currency === 'CLP') {
            return self::format($value * $ratio, 0);
        }

        return self::format($value * $ratio * 100) / 100;
    }

    /**
     * Returns the percentage of parcialValue on the sum with the paid value
     *
     * @param float $parcialValue
     * @param float $paidValue
     *
     * @return float
     */
    public static function getPercentageFromParcialValue(float $parcialValue, float $paidValue): float
    {
        $total = $paidValue + $parcialValue;
        $percentage = ($parcialValue / $total) * 100;

        return self::format($percentage);
    }
}