forked from swaggest/json-diff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonPatch.php
217 lines (200 loc) · 7.38 KB
/
JsonPatch.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
namespace Swaggest\JsonDiff;
use Swaggest\JsonDiff\JsonPatch\Add;
use Swaggest\JsonDiff\JsonPatch\Copy;
use Swaggest\JsonDiff\JsonPatch\Move;
use Swaggest\JsonDiff\JsonPatch\OpPath;
use Swaggest\JsonDiff\JsonPatch\OpPathFrom;
use Swaggest\JsonDiff\JsonPatch\OpPathValue;
use Swaggest\JsonDiff\JsonPatch\Remove;
use Swaggest\JsonDiff\JsonPatch\Replace;
use Swaggest\JsonDiff\JsonPatch\Test;
/**
* JSON Patch is specified in [RFC 6902](http://tools.ietf.org/html/rfc6902) from the IETF.
*
* Class JsonPatch
*/
class JsonPatch implements \JsonSerializable
{
/**
* Disallow converting empty array to object for key creation
* @see JsonPointer::STRICT_MODE
*/
const STRICT_MODE = 2;
/**
* Allow associative arrays to mimic JSON objects (not recommended)
*/
const TOLERATE_ASSOCIATIVE_ARRAYS = 8;
private $flags = 0;
/**
* @param int $options
* @return $this
*/
public function setFlags($options)
{
$this->flags = $options;
return $this;
}
/** @var OpPath[] */
private $operations = array();
/**
* @param array $data
* @return JsonPatch
* @throws Exception
*/
public static function import(array $data)
{
$result = new JsonPatch();
foreach ($data as $operation) {
/** @var OpPath|OpPathValue|OpPathFrom|array $operation */
if (is_array($operation)) {
$operation = (object)$operation;
}
if (!is_object($operation)) {
throw new Exception('Invalid patch operation - should be a JSON object');
}
if (!isset($operation->op)) {
throw new MissingFieldException('op', $operation);
}
if (!isset($operation->path)) {
throw new MissingFieldException('path', $operation);
}
if (!is_string($operation->op)) {
throw new InvalidFieldTypeException('op', 'string', $operation);
}
if (!is_string($operation->path)) {
throw new InvalidFieldTypeException('path', 'string', $operation);
}
$op = null;
switch ($operation->op) {
case Add::OP:
$op = new Add();
break;
case Copy::OP:
$op = new Copy();
break;
case Move::OP:
$op = new Move();
break;
case Remove::OP:
$op = new Remove();
break;
case Replace::OP:
$op = new Replace();
break;
case Test::OP:
$op = new Test();
break;
default:
throw new UnknownOperationException($operation);
}
$op->path = $operation->path;
if ($op instanceof OpPathValue) {
if (property_exists($operation, 'value')) {
$op->value = $operation->value;
} else {
throw new MissingFieldException('value', $operation);
}
} elseif ($op instanceof OpPathFrom) {
if (!isset($operation->from)) {
throw new MissingFieldException('from', $operation);
} elseif (!is_string($operation->from)) {
throw new InvalidFieldTypeException('from', 'string', $operation);
}
$op->from = $operation->from;
}
$result->operations[] = $op;
}
return $result;
}
public static function export(JsonPatch $patch)
{
$result = array();
foreach ($patch->operations as $operation) {
$result[] = (object)(array)$operation;
}
return $result;
}
public function op(OpPath $op)
{
$this->operations[] = $op;
return $this;
}
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
return self::export($this);
}
/**
* @param mixed $original
* @param bool $stopOnError
* @return Exception[] array of errors
* @throws Exception
*/
public function apply(&$original, $stopOnError = true)
{
$errors = array();
foreach ($this->operations as $operation) {
try {
// track the current pointer field so we can use it for a potential PathException
$pointerField = 'path';
$pathItems = JsonPointer::splitPath($operation->path);
switch (true) {
case $operation instanceof Add:
JsonPointer::add($original, $pathItems, $operation->value, $this->flags);
break;
case $operation instanceof Copy:
$pointerField = 'from';
$fromItems = JsonPointer::splitPath($operation->from);
$value = JsonPointer::get($original, $fromItems);
$pointerField = 'path';
JsonPointer::add($original, $pathItems, $value, $this->flags);
break;
case $operation instanceof Move:
$pointerField = 'from';
$fromItems = JsonPointer::splitPath($operation->from);
$value = JsonPointer::get($original, $fromItems);
JsonPointer::remove($original, $fromItems, $this->flags);
$pointerField = 'path';
JsonPointer::add($original, $pathItems, $value, $this->flags);
break;
case $operation instanceof Remove:
JsonPointer::remove($original, $pathItems, $this->flags);
break;
case $operation instanceof Replace:
JsonPointer::get($original, $pathItems);
JsonPointer::remove($original, $pathItems, $this->flags);
JsonPointer::add($original, $pathItems, $operation->value, $this->flags);
break;
case $operation instanceof Test:
$value = JsonPointer::get($original, $pathItems);
$diff = new JsonDiff($operation->value, $value,
JsonDiff::STOP_ON_DIFF);
if ($diff->getDiffCnt() !== 0) {
throw new PatchTestOperationFailedException($operation, $value);
}
break;
}
} catch (JsonPointerException $jsonPointerException) {
$pathException = new PathException(
$jsonPointerException->getMessage(),
$operation,
$pointerField,
$jsonPointerException->getCode()
);
if ($stopOnError) {
throw $pathException;
} else {
$errors[] = $pathException;
}
} catch (Exception $exception) {
if ($stopOnError) {
throw $exception;
} else {
$errors[] = $exception;
}
}
}
return $errors;
}
}