blitz-php /
framework
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * This file is part of Blitz PHP framework. |
||
| 5 | * |
||
| 6 | * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]> |
||
| 7 | * |
||
| 8 | * For the full copyright and license information, please view |
||
| 9 | * the LICENSE file that was distributed with this source code. |
||
| 10 | */ |
||
| 11 | |||
| 12 | /** |
||
| 13 | * FONCTIONS DE CONVERSION DE NOMBRE |
||
| 14 | * |
||
| 15 | * @credit <a href="https://codeigniter.com">CodeIgniter 4.2 - number_helper</a> |
||
| 16 | */ |
||
| 17 | if (! function_exists('number_to_size')) { |
||
| 18 | /** |
||
| 19 | * Formats a numbers as bytes, based on size, and adds the appropriate suffix |
||
| 20 | * |
||
| 21 | * @param mixed $num Will be cast as int |
||
| 22 | * |
||
| 23 | * @return bool|string |
||
| 24 | */ |
||
| 25 | function number_to_size($num, int $precision = 1, ?string $locale = null) |
||
| 26 | { |
||
| 27 | // Strip any formatting & ensure numeric input |
||
| 28 | try { |
||
| 29 | // @phpstan-ignore-next-line |
||
| 30 | $num = 0 + str_replace(',', '', $num); |
||
| 31 | } catch (ErrorException) { |
||
| 32 | return false; |
||
| 33 | } |
||
| 34 | |||
| 35 | // ignore sub part |
||
| 36 | $generalLocale = $locale; |
||
| 37 | if ($locale !== null && ($underscorePos = strpos($locale, '_'))) { |
||
| 38 | $generalLocale = substr($locale, 0, $underscorePos); |
||
| 39 | } |
||
| 40 | |||
| 41 | if ($num >= 1_000_000_000_000) { |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 42 | $num = round($num / 1_099_511_627_776, $precision); |
||
| 43 | $unit = lang('Number.terabyteAbbr', [], $generalLocale); |
||
| 44 | } elseif ($num >= 1_000_000_000) { |
||
| 45 | $num = round($num / 1_073_741_824, $precision); |
||
| 46 | $unit = lang('Number.gigabyteAbbr', [], $generalLocale); |
||
| 47 | } elseif ($num >= 1_000_000) { |
||
| 48 | $num = round($num / 1_048_576, $precision); |
||
| 49 | $unit = lang('Number.megabyteAbbr', [], $generalLocale); |
||
| 50 | } elseif ($num >= 1000) { |
||
| 51 | $num = round($num / 1024, $precision); |
||
| 52 | $unit = lang('Number.kilobyteAbbr', [], $generalLocale); |
||
| 53 | } else { |
||
| 54 | $unit = lang('Number.bytes', [], $generalLocale); |
||
| 55 | } |
||
| 56 | |||
| 57 | return format_number($num, $precision, $locale, ['after' => ' ' . $unit]); |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | if (! function_exists('number_to_amount')) { |
||
| 62 | /** |
||
| 63 | * Converts numbers to a more readable representation |
||
| 64 | * when dealing with very large numbers (in the thousands or above), |
||
| 65 | * up to the quadrillions, because you won't often deal with numbers |
||
| 66 | * larger than that. |
||
| 67 | * |
||
| 68 | * It uses the "short form" numbering system as this is most commonly |
||
| 69 | * used within most English-speaking countries today. |
||
| 70 | * |
||
| 71 | * @see https://simple.wikipedia.org/wiki/Names_for_large_numbers |
||
| 72 | * |
||
| 73 | * @param string $num |
||
| 74 | * |
||
| 75 | * @return bool|string |
||
| 76 | */ |
||
| 77 | function number_to_amount($num, int $precision = 0, ?string $locale = null) |
||
| 78 | { |
||
| 79 | // Strip any formatting & ensure numeric input |
||
| 80 | try { |
||
| 81 | // @phpstan-ignore-next-line |
||
| 82 | $num = 0 + str_replace(',', '', $num); |
||
| 83 | } catch (ErrorException) { |
||
| 84 | return false; |
||
| 85 | } |
||
| 86 | |||
| 87 | $suffix = ''; |
||
| 88 | |||
| 89 | // ignore sub part |
||
| 90 | $generalLocale = $locale; |
||
| 91 | if ($locale !== null && ($underscorePos = strpos($locale, '_'))) { |
||
| 92 | $generalLocale = substr($locale, 0, $underscorePos); |
||
| 93 | } |
||
| 94 | |||
| 95 | if ($num > 1_000_000_000_000_000) { |
||
| 96 | $suffix = lang('Number.quadrillion', [], $generalLocale); |
||
| 97 | $num = round(($num / 1_000_000_000_000_000), $precision); |
||
| 98 | } elseif ($num > 1_000_000_000_000) { |
||
| 99 | $suffix = lang('Number.trillion', [], $generalLocale); |
||
| 100 | $num = round(($num / 1_000_000_000_000), $precision); |
||
| 101 | } elseif ($num > 1_000_000_000) { |
||
| 102 | $suffix = lang('Number.billion', [], $generalLocale); |
||
| 103 | $num = round(($num / 1_000_000_000), $precision); |
||
| 104 | } elseif ($num > 1_000_000) { |
||
| 105 | $suffix = lang('Number.million', [], $generalLocale); |
||
| 106 | $num = round(($num / 1_000_000), $precision); |
||
| 107 | } elseif ($num > 1000) { |
||
| 108 | $suffix = lang('Number.thousand', [], $generalLocale); |
||
| 109 | $num = round(($num / 1000), $precision); |
||
| 110 | } |
||
| 111 | |||
| 112 | return format_number($num, $precision, $locale, ['after' => $suffix]); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | if (! function_exists('number_to_currency')) { |
||
| 117 | function number_to_currency(float $num, string $currency, ?string $locale = null, int $fraction = 0): string |
||
| 118 | { |
||
| 119 | return format_number($num, 1, $locale, [ |
||
| 120 | 'type' => NumberFormatter::CURRENCY, |
||
| 121 | 'currency' => $currency, |
||
| 122 | 'fraction' => $fraction, |
||
| 123 | ]); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | if (! function_exists('format_number')) { |
||
| 128 | /** |
||
| 129 | * A general purpose, locale-aware, number_format method. |
||
| 130 | * Used by all of the functions of the number_helper. |
||
| 131 | */ |
||
| 132 | function format_number(float $num, int $precision = 1, ?string $locale = null, array $options = []): string |
||
| 133 | { |
||
| 134 | // If locale is not passed, get from the default locale that is set from our config file |
||
| 135 | // or set by HTTP content negotiation. |
||
| 136 | $locale ??= Locale::getDefault(); |
||
| 137 | |||
| 138 | // Type can be any of the NumberFormatter options, but provide a default. |
||
| 139 | $type = (int) ($options['type'] ?? NumberFormatter::DECIMAL); |
||
| 140 | |||
| 141 | $formatter = new NumberFormatter($locale, $type); |
||
| 142 | |||
| 143 | // Try to format it per the locale |
||
| 144 | if ($type === NumberFormatter::CURRENCY) { |
||
| 145 | $formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $options['fraction']); |
||
| 146 | $output = $formatter->formatCurrency($num, $options['currency']); |
||
| 147 | } else { |
||
| 148 | // In order to specify a precision, we'll have to modify |
||
| 149 | // the pattern used by NumberFormatter. |
||
| 150 | $pattern = '#,##0.' . str_repeat('#', $precision); |
||
| 151 | |||
| 152 | $formatter->setPattern($pattern); |
||
| 153 | $output = $formatter->format($num); |
||
| 154 | } |
||
| 155 | |||
| 156 | // This might lead a trailing period if $precision == 0 |
||
| 157 | $output = trim($output, '. '); |
||
| 158 | |||
| 159 | if (intl_is_failure($formatter->getErrorCode())) { |
||
| 160 | throw new BadFunctionCallException($formatter->getErrorMessage()); |
||
| 161 | } |
||
| 162 | |||
| 163 | // Add on any before/after text. |
||
| 164 | if (isset($options['before']) && is_string($options['before'])) { |
||
| 165 | $output = $options['before'] . $output; |
||
| 166 | } |
||
| 167 | |||
| 168 | if (isset($options['after']) && is_string($options['after'])) { |
||
| 169 | $output .= $options['after']; |
||
| 170 | } |
||
| 171 | |||
| 172 | return $output; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | if (! function_exists('number_to_roman')) { |
||
| 177 | /** |
||
| 178 | * Convert a number to a roman numeral. |
||
| 179 | * |
||
| 180 | * @param string $num it will convert to int |
||
| 181 | */ |
||
| 182 | function number_to_roman(string $num): ?string |
||
| 183 | { |
||
| 184 | static $map = [ |
||
| 185 | 'M' => 1000, |
||
| 186 | 'CM' => 900, |
||
| 187 | 'D' => 500, |
||
| 188 | 'CD' => 400, |
||
| 189 | 'C' => 100, |
||
| 190 | 'XC' => 90, |
||
| 191 | 'L' => 50, |
||
| 192 | 'XL' => 40, |
||
| 193 | 'X' => 10, |
||
| 194 | 'IX' => 9, |
||
| 195 | 'V' => 5, |
||
| 196 | 'IV' => 4, |
||
| 197 | 'I' => 1, |
||
| 198 | ]; |
||
| 199 | |||
| 200 | $num = (int) $num; |
||
| 201 | |||
| 202 | if ($num < 1 || $num > 3999) { |
||
| 203 | return null; |
||
| 204 | } |
||
| 205 | |||
| 206 | $result = ''; |
||
| 207 | |||
| 208 | foreach ($map as $roman => $arabic) { |
||
| 209 | $repeat = (int) floor($num / $arabic); |
||
| 210 | $result .= str_repeat($roman, $repeat); |
||
| 211 | $num %= $arabic; |
||
| 212 | } |
||
| 213 | |||
| 214 | return $result; |
||
| 215 | } |
||
| 216 | } |
||
| 217 |