Completed
Push — master ( 8a9a36...5d30b0 )
by Ondřej
03:00
created

Json::getJsonErrorMsg()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 3
eloc 12
nc 3
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Ivory\Value;
5
6
/**
7
 * Encapsulation of JSON-encoded data.
8
 *
9
 * The meaning of this class is to represent JSON-encoded data including the semantically insignificant whitespace among
10
 * tokens, as well as the order of object keys and multiple occurrences of the same key (when working with the actual
11
 * value, the last occurrence of each key is actually used). This reflects the PostgreSQL `JSON` data type. In order to
12
 * do that, both the value and its JSON encoding are kept in the object.
13
 *
14
 * The objects are immutable, i.e., operations always produce a new object.
15
 */
16
class Json
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
17
{
18
    private $value;
19
    private $encoded;
20
21
22
    /**
23
     * @param mixed $value value to be represented
24
     * @return Json
25
     */
26
    public static function fromValue($value): Json
27
    {
28
        $json = self::jsonEncode($value);
29
        return new Json($value, $json);
30
    }
31
32
    /**
33
     * @param string $json JSON-encoded value
34
     * @return Json
35
     */
36
    public static function fromEncoded(string $json): Json
37
    {
38
        $value = self::jsonDecode($json);
39
        return new Json($value, $json);
40
    }
41
42
    /**
43
     * @return Json representation of the <tt>null</tt> value; always returns the same object
44
     */
45
    public static function null(): Json
46
    {
47
        static $inst = null;
48
        if ($inst === null) {
49
            $inst = self::fromValue(null);
50
        }
51
        return $inst;
52
    }
53
54
    private function __construct($value, string $encoded)
55
    {
56
        $this->value = $value;
57
        $this->encoded = $encoded;
58
    }
59
60
    /**
61
     * @return bool|int|float|string|array|\stdClass the represented value
62
     */
63
    public function getValue()
64
    {
65
        return $this->value;
66
    }
67
68
    /**
69
     * @return string the JSON encoding of the represented value
70
     */
71
    public function getEncoded(): string
72
    {
73
        return $this->encoded;
74
    }
75
76
    private static function jsonEncode($value): string
77
    {
78
        $json = json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
79
        if ($json === false) {
80
            throw new \InvalidArgumentException(json_last_error_msg());
81
        }
82
        return $json;
83
    }
84
85
    private static function jsonDecode(string $json)
86
    {
87
        $value = json_decode($json);
88
        if ($value === null && json_last_error() != JSON_ERROR_NONE) {
89
            throw new \InvalidArgumentException(json_last_error_msg());
90
        }
91
        return $value;
92
    }
93
}
94