JSON::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 11 and the first side effect is on line 11.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * JSON.php
4
 *
5
 * @author Tian.
6
 */
7
8
namespace Wechat\Utils;
9
10
// for PHP5.3, prevent PHP Notice msg assumed
11
defined('JSON_UNESCAPED_UNICODE') || define('JSON_UNESCAPED_UNICODE', 256);
12
13
/**
14
 * Unicode2multi characters supported for the wechat server
15
 */
16
class JSON
17
{
18
    /**
19
     * To prevent new operation, under static usage only
20
     */
21
    protected function __construct()
22
    {
23
    }
24
25
    /**
26
     * PHP >= 5.3 JSON_UNESCAPED_UNICODE constant supported
27
     *
28
     * @param mixed $value   The value (except a resource) being encoded.
29
     * @param int   $options Bitmask consisting of blah...
30
     * @param int   $depth   Set the maximum depth. Must be greater than zero.
31
     *
32
     * @see http://php.net/manual/en/function.json-encode.php
33
     *
34
     * @return mixed Returns a string containing the JSON representation of data
35
     */
36
    public static function encode($value, $options = 0, $depth = 512)
37
    {
38
        // multi-characters supported by default
39
        $options |= JSON_UNESCAPED_UNICODE;
40
41
        $data = version_compare(PHP_VERSION, '5.5.0', '>=')
42
            ? json_encode($value, $options, $depth)
43
            : json_encode($value, $options);
44
45
        if (JSON_ERROR_NONE !== json_last_error()) {
46
            return $data;
47
        }
48
49
        return version_compare(PHP_VERSION, '5.4.0', '>=')
50
            ? $data
51
            : preg_replace_callback("/\\\\u([0-9a-f]{2})([0-9a-f]{2})/iu", function ($pipe) {
52
                return iconv(
53
                    strncasecmp(PHP_OS, 'WIN', 3) ? 'UCS-2BE' : 'UCS-2',
54
                    'UTF-8',
55
                    chr(hexdec($pipe[1])) . chr(hexdec($pipe[2]))
56
                );
57
            }, $data);
58
    }
59
60
    /**
61
     * PHP >= 5.3 options supported (TODO)
62
     *
63
     * @param string $json    The json string being decoded.
64
     * @param bool   $assoc   When TRUE, returned objects will be converted into associative arrays.
65
     * @param int    $depth   User specified recursion depth.
66
     * @param int    $options Bitmask of JSON decode options blah...
67
     *
68
     * @see http://php.net/manual/en/function.json-decode.php
69
     *
70
     * @return mixed Returns the value encoded in json in appropriate PHP type.
71
     */
72
    public static function decode($json, $assoc = false, $depth = 512, $options = 0)
73
    {
74
        if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
75
            return json_decode($json, $assoc, $depth, $options);
76
        }
77
78
        return json_decode($json, $assoc, $depth);
79
    }
80
}
81