bytic /
money
| 1 | <?php |
||
| 2 | |||
| 3 | use ByTIC\Money\Models\Currencies\CurrenciesTrait; |
||
| 4 | use Nip\Records\AbstractModels\RecordManager; |
||
| 5 | use Nip\Records\Locator\ModelLocator; |
||
| 6 | |||
| 7 | if (!function_exists('currencyManager')) { |
||
| 8 | /** |
||
| 9 | * @return CurrenciesTrait|RecordManager |
||
| 10 | */ |
||
| 11 | function currencyManager() |
||
| 12 | { |
||
| 13 | return ModelLocator::get('currencies'); |
||
| 14 | } |
||
| 15 | } |
||
| 16 | |||
| 17 | if (!function_exists('money_formatter')) { |
||
| 18 | /** |
||
| 19 | * @return \ByTIC\Money\Formatter\Manager |
||
| 20 | */ |
||
| 21 | function money_formatter() |
||
| 22 | { |
||
| 23 | 1 | if (function_exists('app') && app() instanceof Nip\Container\Container) { |
|
| 24 | 1 | if (app()->has('money.formatter')) { |
|
| 25 | return app('money.formatter'); |
||
| 26 | } |
||
| 27 | } |
||
| 28 | |||
| 29 | 1 | return \ByTIC\Money\Formatter\Manager::instance(); |
|
| 30 | } |
||
| 31 | } |
||
| 32 | |||
| 33 | if (!function_exists('money_format')) { |
||
| 34 | function money_format($format, $number) |
||
| 35 | { |
||
| 36 | $regex = '/%((?:[\^!\-]|\+|\(|\=.)*)([0-9]+)?' . |
||
| 37 | '(?:#([0-9]+))?(?:\.([0-9]+))?([in%])/'; |
||
| 38 | if (setlocale(LC_MONETARY, 0) == 'C') { |
||
| 39 | setlocale(LC_MONETARY, ''); |
||
| 40 | } |
||
| 41 | $locale = localeconv(); |
||
| 42 | preg_match_all($regex, $format, $matches, PREG_SET_ORDER); |
||
| 43 | foreach ($matches as $fmatch) { |
||
| 44 | $value = floatval($number); |
||
| 45 | $flags = [ |
||
| 46 | 'fillchar' => preg_match('/\=(.)/', $fmatch[1], $match) ? |
||
| 47 | $match[1] : ' ', |
||
| 48 | 'nogroup' => preg_match('/\^/', $fmatch[1]) > 0, |
||
| 49 | 'usesignal' => preg_match('/\+|\(/', $fmatch[1], $match) ? |
||
| 50 | $match[0] : '+', |
||
| 51 | 'nosimbol' => preg_match('/\!/', $fmatch[1]) > 0, |
||
| 52 | 'isleft' => preg_match('/\-/', $fmatch[1]) > 0, |
||
| 53 | ]; |
||
| 54 | $width = trim($fmatch[2]) ? (int)$fmatch[2] : 0; |
||
| 55 | $left = trim($fmatch[3]) ? (int)$fmatch[3] : 0; |
||
| 56 | $right = trim($fmatch[4]) ? (int)$fmatch[4] : $locale['int_frac_digits']; |
||
| 57 | $conversion = $fmatch[5]; |
||
| 58 | |||
| 59 | $positive = true; |
||
| 60 | if ($value < 0) { |
||
| 61 | $positive = false; |
||
| 62 | $value *= -1; |
||
| 63 | } |
||
| 64 | $letter = $positive ? 'p' : 'n'; |
||
| 65 | |||
| 66 | $prefix = $suffix = $cprefix = $csuffix = $signal = ''; |
||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 67 | |||
| 68 | $signal = $positive ? $locale['positive_sign'] : $locale['negative_sign']; |
||
| 69 | switch (true) { |
||
| 70 | case $locale["{$letter}_sign_posn"] == 1 && $flags['usesignal'] == '+': |
||
| 71 | $prefix = $signal; |
||
| 72 | break; |
||
| 73 | case $locale["{$letter}_sign_posn"] == 2 && $flags['usesignal'] == '+': |
||
| 74 | $suffix = $signal; |
||
| 75 | break; |
||
| 76 | case $locale["{$letter}_sign_posn"] == 3 && $flags['usesignal'] == '+': |
||
| 77 | $cprefix = $signal; |
||
| 78 | break; |
||
| 79 | case $locale["{$letter}_sign_posn"] == 4 && $flags['usesignal'] == '+': |
||
| 80 | $csuffix = $signal; |
||
| 81 | break; |
||
| 82 | case $flags['usesignal'] == '(': |
||
| 83 | case $locale["{$letter}_sign_posn"] == 0: |
||
| 84 | $prefix = '('; |
||
| 85 | $suffix = ')'; |
||
| 86 | break; |
||
| 87 | } |
||
| 88 | if (!$flags['nosimbol']) { |
||
| 89 | $currency = $cprefix . |
||
| 90 | ($conversion == 'i' ? $locale['int_curr_symbol'] : $locale['currency_symbol']) . |
||
| 91 | $csuffix; |
||
| 92 | } else { |
||
| 93 | $currency = ''; |
||
| 94 | } |
||
| 95 | $space = $locale["{$letter}_sep_by_space"] ? ' ' : ''; |
||
| 96 | |||
| 97 | $value = number_format($value, $right, $locale['mon_decimal_point'], |
||
| 98 | $flags['nogroup'] ? '' : $locale['mon_thousands_sep']); |
||
| 99 | $value = @explode($locale['mon_decimal_point'], $value); |
||
| 100 | |||
| 101 | $n = strlen($prefix) + strlen($currency) + strlen($value[0]); |
||
| 102 | if ($left > 0 && $left > $n) { |
||
| 103 | $value[0] = str_repeat($flags['fillchar'], $left - $n) . $value[0]; |
||
| 104 | } |
||
| 105 | $value = implode($locale['mon_decimal_point'], $value); |
||
| 106 | if ($locale["{$letter}_cs_precedes"]) { |
||
| 107 | $value = $prefix . $currency . $space . $value . $suffix; |
||
| 108 | } else { |
||
| 109 | $value = $prefix . $value . $space . $currency . $suffix; |
||
| 110 | } |
||
| 111 | if ($width > 0) { |
||
| 112 | $value = str_pad($value, $width, $flags['fillchar'], $flags['isleft'] ? |
||
| 113 | STR_PAD_RIGHT : STR_PAD_LEFT); |
||
| 114 | } |
||
| 115 | |||
| 116 | $format = str_replace($fmatch[0], $value, $format); |
||
| 117 | } |
||
| 118 | |||
| 119 | return $format; |
||
| 120 | } |
||
| 121 | } |