Issues (3083)

money_format_polyfill/src/money_format.php (1 issue)

Severity
1
<?php
2
3
if (!function_exists('money_format')) {
4
5
    /**
6
     * Formats a number as a currency string.
7
     *
8
     * @param string $format The format specification.
9
     *                       See {@link http://php.net/money_format}.
10
     * @param number $number The number to be formatted.
11
     *
12
     * @return string Returns the formatted string.
13
     * Characters before and after the formatting string will be returned unchanged.
14
     */
15
    function money_format($format, $number)
16
    {
17
        $regex  = '/%((?:[\^!\-]|\+|\(|\=.)*)([0-9]+)?'.
18
                  '(?:#([0-9]+))?(?:\.([0-9]+))?([in%])/';
19
        if (setlocale(LC_MONETARY, 0) == 'C') {
20
            setlocale(LC_MONETARY, '');
21
        }
22
        $locale = localeconv();
23
        preg_match_all($regex, $format, $matches, PREG_SET_ORDER);
24
        foreach ($matches as $fmatch) {
25
            $value = floatval($number);
26
            $flags = array(
27
                'fillchar'  => preg_match('/\=(.)/', $fmatch[1], $match) ?
28
                               $match[1] : ' ',
29
                'nogroup'   => preg_match('/\^/', $fmatch[1]) > 0,
30
                'usesignal' => preg_match('/\+|\(/', $fmatch[1], $match) ?
31
                               $match[0] : '+',
32
                'nosimbol'  => preg_match('/\!/', $fmatch[1]) > 0,
33
                'isleft'    => preg_match('/\-/', $fmatch[1]) > 0
34
            );
35
            $width      = trim($fmatch[2]) ? (int)$fmatch[2] : 0;
36
            $left       = trim($fmatch[3]) ? (int)$fmatch[3] : 0;
37
            $right      = trim($fmatch[4]) === ''
38
                ? $locale['int_frac_digits'] : (int)$fmatch[4];
39
            $conversion = $fmatch[5];
40
41
            $positive = true;
42
            if ($value < 0) {
43
                $positive = false;
44
                $value  *= -1;
45
            }
46
            $letter = $positive ? 'p' : 'n';
47
48
            $prefix = $suffix = $cprefix = $csuffix = $signal = '';
0 ignored issues
show
The assignment to $signal is dead and can be removed.
Loading history...
49
50
            $signal = $positive
51
                ? $locale['positive_sign'] : $locale['negative_sign'];
52
            switch (true) {
53
            case $locale["{$letter}_sign_posn"] == 1 && $flags['usesignal'] == '+':
54
                $prefix = $signal;
55
                break;
56
            case $locale["{$letter}_sign_posn"] == 2 && $flags['usesignal'] == '+':
57
                $suffix = $signal;
58
                break;
59
            case $locale["{$letter}_sign_posn"] == 3 && $flags['usesignal'] == '+':
60
                $cprefix = $signal;
61
                break;
62
            case $locale["{$letter}_sign_posn"] == 4 && $flags['usesignal'] == '+':
63
                $csuffix = $signal;
64
                break;
65
            case $flags['usesignal'] == '(' && $letter === 'n':
66
            case $locale["{$letter}_sign_posn"] == 0:
67
                $prefix = '(';
68
                $suffix = ')';
69
                break;
70
            }
71
            if (!$flags['nosimbol']) {
72
                $currency = $cprefix . ($conversion == 'i'
73
                            ? $locale['int_curr_symbol'] : $locale['currency_symbol']
74
                        ) . $csuffix;
75
            } else {
76
                $currency = '';
77
            }
78
            $space  = $locale["{$letter}_sep_by_space"] ? ' ' : '';
79
80
            $value = number_format(
81
                $value,
82
                $right,
83
                $locale['mon_decimal_point'],
84
                $flags['nogroup'] ? '' : $locale['mon_thousands_sep']
85
            );
86
            $value = @explode($locale['mon_decimal_point'], $value);
87
88
            $n = strlen($prefix) + strlen($currency) + strlen($value[0]);
89
            if ($left > 0 && $left > $n) {
90
                $value[0] = str_repeat($flags['fillchar'], $left - $n) . $value[0];
91
            }
92
            $value = implode($locale['mon_decimal_point'], $value);
93
            if ($locale["{$letter}_cs_precedes"]) {
94
                $value = $prefix . $currency . $space . $value . $suffix;
95
            } else {
96
                $value = $prefix . $value . $space . $currency . $suffix;
97
            }
98
            if ($width > 0) {
99
                $value = str_pad(
100
                    $value,
101
                    $width,
102
                    $flags['fillchar'],
103
                    $flags['isleft'] ? STR_PAD_RIGHT : STR_PAD_LEFT
104
                );
105
            }
106
107
            $format = str_replace($fmatch[0], $value, $format);
108
        }
109
        return $format;
110
    }
111
}
112