|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Stratadox\Json; |
|
5
|
|
|
|
|
6
|
|
|
use function array_pop; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Mutable Json Representation. |
|
10
|
|
|
* |
|
11
|
|
|
* @author Stratadox |
|
12
|
|
|
*/ |
|
13
|
|
|
final class MutableJson 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 object 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 MutableJson(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 MutableJson($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
|
|
|
$this->data = $value; |
|
86
|
|
|
$this->encoded = $this->encode($this->data); |
|
87
|
|
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
$data = &$this->data; |
|
90
|
|
|
$end = array_pop($path); |
|
91
|
|
|
foreach ($path as $offset) { |
|
92
|
|
|
$data = &$data[$offset]; |
|
93
|
|
|
} |
|
94
|
|
|
$data[$end] = $value; |
|
95
|
|
|
$this->encoded = $this->encode($this->data); |
|
96
|
|
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** @inheritdoc */ |
|
100
|
|
|
public function data() |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->data; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** @inheritdoc */ |
|
106
|
|
|
public function __toString(): string |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->encoded; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|