Passed
Push — master ( c1245f...df11ef )
by Eliseev
01:45
created

Json::setCheckException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
18
    /**
19
     * @var bool
20
     */
21
    protected static $checkException = false;
22
23
    /**
24
     * Json encode
25
     *
26
     * @param array $data
27
     * @param int $options
28
     *
29
     * @return string
30
     *
31
     * @throws CSEHelpersJsonException
32
     */
33
    public static function encode(array $data, int $options = self::JSON_DEFAULT_UNESCAPED): string
34
    {
35
        $result = json_encode($data, $options);
36
37
        if (self::$checkException) self::errorToException();
38
39
        return $result;
40
    }
41
42
    /**
43
     * Print JSON data
44
     *
45
     * @param $data
46
     *
47
     * @return string
48
     *
49
     * @throws CSEHelpersJsonException
50
     */
51
    public static function prettyPrint($data): string
52
    {
53
        $result = json_encode($data, JSON_PRETTY_PRINT);
54
55
        if (self::$checkException) self::errorToException();
56
57
        return $result;
58
    }
59
60
    /**
61
     * Json decode
62
     *
63
     * @param string $data
64
     * @param bool $assoc
65
     *
66
     * @return mixed
67
     *
68
     * @throws CSEHelpersJsonException
69
     */
70
    public static function decode(string $data, bool $assoc = true)
71
    {
72
        $result = json_decode($data, $assoc);
73
74
        if (self::$checkException) self::errorToException();
75
76
        return $result;
77
    }
78
79
    /**
80
     * Set check Exception
81
     *
82
     * @param bool $status
83
     */
84
    public static function setCheckException(bool $status = true): void
85
    {
86
        self::$checkException = $status;
87
    }
88
89
    /**
90
     * Check error last json transform
91
     *
92
     * @return bool
93
     */
94
    public static function isNoteError(): bool
95
    {
96
        return empty(json_last_error());
97
    }
98
99
    /**
100
     * Error to exception
101
     *
102
     * @param null $msg
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $msg is correct as it would always require null to be passed?
Loading history...
103
     *
104
     * @throws CSEHelpersJsonException
105
     */
106
    public static function errorToException($msg = null): void
107
    {
108
        if (self::isNoteError()) return;
109
110
        throw new CSEHelpersJsonException(
111
            json_last_error_msg() . (empty($msg) ? '' : ' ' . print_r($msg ,true)),
112
            json_last_error()
113
        );
114
    }
115
}