1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Stratadox\Json; |
5
|
|
|
|
6
|
|
|
use function array_pop; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Immutable Json Representation. |
10
|
|
|
* |
11
|
|
|
* @author Stratadox |
12
|
|
|
*/ |
13
|
|
|
final class ImmutableJson implements Json |
14
|
|
|
{ |
15
|
|
|
use JsonEncoding, JsonDecoding, PathValidation; |
16
|
|
|
|
17
|
|
|
/** @var mixed The raw Json data */ |
18
|
|
|
private $data; |
19
|
|
|
|
20
|
|
|
/** @var string The encoded Json string */ |
21
|
|
|
private $encoded; |
22
|
|
|
|
23
|
|
|
/** @throws InvalidJson */ |
24
|
|
|
private function __construct($decodedJsonData) |
25
|
|
|
{ |
26
|
|
|
$this->data = $decodedJsonData; |
27
|
|
|
$this->encoded = $this->encode($decodedJsonData); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Produces a Json object from a valid json-encoded string. |
32
|
|
|
* |
33
|
|
|
* @param string $jsonInput The json-encoded string input. |
34
|
|
|
* @return Json The Json resulting from the input string. |
35
|
|
|
* @throws InvalidJson When the input string is not correctly encoded |
36
|
|
|
* in json format. |
37
|
|
|
*/ |
38
|
|
|
public static function fromString(string $jsonInput): Json |
39
|
|
|
{ |
40
|
|
|
return new ImmutableJson(self::decode($jsonInput)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Produces a Json object from raw input data. |
45
|
|
|
* |
46
|
|
|
* @param mixed $input The "raw" input data. Any input that can be encoded |
47
|
|
|
* as json is accepted. |
48
|
|
|
* @return Json The Json resulting from the input data. |
49
|
|
|
* @throws InvalidJson When the input data cannot be encoded in json |
50
|
|
|
* format. |
51
|
|
|
*/ |
52
|
|
|
public static function fromData($input): Json |
53
|
|
|
{ |
54
|
|
|
return new ImmutableJson($input); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** @inheritdoc */ |
58
|
|
|
public function has(string ...$path): bool |
59
|
|
|
{ |
60
|
|
|
$context = $this->data; |
61
|
|
|
foreach ($path as $offset) { |
62
|
|
|
if (!$this->isValid($context, $offset)) { |
63
|
|
|
return false; |
64
|
|
|
} |
65
|
|
|
$context = $context[$offset]; |
66
|
|
|
} |
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** @inheritdoc */ |
71
|
|
|
public function retrieve(string ...$path) |
72
|
|
|
{ |
73
|
|
|
$context = $this->data; |
74
|
|
|
foreach ($path as $offset) { |
75
|
|
|
$this->mustBeValid($context, $offset, ...$path); |
76
|
|
|
$context = $context[$offset]; |
77
|
|
|
} |
78
|
|
|
return $context; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** @inheritdoc */ |
82
|
|
|
public function write($value, string ...$path): Json |
83
|
|
|
{ |
84
|
|
|
if (empty($path)) { |
85
|
|
|
return new ImmutableJson($value); |
86
|
|
|
} |
87
|
|
|
$copy = $this->data; |
88
|
|
|
$write = &$copy; |
89
|
|
|
$end = array_pop($path); |
90
|
|
|
foreach ($path as $offset) { |
91
|
|
|
$write = &$write[$offset]; |
92
|
|
|
} |
93
|
|
|
$write[$end] = $value; |
94
|
|
|
return new ImmutableJson($copy); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** @inheritdoc */ |
98
|
|
|
public function data() |
99
|
|
|
{ |
100
|
|
|
return $this->data; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** @inheritdoc */ |
104
|
|
|
public function __toString(): string |
105
|
|
|
{ |
106
|
|
|
return $this->encoded; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|