Completed
Branch fix-wp-gql-for-plain-urls (809337)
by
unknown
55:28 queued 46:00
created

JsonDataHandler::setEncodeFlags()   C

Complexity

Conditions 16
Paths 30

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
nc 30
nop 1
dl 0
loc 23
rs 5.5666
c 0
b 0
f 0

How to fix   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
namespace EventEspresso\core\services\json;
4
5
use stdClass;
6
7
/**
8
 * Class JsonDataHandler
9
 * A class for handling serialization to and from JSON and tracking any errors that occur during that process
10
 *
11
 * @author  Brent Christensen
12
 * @package EventEspresso\core\services\json
13
 * @since   $VID:$
14
 */
15
class JsonDataHandler
16
{
17
    const DATA_TYPE_ARRAY     = 'array';
18
19
    const DATA_TYPE_OBJECT    = 'object';
20
21
    const DATA_TYPE_USE_FLAGS = 'flags';
22
23
    /**
24
     * @var string
25
     */
26
    private $data_type;
27
28
    /**
29
     * @var array|stdClass
30
     */
31
    private $decoded_data;
32
33
    /**
34
     * JSON_BIGINT_AS_STRING,
35
     * JSON_INVALID_UTF8_IGNORE,
36
     * JSON_INVALID_UTF8_SUBSTITUTE,
37
     * JSON_OBJECT_AS_ARRAY,
38
     * JSON_THROW_ON_ERROR
39
     *
40
     * @var int
41
     */
42
    private $decode_flags;
43
44
    /**
45
     * @var int
46
     */
47
    private $depth;
48
49
    /**
50
     * @var string
51
     */
52
    private $encoded_data;
53
54
    /**
55
     * JSON_FORCE_OBJECT,
56
     * JSON_HEX_QUOT,
57
     * JSON_HEX_TAG,
58
     * JSON_HEX_AMP,
59
     * JSON_HEX_APOS,
60
     * JSON_INVALID_UTF8_IGNORE,
61
     * JSON_INVALID_UTF8_SUBSTITUTE,
62
     * JSON_NUMERIC_CHECK,
63
     * JSON_PARTIAL_OUTPUT_ON_ERROR,
64
     * JSON_PRESERVE_ZERO_FRACTION,
65
     * JSON_PRETTY_PRINT,
66
     * JSON_UNESCAPED_LINE_TERMINATORS,
67
     * JSON_UNESCAPED_SLASHES,
68
     * JSON_UNESCAPED_UNICODE,
69
     * JSON_THROW_ON_ERROR.
70
     *
71
     * @var int
72
     */
73
    private $encode_flags;
74
75
    /**
76
     * @var int
77
     */
78
    private $last_error_code;
79
80
    /**
81
     * @var string
82
     */
83
    private $last_error_msg;
84
85
86
    /**
87
     * JsonDataHandler constructor.
88
     */
89
    public function __construct()
90
    {
91
        if (!defined('JSON_INVALID_UTF8_IGNORE')) {
92
            define('JSON_INVALID_UTF8_IGNORE', 1048576);
93
        }
94
        if (!defined('JSON_INVALID_UTF8_SUBSTITUTE')) {
95
            define('JSON_INVALID_UTF8_SUBSTITUTE', 2097152);
96
        }
97
        if (!defined('JSON_THROW_ON_ERROR')) {
98
            define('JSON_THROW_ON_ERROR', 4194304);
99
        }
100
    }
101
102
103
    /**
104
     * set $data_type, $decode_flags, $encode_flags, and depth all in one shot
105
     *
106
     * @param string $data_type
107
     * @param int    $decode_flags
108
     * @param int    $encode_flags
109
     * @param int    $depth
110
     */
111
    public function configure(
112
        string $data_type = JsonDataHandler::DATA_TYPE_USE_FLAGS,
113
        int $decode_flags = 0,
114
        int $encode_flags = 0,
115
        int $depth = 512
116
    ) {
117
        $this->setDataType($data_type);
118
        $this->setDecodeFlags($decode_flags);
119
        $this->setDepth($depth);
120
        $this->setEncodeFlags($encode_flags);
121
    }
122
123
124
    /**
125
     * @param string $data_type
126
     */
127
    public function setDataType(string $data_type): void
128
    {
129
        $this->data_type = $data_type === JsonDataHandler::DATA_TYPE_ARRAY
130
                           || $data_type === JsonDataHandler::DATA_TYPE_OBJECT
131
                           || $data_type === JsonDataHandler::DATA_TYPE_USE_FLAGS
132
            ? $data_type
133
            : JsonDataHandler::DATA_TYPE_USE_FLAGS;
134
    }
135
136
137
    /**
138
     * One or more Bitmask values:
139
     * JSON_BIGINT_AS_STRING,
140
     * JSON_INVALID_UTF8_IGNORE,        PHP >= 7.2
141
     * JSON_INVALID_UTF8_SUBSTITUTE,    PHP >= 7.2
142
     * JSON_OBJECT_AS_ARRAY,
143
     * JSON_THROW_ON_ERROR              PHP >= 7.3
144
     *
145
     * pass multiple values separated with |
146
     * ex: JSON_BIGINT_AS_STRING | JSON_INVALID_UTF8_IGNORE | JSON_OBJECT_AS_ARRAY
147
     *
148
     * @param int $decode_flags
149
     */
150
    public function setDecodeFlags(int $decode_flags): void
151
    {
152
        $this->decode_flags = $decode_flags === JSON_BIGINT_AS_STRING
153
                              || $decode_flags === JSON_OBJECT_AS_ARRAY
154
                              // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_invalid_utf8_ignoreFound
155
                              || $decode_flags === JSON_INVALID_UTF8_IGNORE
156
                              // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_invalid_utf8_substituteFound
157
                              || $decode_flags === JSON_INVALID_UTF8_SUBSTITUTE
158
                              // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_throw_on_errorFound
159
                              || $decode_flags === JSON_THROW_ON_ERROR
160
            ? $decode_flags
161
            : 0;
162
    }
163
164
165
    /**
166
     * @param int $depth
167
     */
168
    public function setDepth(int $depth): void
169
    {
170
        $depth = absint($depth);
171
        $this->depth = $depth ? $depth : 512;
172
    }
173
174
175
    /**
176
     * One or more Bitmask values:
177
     * JSON_FORCE_OBJECT,
178
     * JSON_HEX_QUOT,
179
     * JSON_HEX_TAG,
180
     * JSON_HEX_AMP,
181
     * JSON_HEX_APOS,
182
     * JSON_INVALID_UTF8_IGNORE,        PHP >= 7.2
183
     * JSON_INVALID_UTF8_SUBSTITUTE,    PHP >= 7.2
184
     * JSON_NUMERIC_CHECK,
185
     * JSON_PARTIAL_OUTPUT_ON_ERROR,
186
     * JSON_PRESERVE_ZERO_FRACTION,
187
     * JSON_PRETTY_PRINT,
188
     * JSON_UNESCAPED_LINE_TERMINATORS,
189
     * JSON_UNESCAPED_SLASHES,
190
     * JSON_UNESCAPED_UNICODE,
191
     * JSON_THROW_ON_ERROR.             PHP >= 7.3
192
     *
193
     * pass multiple values separated with |
194
     * ex: JSON_FORCE_OBJECT | JSON_INVALID_UTF8_IGNORE | JSON_THROW_ON_ERROR
195
     *
196
     * @param int $encode_flags
197
     */
198
    public function setEncodeFlags(int $encode_flags): void
199
    {
200
        $this->encode_flags = $encode_flags === JSON_FORCE_OBJECT
201
                              || $encode_flags === JSON_HEX_QUOT
202
                              || $encode_flags === JSON_HEX_TAG
203
                              || $encode_flags === JSON_HEX_AMP
204
                              || $encode_flags === JSON_HEX_APOS
205
                              || $encode_flags === JSON_NUMERIC_CHECK
206
                              || $encode_flags === JSON_PARTIAL_OUTPUT_ON_ERROR
207
                              || $encode_flags === JSON_PRESERVE_ZERO_FRACTION
208
                              || $encode_flags === JSON_PRETTY_PRINT
209
                              || $encode_flags === JSON_UNESCAPED_LINE_TERMINATORS
210
                              || $encode_flags === JSON_UNESCAPED_SLASHES
211
                              || $encode_flags === JSON_UNESCAPED_UNICODE
212
                              // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_invalid_utf8_ignoreFound
213
                              || $encode_flags === JSON_INVALID_UTF8_IGNORE
214
                              // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_invalid_utf8_substituteFound
215
                              || $encode_flags === JSON_INVALID_UTF8_SUBSTITUTE
216
                              // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_throw_on_errorFound
217
                              || $encode_flags === JSON_THROW_ON_ERROR
218
            ? $encode_flags
219
            : 0;
220
    }
221
222
223
    /**
224
     * @return bool|null
225
     */
226
    private function asAssociative()
227
    {
228
        switch ($this->data_type) {
229
            case JsonDataHandler::DATA_TYPE_ARRAY:
230
                return true;
231
            case JsonDataHandler::DATA_TYPE_OBJECT:
232
                return false;
233
            case JsonDataHandler::DATA_TYPE_USE_FLAGS:
234
                return null;
235
        }
236
        return null;
237
    }
238
239
240
    /**
241
     * @param string $json
242
     * @return array|mixed|stdClass
243
     */
244
    public function decodeJson(string $json)
245
    {
246
        $this->decoded_data    = json_decode($json, $this->asAssociative(), $this->depth, $this->decode_flags);
247
        $this->last_error_code = json_last_error();
248
        $this->last_error_msg  = json_last_error_msg();
249
        return $this->decoded_data;
250
    }
251
252
253
    /**
254
     * @param $data
255
     * @return false|string
256
     */
257
    public function encodeData($data)
258
    {
259
        $this->encoded_data    = json_encode($data, $this->encode_flags, $this->depth);
260
        $this->last_error_code = json_last_error();
261
        $this->last_error_msg  = json_last_error_msg();
262
        return $this->encoded_data;
263
    }
264
265
266
    /**
267
     * @return array|stdClass
268
     */
269
    public function getDecodedData()
270
    {
271
        return $this->decoded_data;
272
    }
273
274
275
    /**
276
     * @return string
277
     */
278
    public function getEncodedData(): string
279
    {
280
        return $this->encoded_data;
281
    }
282
283
284
    /**
285
     * @param false $reset
286
     * @return int
287
     */
288
    public function getLastErrorCode($reset = false)
289
    {
290
        $last_error = $this->last_error_code;
291
        if ($reset) {
292
            $this->last_error_code = JSON_ERROR_NONE;
293
        }
294
        return $last_error;
295
    }
296
297
298
    /**
299
     * @param false $reset
300
     * @return string
301
     */
302
    public function getLastErrorMessage($reset = false)
303
    {
304
        $last_error = $this->last_error_msg;
305
        if ($reset) {
306
            $this->last_error_msg = '';
307
        }
308
        return $last_error;
309
    }
310
}
311