-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-patch.php
More file actions
21 lines (17 loc) · 902 Bytes
/
Copy pathbasic-patch.php
File metadata and controls
21 lines (17 loc) · 902 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
declare(strict_types=1);
require dirname(__DIR__) . '/vendor/autoload.php';
use Phore\JsonPatch\{JsonPatch, JsonPatchApplier, JsonValue};
$original = JsonValue::decode('{"title":"Draft","tags":["php"],"settings":{"a/b":false}}');
$patch = JsonPatch::fromArray([
['op'=>'test', 'path'=>'/title', 'value'=>'Draft'],
['op'=>'replace', 'path'=>'/title', 'value'=>'Published'],
['op'=>'add', 'path'=>'/tags/-', 'value'=>'json-patch'],
['op'=>'replace', 'path'=>'/settings/a~1b', 'value'=>true],
]);
$result = (new JsonPatchApplier())->apply($original, $patch);
$expected = JsonValue::decode('{"title":"Published","tags":["php","json-patch"],"settings":{"a/b":true}}');
if (!JsonValue::equal($expected, $result->value) || $original->title !== 'Draft') {
throw new RuntimeException('Unexpected patch result or mutated input.');
}
echo JsonValue::encode($result->value), PHP_EOL;