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

XoopsLocalAbstract::formatCurrency()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * XOOPS localization abstract
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
13
 * @license             GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14
 * @package             kernel
15
 * @since               2.3.0
16
 * @author              Taiwen Jiang <[email protected]>
17
 */
18
19
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
20
21
/**
22
 * Class XoopsLocalAbstract
23
 */
24
class XoopsLocalAbstract
25
{
26
    /**
27
     * XoopsLocalAbstract::substr()
28
     *
29
     * @param mixed  $str
30
     * @param mixed  $start
31
     * @param mixed  $length
32
     * @param string $trimmarker
33
     *
34
     * @return mixed|string
35
     */
36
    public static function substr($str, $start, $length, $trimmarker = '...')
37
    {
38
        if (!XOOPS_USE_MULTIBYTES) {
39
            return (strlen($str) - $start <= $length) ? substr($str, $start, $length) : substr($str, $start, $length - strlen($trimmarker)) . $trimmarker;
40
        }
41
        if (function_exists('mb_internal_encoding') && @mb_internal_encoding(_CHARSET)) {
42
            $str2 = mb_strcut($str, $start, $length - strlen($trimmarker));
43
44
            return $str2 . (mb_strlen($str) != mb_strlen($str2) ? $trimmarker : '');
45
        }
46
47
        return $str;
48
    }
49
    // Each local language should define its own equalient utf8_encode
50
    /**
51
     * XoopsLocalAbstract::utf8_encode()
52
     *
53
     * @param  mixed $text
54
     * @return string
55
     */
56
    public static function utf8_encode($text)
57
    {
58
        if (XOOPS_USE_MULTIBYTES == 1) {
0 ignored issues
show
introduced by
The condition XOOPS_USE_MULTIBYTES == 1 is always false.
Loading history...
59
            if (function_exists('mb_convert_encoding')) {
60
                return mb_convert_encoding($text, 'UTF-8', 'auto');
61
            }
62
        }
63
64
        return utf8_encode($text);
65
    }
66
67
    /**
68
     * XoopsLocalAbstract::convert_encoding()
69
     *
70
     * @param  mixed  $text
71
     * @param  string $to
72
     * @param  string $from
73
     * @return mixed|string
74
     */
75
    public static function convert_encoding($text, $to = 'utf-8', $from = '')
76
    {
77
        if (empty($text)) {
78
            return $text;
79
        }
80
        if (empty($from)) {
81
            $from = empty($GLOBALS['xlanguage']['charset_base']) ? _CHARSET : $GLOBALS['xlanguage']['charset_base'];
82
        }
83
        if (empty($to) || !strcasecmp($to, $from)) {
84
            return $text;
85
        }
86
87
        if (XOOPS_USE_MULTIBYTES && function_exists('mb_convert_encoding')) {
88
            $converted_text = @mb_convert_encoding($text, $to, $from);
89
        } elseif (function_exists('iconv')) {
90
            $converted_text = @iconv($from, $to . '//TRANSLIT', $text);
91
        } elseif ('utf-8' === $to) {
92
            $converted_text = utf8_encode($text);
93
        }
94
        $text = empty($converted_text) ? $text : $converted_text;
95
96
        return $text;
97
    }
98
99
    /**
100
     * XoopsLocalAbstract::trim()
101
     *
102
     * @param  mixed $text
103
     * @return string
104
     */
105
    public static function trim($text)
106
    {
107
        $ret = trim($text);
108
109
        return $ret;
110
    }
111
112
    /**
113
     * Get description for setting time format
114
     */
115
    public static function getTimeFormatDesc()
116
    {
117
        return _TIMEFORMAT_DESC;
118
    }
119
120
    /**
121
     * Function to display formatted times in user timezone
122
     *
123
     * Setting $timeoffset to null (by default) will skip timezone calculation for user, using default timezone instead, which is a MUST for cached contents
124
     * @param        $time
125
     * @param string $format
126
     * @param null|string   $timeoffset
127
     * @return string
128
     */
129
    public static function formatTimestamp($time, $format = 'l', $timeoffset = null)
130
    {
131
        global $xoopsConfig, $xoopsUser;
132
133
        $format_copy = $format;
134
        $format      = strtolower($format);
135
136
        if ($format === 'rss' || $format === 'r') {
137
            $TIME_ZONE = '';
138
            if (isset($GLOBALS['xoopsConfig']['server_TZ'])) {
139
                $server_TZ = abs((int)($GLOBALS['xoopsConfig']['server_TZ'] * 3600.0));
140
                $prefix    = ($GLOBALS['xoopsConfig']['server_TZ'] < 0) ? ' -' : ' +';
141
                $TIME_ZONE = $prefix . date('Hi', $server_TZ);
0 ignored issues
show
Bug introduced by
It seems like $server_TZ can also be of type double; however, parameter $timestamp of date() does only seem to accept integer|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

141
                $TIME_ZONE = $prefix . date('Hi', /** @scrutinizer ignore-type */ $server_TZ);
Loading history...
142
            }
143
            $date = gmdate('D, d M Y H:i:s', (int)$time) . $TIME_ZONE;
144
145
            return $date;
146
        }
147
148
        if (($format === 'elapse' || $format === 'e') && $time < time()) {
149
            $elapse = time() - $time;
150
            if ($days = floor($elapse / (24 * 3600))) {
151
                $num = $days > 1 ? sprintf(_DAYS, $days) : _DAY;
152
            } elseif ($hours = floor(($elapse % (24 * 3600)) / 3600)) {
153
                $num = $hours > 1 ? sprintf(_HOURS, $hours) : _HOUR;
154
            } elseif ($minutes = floor(($elapse % 3600) / 60)) {
155
                $num = $minutes > 1 ? sprintf(_MINUTES, $minutes) : _MINUTE;
156
            } else {
157
                $seconds = $elapse % 60;
158
                $num     = $seconds > 1 ? sprintf(_SECONDS, $seconds) : _SECOND;
159
            }
160
            $ret = sprintf(_ELAPSE, $num);
161
162
            return $ret;
163
        }
164
        // disable user timezone calculation and use default timezone,
165
        // for cache consideration
166
        if ($timeoffset === null) {
167
            $timeoffset = ($xoopsConfig['default_TZ'] == '') ? '0.0' : $xoopsConfig['default_TZ'];
168
        }
169
        $usertimestamp = xoops_getUserTimestamp($time, $timeoffset);
170
        switch ($format) {
171
            case 's':
172
                $datestring = _SHORTDATESTRING;
173
                break;
174
175
            case 'm':
176
                $datestring = _MEDIUMDATESTRING;
177
                break;
178
179
            case 'mysql':
180
                $datestring = 'Y-m-d H:i:s';
181
                break;
182
183
            case 'l':
184
                $datestring = _DATESTRING;
185
                break;
186
187
            case 'c':
188
            case 'custom':
189
                static $current_timestamp, $today_timestamp, $monthy_timestamp;
190
                if (!isset($current_timestamp)) {
191
                    $current_timestamp = xoops_getUserTimestamp(time(), $timeoffset);
192
                }
193
                if (!isset($today_timestamp)) {
194
                    $today_timestamp = mktime(0, 0, 0, date('m', $current_timestamp), date('d', $current_timestamp), date('Y', $current_timestamp));
0 ignored issues
show
Bug introduced by
date('Y', $current_timestamp) of type string is incompatible with the type integer expected by parameter $year of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

194
                    $today_timestamp = mktime(0, 0, 0, date('m', $current_timestamp), date('d', $current_timestamp), /** @scrutinizer ignore-type */ date('Y', $current_timestamp));
Loading history...
Bug introduced by
date('m', $current_timestamp) of type string is incompatible with the type integer expected by parameter $month of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

194
                    $today_timestamp = mktime(0, 0, 0, /** @scrutinizer ignore-type */ date('m', $current_timestamp), date('d', $current_timestamp), date('Y', $current_timestamp));
Loading history...
Bug introduced by
date('d', $current_timestamp) of type string is incompatible with the type integer expected by parameter $day of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

194
                    $today_timestamp = mktime(0, 0, 0, date('m', $current_timestamp), /** @scrutinizer ignore-type */ date('d', $current_timestamp), date('Y', $current_timestamp));
Loading history...
195
                }
196
197
                if (abs($elapse_today = $usertimestamp - $today_timestamp) < 24 * 60 * 60) {
198
                    $datestring = ($elapse_today > 0) ? _TODAY : _YESTERDAY;
199
                } else {
200
                    if (!isset($monthy_timestamp)) {
201
                        $monthy_timestamp[0] = mktime(0, 0, 0, 0, 0, date('Y', $current_timestamp));
202
                        $monthy_timestamp[1] = mktime(0, 0, 0, 0, 0, date('Y', $current_timestamp) + 1);
203
                    }
204
                    $datestring = _YEARMONTHDAY;
205
                    if ($usertimestamp >= $monthy_timestamp[0] && $usertimestamp < $monthy_timestamp[1]) {
206
                        $datestring = _MONTHDAY;
207
                    }
208
                }
209
                break;
210
211
            default:
212
                $datestring = _DATESTRING;
213
                if ($format != '') {
214
                    $datestring = $format_copy;
215
                }
216
                break;
217
        }
218
219
        return ucfirst(date($datestring, $usertimestamp));
220
    }
221
222
    /**
223
     * XoopsLocalAbstract::number_format()
224
     *
225
     * @param  mixed $number
226
     * @return mixed
227
     */
228
    public function number_format($number)
229
    {
230
        return $number;
231
    }
232
233
    /**
234
     * XoopsLocalAbstract::money_format()
235
     *
236
     * @param  mixed $format
237
     * @param  mixed $number
238
     * @return mixed
239
     */
240
    public function money_format($format, $number)
241
    {
242
        return $number;
243
    }
244
245
    /**
246
     * XoopsLocalAbstract::__call()
247
     *
248
     * @param  mixed $name
249
     * @param  mixed $args
250
     * @return mixed
251
     */
252
    public function __call($name, $args)
253
    {
254
        if (function_exists($name)) {
255
            return call_user_func_array($name, $args);
256
        }
257
        return null;
258
    }
259
260
    /**
261
     * Format a currency value
262
     *
263
     * @param float $amount The numeric currency value.
264
     * @param string $currency The 3-letter ISO 4217 currency code indicating the currency to use.
265
     * @return mixed formatted currency value
266
     */
267
    function formatCurrency($amount, $currency)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Unused Code introduced by
The parameter $currency is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

267
    function formatCurrency($amount, /** @scrutinizer ignore-unused */ $currency)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
268
    {
269
        return $amount;
270
    }
271
272
273
274
}
275