Passed
Pull Request — master (#1078)
by Michael
06:23
created

XoopsLocal::money_format()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 10
nc 2
nop 2
dl 0
loc 16
rs 9.9332
c 1
b 0
f 1
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
/**
13
 * Xoops locale
14
 *
15
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
16
 * @license             GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
17
 * @package             kernel
18
 * @since               2.3.0
19
 * @author              Taiwen Jiang <[email protected]>
20
 * @todo                To be handled by i18n/l10n
21
 */
22
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
23
24
setlocale(LC_ALL, 'en_US');
25
26
// !!IMPORTANT!! insert '\' before any char among reserved chars: "a","A","B","c","d","D","e","F","g","G","h","H","i","I","j","l","L","m","M","n","O","r","s","S","t","T","U","w","W","Y","y","z","Z"
27
// insert double '\' before 't','r','n'
28
define('_TODAY', "\T\o\d\a\y G:i");
29
define('_YESTERDAY', "\Y\\e\s\\t\\e\\r\d\a\y G:i");
30
define('_MONTHDAY', 'n/j G:i');
31
define('_YEARMONTHDAY', 'Y/n/j G:i');
32
define('_ELAPSE', '%s ago');
33
define('_TIMEFORMAT_DESC', "Valid formats: \"s\" - " . _SHORTDATESTRING . "; \"m\" - " . _MEDIUMDATESTRING . "; \"l\" - " . _DATESTRING . ';<br>' . "\"c\" or \"custom\" - format determined according to interval to present; \"e\" - Elapsed; \"mysql\" - Y-m-d H:i:s;<br>" . "specified string - Refer to <a href=\"https://php.net/manual/en/function.date.php\" rel=\"external\">PHP manual</a>.");
34
35
if (!class_exists('XoopsLocalAbstract')) {
36
    include_once XOOPS_ROOT_PATH . '/class/xoopslocal.php';
37
}
38
39
/**
40
 * A Xoops Local
41
 *
42
 * @package             kernel
43
 * @subpackage          Language
44
 *
45
 * @author              Taiwen Jiang <[email protected]>
46
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
47
 */
48
class XoopsLocal extends XoopsLocalAbstract
49
{
50
    /**
51
     * Number Formats
52
     *
53
     * @param  int|float $number
54
     * @return string
55
     */
56
    public function number_format($number)
57
    {
58
        return number_format($number, 2, '.', ',');
59
    }
60
61
    /**
62
     * Money Format
63
     *
64
     * @param  string $format
65
     * @param  float $number
66
     * @return string|null formatted money or error message
67
     */
68
    public function money_format($format, $number)
69
    {
70
        $trace                  = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
71
        $errorMessageDeprecated = "Function '" . __FUNCTION__ . "' in '" . __FILE__ . ' is deprecated, ';
72
        $errorMessageRemoved    = "Function '" . __FUNCTION__ . "' in '" . __FILE__ . ' is removed in PHP8, ';
73
        $errorMessageAdvise     = 'use formatCurrency($number, $currency) instead.' . ". Called from {$trace[0]['file']}line {$trace[0]['line']}";
74
        $GLOBALS['xoopsLogger']->addDeprecated($errorMessageDeprecated . $errorMessageAdvise);
75
76
        setlocale(LC_MONETARY, 'en_US');
77
78
        if (function_exists('money_format')) {
79
            return money_format($format, $number);
80
        }
81
82
        trigger_error($errorMessageRemoved . $errorMessageAdvise, E_USER_NOTICE);
83
        return null;
84
    }
85
86
    /**
87
     * Format a currency value
88
     *
89
     * @param float  $amount The numeric currency value.
90
     * @param string $currency The 3-letter ISO 4217 currency code indicating the currency to use.
91
     * @return string String representing the formatted currency value.
92
     */
93
    public function formatCurrency($amount, $currency = 'USD')
94
    {
95
        $fmt = new \NumberFormatter('en_US', NumberFormatter::CURRENCY);
96
        return $fmt->formatCurrency($amount, $currency);
97
    }
98
99
}
100