php-jsonpatch / php-jsonpatch
Implementation of JSON Patch (http://tools.ietf.org/html/rfc6902)
Installs: 816 162
Dependents: 5
Suggesters: 0
Security: 0
Stars: 91
Watchers: 7
Forks: 24
Open Issues: 7
Requires
- php: >=7.4
- php-jsonpointer/php-jsonpointer: ^4.0
- symfony/polyfill-mbstring: ^1.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- phpunit/phpunit: 8.*
README
This is an implementation of JSON Patch written in PHP.
Installation via Composer
composer require php-jsonpatch/php-jsonpatch
Usage
Now you can use JSON Patch for PHP via the available Composer autoload file.
Patch operations are defined in JSON and bundled in an array. Available JSON Patch
operations are add
, remove
, replace
, move
, copy
,
and test
; if their mandatory properties are not set a Rs\Json\Patch\InvalidOperationException
will be
thrown.
If necessary you can disable some patch operations by setting a whitelist bitmask in the constructor e.g. $patch = new Patch($targetDocument, $patchDocument, Add::APPLY | Copy::APPLY | Replace::APPLY | Remove::APPLY);
. This will not allow the usage of the move
and test
patch operation from the patch document. If used these operations are ignored. The default is to allow all patch operations.
<?php require_once 'vendor/autoload.php'; use Rs\Json\Patch; use Rs\Json\Patch\InvalidPatchDocumentJsonException; use Rs\Json\Patch\InvalidTargetDocumentJsonException; use Rs\Json\Patch\InvalidOperationException; try { $targetDocument = '{"a":{"b":["c","d","e"]}}'; $patchDocument = '[ {"op":"add", "path":"/a/d", "value":["a","b"]}, // {"a":{"b":["c","d","e"],"d":["a","b"]}} {"op":"test", "path":"/a/d/-", "value":"b"}, // previous target document {"op":"remove", "path":"/a/d/-"}, // {"a":{"b":["c","d","e"],"d":["a"]}} {"op":"test", "path":"/a/d/-", "value":"a"}, // previous target document {"op":"add", "path":"/a/d/-", "value":"b"}, // {"a":{"b":["c","d","e"],"d":["a","b"]}} {"op":"test", "path":"/a/d", "value":["a","b"]}, // previous target document {"op":"move", "path":"/a/c", "from":"/a/d"}, // {"a":{"b":["c","d","e"],"c":["a","b"]}} {"op":"test", "path":"/a/c", "value":["a","b"]}, // previous target document {"op":"copy", "path":"/a/e", "from":"/a/c"}, // {"a":{"b":["c","d","e"],"c":["a","b"],"e":["a","b"]}} {"op":"test", "path":"/a/e", "value":["a","b"]}, // previous target document {"op":"replace", "path":"/a/e", "value":["a"]}, // {"a":{"b":["c","d","e"],"c":["a","b"],"e":["a"]}} {"op":"test", "path":"/a/e", "value":["a"]} // previous target document ]'; $patch = new Patch($targetDocument, $patchDocument); $patchedDocument = $patch->apply(); // {"a":{"b":["c","d","e"],"c":["a","b"],"e":["a"]}} } catch (InvalidPatchDocumentJsonException $e) { // Will be thrown when using invalid JSON in a patch document } catch (InvalidTargetDocumentJsonException $e) { // Will be thrown when using invalid JSON in a target document } catch (InvalidOperationException $e) { // Will be thrown when using an invalid JSON Pointer operation (i.e. missing property) }
For some more usage examples of JSON Patch operations have a look at the
integration tests located under tests/integration/*
.
Running tests
composer test
License
This library is licensed under the MIT License. Please see LICENSE for more information.
Changelog
Please see CHANGELOG for more information.
Contributing
Please see CONTRIBUTING for more information.