Completed
Pull Request — master (#561)
by Richard
13:57
created

AbstractLocale::formatTimestamp()   C

Complexity

Conditions 25
Paths 26

Size

Total Lines 72
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 43
CRAP Score 25.7071

Importance

Changes 0
Metric Value
cc 25
eloc 50
c 0
b 0
f 0
nc 26
nop 2
dl 0
loc 72
rs 5.3816
ccs 43
cts 48
cp 0.8958
crap 25.7071

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
namespace Xoops\Locale;
13
14
use Patchwork\Utf8;
15
use Xoops\Core\Locale\Punic\Calendar;
16
use Punic\Misc;
17
use \Xoops\Core\Locale\Time;
18
19
/**
20
 * XOOPS localization abstract
21
 *
22
 * @category  Xoops\Locale
23
 * @package   Xoops\Locale\Abstract
24
 * @author    Taiwen Jiang <[email protected]>
25
 * @copyright 2000-2015 XOOPS Project (http://xoops.org)
26
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
27
 * @link      http://xoops.org
28
 */
29
abstract class AbstractLocale
30
{
31
    /**
32
     * isMultiByte - does locale depend on multi-byte characters?
33
     *
34
     * @return bool true always true with UTF-8
35
     *
36
     * @deprecated since 2.6.0 -- UTF-8 is always used
37
     */
38 1
    public static function isMultiByte()
39
    {
40 1
        return true;
41
    }
42
43
    /**
44
     * isRtl - is text order right to left?
45
     *
46
     * @return bool true if right to left
47
     */
48 1
    public static function isRtl()
49
    {
50 1
        return ('right-to-left' === Misc::getCharacterOrder());
51
    }
52
53
    /**
54
     * todo, do not forget to set this on locale load
55
     */
56
    public static function setLocale()
57
    {
58
        return setlocale(LC_ALL, self::getLocale());
59
    }
60
61
    /**
62
     * getCharset - return current character set, always UTF-8
63
     *
64
     * @return string character set
65
     */
66 19
    public static function getCharset()
67
    {
68 19
        return 'UTF-8';
69
    }
70
71
    /**
72
     * getLocale - return the current locale
73
     *
74
     * @return string
75
     */
76 5
    public static function getLocale()
77
    {
78 5
        return \Xoops\Locale::getCurrent();
79
    }
80
81
    /**
82
     * getLangCode - return language code for the current locale (locale with '-' separator)
83
     *
84
     * @return string
85
     */
86 16
    public static function getLangCode()
87
    {
88 16
        return \Xoops\Locale::normalizeLocale(\Xoops\Locale::getCurrent(), '-', false);
89
    }
90
91
    /**
92
     * getLegacyLanguage - return legacy language code for the current locale
93
     * @return string
94
     */
95 9
    public static function getLegacyLanguage()
96
    {
97 9
        $legacyLanguages = \Xoops\Core\Locale\LegacyCodes::getLegacyName(\Xoops\Locale::getCurrent());
98 9
        return reset($legacyLanguages);
99
    }
100
101
    /**
102
     * @return string
103
     */
104 1
    public static function getTimezone()
105
    {
106 1
        return \Xoops\Locale::getTimeZone()->getName();
107
    }
108
109
    /**
110
     * The generic css fonts are:
111
     *  - cursive
112
     *  - fantasy
113
     *  - monospace
114
     *  - sans-serif
115
     *  - serif
116
     *
117
     * @return string[]
118
     */
119 1
    public static function getFonts()
120
    {
121
        return array(
122 1
            'Arial',
123
            'Courier',
124
            'Georgia',
125
            'Helvetica',
126
            'Impact',
127
            'Verdana',
128
            'Haettenschweiler'
129
        );
130
    }
131
132
    /**
133
     * The css should adjust based on the
134
     *    html:lang(ja) {
135
     *        font-size: 150%;
136
     *    }
137
     * Then classes can be relative to that base em
138
     * CJK fonts may need to be shown in a larger size due to complex glyphs
139
     *
140
     * @return array
141
     */
142 1
    public static function getFontSizes()
143
    {
144
        return array(
145 1
            'xx-small' => 'xx-Small',
146
            'x-small'  => 'x-Small',
147
            'small'    => 'Small',
148
            'medium'   => 'Medium',
149
            'large'    => 'Large',
150
            'x-large'  => 'x-Large',
151
            'xx-large' => 'xx-Large'
152
        );
153
    }
154
155
    /**
156
     * @return string[]
157
     */
158 1
    public static function getAdminRssUrls()
159
    {
160 1
        return array('http://www.xoops.org/backend.php');
161
    }
162
163
    /**
164
     * @param mixed   $str
165
     * @param integer $start
166
     * @param integer $length
167
     * @param string  $ellipsis
168
     *
169
     * @return string
170
     */
171 1
    public static function substr($str, $start, $length, $ellipsis = '…')
172
    {
173 1
        $str2 = mb_strcut($str, $start, $length - strlen($ellipsis));
174 1
        return $str2 . (mb_strlen($str)-$start != mb_strlen($str2) ? $ellipsis : '');
175
    }
176
177
    /**
178
     *  filter to UTF-8, converts invalid $text as CP1252 and forces NFC normalization
179
     *
180
     * @param mixed $text
181
     *
182
     * @return string
183
     */
184 1
    public static function utf8_encode($text)
185
    {
186 1
        return Utf8::filter($text);
187
    }
188
189
    /**
190
     * @param mixed  $text
191
     * @param string $to
192
     * @param string $from
193
     *
194
     * @return string
195
     *
196
     * @deprecated
197
     */
198 1
    public static function convert_encoding($text, $to = 'utf-8', $from = '')
0 ignored issues
show
Unused Code introduced by
The parameter $to is not used and could be removed.

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

Loading history...
Unused Code introduced by
The parameter $from is not used and could be removed.

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

Loading history...
199
    {
200 1
        return $text;
201
    }
202
203
    /**
204
     * XoopsLocalAbstract::trim()
205
     *
206
     * @param mixed $text
207
     *
208
     * @return string
209
     */
210 1
    public static function trim($text)
211
    {
212 1
        $ret = Utf8::trim($text);
213
214 1
        return $ret;
215
    }
216
217
    /**
218
     * Function to display formatted times in user timezone
219
     *
220
     * @param mixed  $time
221
     * @param string $format Format codes ()
222
     *                       's' or 'short'  - short;
223
     *                       'm' or 'medium' - medium;
224
     *                       'l' or 'long'   - long;
225
     *                       'c' or 'custom' - format determined according to interval to present;
226
     *                       'e' or 'elapse' - Elapsed;
227
     *                       'mysql' - Y-m-d H:i:s;
228
     *                       'rss'
229
     *
230
     * @return string
231
     */
232 36
    public static function formatTimestamp($time, $format = 'l')
233
    {
234 36
        $workingTime = Time::cleanTime($time);
235
236 36
        switch (strtolower($format)) {
237 36
            case 'short':
238 36
            case 's':
239 2
                return Time::formatDateTime($workingTime, 'short');
240
241 34
            case 'medium':
242 34
            case 'm':
243 2
                return Time::formatDateTime($workingTime, 'medium');
244
245 32
            case 'long':
246 32
            case 'l':
247 2
                return Time::formatDateTime($workingTime, 'long');
248
249 30
            case 'full':
250 30
            case 'f':
251 2
                return Time::formatDateTime($workingTime, 'full');
252
253 28
            case 'custom':
254 26
            case 'c':
255 2
                $specialName = Calendar::getDateRelativeName($workingTime, true);
256 2
                if ($specialName != '') {
257 2
                    return $specialName;
258
                }
259
                // no break - fall through
260 26
            case 'elapse':
261 12
            case 'e':
262 14
                return Time::describeRelativeInterval($workingTime);
263
264 12
            case 'short-date':
265 2
                return Time::formatDate($workingTime, 'short');
266
267 10
            case 'short-time':
268 2
                return Time::formatTime($workingTime, 'short');
269
270 8
            case 'medium-date':
271 1
                return Time::formatDate($workingTime, 'medium');
272
273 7
            case 'medium-time':
274 1
                return Time::formatTime($workingTime, 'medium');
275
276 6
            case 'long-date':
277
                return Time::formatDate($workingTime, 'long');
278
279 6
            case 'long-time':
280
                return Time::formatTime($workingTime, 'long');
281
282 6
            case 'full-date':
283 1
                return Time::formatDate($workingTime, 'full');
284
285 5
            case 'full-time':
286 1
                return Time::formatTime($workingTime, 'full');
287
288 4
            case 'rss':
289 2
                $workingTime->setTimezone(new \DateTimeZone('UTC'));
290 2
                return $workingTime->format($workingTime::RSS);
291
292 2
            case 'mysql':
293 2
                $workingTime->setTimezone(new \DateTimeZone('UTC'));
294 2
                return $workingTime->format('Y-m-d H:i:s');
295
296
            default:
297
                if ($format != '') {
298
                    return $workingTime->format($format);
299
                }
300
                return Time::formatDateTime($workingTime, 'long');
301
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
302
        }
303
    }
304
305
    /**
306
     * @param int $number
307
     *
308
     * @return string
309
     */
310 1
    public static function number_format($number)
311
    {
312 1
        return number_format($number, 2, '.', ',');
313
    }
314
315
    /**
316
     * @param string $format
317
     * @param string $number
318
     *
319
     * @return string
320
     */
321 1
    public static function money_format($format, $number)
322
    {
323 1
        if (function_exists('money_format')) {
324 1
            $result = money_format($format, $number);
325
        } else {
326
            $result = sprintf('%01.2f', $number);
327
        }
328
329 1
        return $result;
330
    }
331
332
    /**
333
     * Sort array values according to current locale rules, maintaining index association
334
     *
335
     * @param array $array to sort
336
     * @return void
337
     */
338 6
    public static function asort(&$array)
339
    {
340
        //if (class_exists('\Collator')) {
341
        //    $col = new \Collator(self::getLocale());
342
        //    $col->asort($array);
343
        //} else {
344
        //    asort($array);
345
        //}
346 6
        uasort($array, '\Patchwork\Utf8::strcasecmp');
347 6
    }
348
}
349