PHP code example of bumpcore / json-patch
1. Go to this page and download the library: Download bumpcore/json-patch library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bumpcore / json-patch example snippets
use BumpCore\JsonPatch\JsonPatch;
$document = ['foo' => ['bar', 'baz']];
$result = JsonPatch::apply($document, [
['op' => 'add', 'path' => '/foo/1', 'value' => 'qux'],
]);
// ['foo' => ['bar', 'qux', 'baz']]
use BumpCore\JsonPatch\JsonPatch;
$json = JsonPatch::applyJson(
'{"foo":["bar","baz"]}',
'[{"op":"add","path":"/foo/1","value":"qux"}]',
);
// {"foo":["bar","qux","baz"]}
use BumpCore\JsonPatch\JsonPatch;
$document = [
'title' => 'Goodbye!',
'tags' => ['example', 'sample'],
];
$patched = JsonPatch::apply($document, [
['op' => 'replace', 'path' => '/title', 'value' => 'Hello!'],
['op' => 'remove', 'path' => '/tags/1'],
['op' => 'add', 'path' => '/published', 'value' => true],
]);
// [
// 'title' => 'Hello!',
// 'tags' => ['example'],
// 'published' => true,
// ]
use BumpCore\JsonPatch\JsonPatch;
$source = ['name' => 'old', 'tags' => ['stable']];
$target = ['name' => 'new', 'tags' => ['stable', 'fast']];
$patch = JsonPatch::diff($source, $target);
// [
// ['op' => 'replace', 'path' => '/name', 'value' => 'new'],
// ['op' => 'add', 'path' => '/tags/-', 'value' => 'fast'],
// ]
use BumpCore\JsonPatch\JsonPatch;
$patchJson = JsonPatch::diffJson(
'{"name":"old"}',
'{"name":"new","enabled":true}',
);
// [{"op":"add","path":"/enabled","value":true},{"op":"replace","path":"/name","value":"new"}]
JsonPatch::MEDIA_TYPE; // application/json-patch+json
use BumpCore\JsonPatch\JsonMergePatch;
$document = [
'title' => 'Goodbye!',
'author' => [
'givenName' => 'John',
'familyName' => 'Doe',
],
'tags' => ['example', 'sample'],
];
$patched = JsonMergePatch::apply($document, [
'title' => 'Hello!',
'author' => [
'familyName' => null,
],
'tags' => ['example'],
]);
// [
// 'title' => 'Hello!',
// 'author' => ['givenName' => 'John'],
// 'tags' => ['example'],
// ]
use BumpCore\JsonPatch\JsonMergePatch;
$json = JsonMergePatch::applyJson(
'{"a":"b","c":{"d":"e","f":"g"}}',
'{"a":"z","c":{"f":null}}',
);
// {"a":"z","c":{"d":"e"}}
JsonMergePatch::MEDIA_TYPE; // application/merge-patch+json
use BumpCore\JsonPatch\JsonPointer;
$pointer = JsonPointer::fromString('/foo/~01');
$pointer->tokens(); // ['foo', '~1']
JsonPointer::escapeToken('a/b~c'); // a~1b~0c
JsonPointer::unescapeToken('a~1b~0c'); // a/b~c
use BumpCore\JsonPatch\JsonPointer;
$document = ['items' => [['name' => 'Old']]];
JsonPointer::get($document, '/items/0/name'); // Old
JsonPointer::has($document, '/items/0/name'); // true
$updated = JsonPointer::set($document, '/items/0/name', 'New');
$removed = JsonPointer::remove($updated, '/items/0');
use BumpCore\JsonPatch\JsonPatch;
$json = JsonPatch::applyJson(
'{"child":{}}',
'[{"op":"add","path":"/child/0","value":"kept as an object member"}]',
);
// {"child":{"0":"kept as an object member"}}
BumpCore\JsonPatch\Exception\JsonPatchException
$exception->operationIndex(); // 0
$exception->operation(); // remove
$exception->path(); // /items/0
$exception->from(); // /source for move/copy failures
bash
composer analyse