Json::set()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace cse\helpers;
6
7
use cse\helpers\Exceptions\CSEHelpersJsonException;
8
9
/**
10
 * Class Json
11
 *
12
 * @package cse\helpers
13
 */
14
class Json
15
{
16
    const JSON_DEFAULT_UNESCAPED = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;
17
    const PRETTY_PRINT = self::JSON_DEFAULT_UNESCAPED | JSON_PRETTY_PRINT;
18
19
    /**
20
     * @var bool
21
     */
22
    protected static $checkException = false;
23
24
    /**
25
     * Json encode
26
     *
27
     * @param array $array
28
     * @param int $options
29
     *
30
     * @return string
31
     *
32
     * @throws CSEHelpersJsonException
33
     */
34
    public static function encode(array $array, int $options = self::JSON_DEFAULT_UNESCAPED): string
35
    {
36
        $result = json_encode($array, $options);
37
38
        if (self::$checkException) self::errorToException();
39
40
        return $result;
41
    }
42
43
    /**
44
     * Print JSON data
45
     *
46
     * @param mixed $data
47
     *
48
     * @return string
49
     *
50
     * @throws CSEHelpersJsonException
51
     */
52
    public static function prettyPrint($data): string
53
    {
54
        $result = json_encode($data, self::PRETTY_PRINT);
55
56
        if (self::$checkException) self::errorToException();
57
58
        return $result;
59
    }
60
61
    /**
62
     * Json decode
63
     *
64
     * @param string $json
65
     * @param bool $assoc
66
     *
67
     * @return mixed
68
     *
69
     * @throws CSEHelpersJsonException
70
     */
71
    public static function decode(string $json, bool $assoc = true)
72
    {
73
        $result = json_decode($json, $assoc);
74
75
        if (self::$checkException) self::errorToException();
76
77
        return $result;
78
    }
79
80
    /**
81
     * Get json data
82
     *
83
     * @param string $json
84
     * @param string $key
85
     * @param mixed|null $default
86
     *
87
     * @return string
88
     *
89
     * @throws CSEHelpersJsonException
90
     */
91
    public static function get(string $json, string $key, $default = null)
92
    {
93
        return self::decode($json)[$key] ?? $default;
94
    }
95
96
    /**
97
     * Set json data
98
     *
99
     * @param string $json
100
     * @param string $key
101
     * @param mixed $value
102
     * @param int $options
103
     *
104
     * @return string
105
     *
106
     * @throws CSEHelpersJsonException
107
     */
108
    public static function set(string $json, string $key, $value, int $options = self::JSON_DEFAULT_UNESCAPED): string
109
    {
110
        $result = self::decode($json);
111
        $result[$key] = $value;
112
113
        return self::encode($result, $options);
114
    }
115
116
    /**
117
     * Set array json data
118
     *
119
     * @param string $json
120
     * @param array $data
121
     * @param int $options
122
     *
123
     * @return string
124
     *
125
     * @throws CSEHelpersJsonException
126
     */
127
    public static function setArray(string $json, array $data, int $options = self::JSON_DEFAULT_UNESCAPED): string
128
    {
129
        $result = self::decode($json);
130
131
        return self::encode(array_merge($result, $data), $options);
132
    }
133
134
    /**
135
     * Set check Exception
136
     *
137
     * @param bool $status
138
     */
139
    public static function setCheckException(bool $status = true): void
140
    {
141
        self::$checkException = $status;
142
    }
143
144
    /**
145
     * Check error last json transform
146
     *
147
     * @return bool
148
     */
149
    public static function isNoteError(): bool
150
    {
151
        return empty(json_last_error());
152
    }
153
154
    /**
155
     * Get error msg
156
     *
157
     * @param mixed|null $msg
158
     *
159
     * @return string|null
160
     */
161
    public static function getErrorMsg($msg = null): ?string
162
    {
163
        return self::isNoteError() ? null : json_last_error_msg() . (empty($msg) ? '' : ' ' . print_r($msg ,true));
164
    }
165
166
    /**
167
     * Error to exception
168
     *
169
     * @param mixed|null $msg
170
     *
171
     * @throws CSEHelpersJsonException
172
     */
173
    public static function errorToException($msg = null): void
174
    {
175
        if (self::isNoteError()) return;
176
177
        throw new CSEHelpersJsonException(
178
            self::getErrorMsg($msg),
179
            json_last_error()
180
        );
181
    }
182
}