stolt / json-merge-patch
Implementation of JSON Merge Patch (https://tools.ietf.org/html/rfc7396).
Installs: 160 635
Dependents: 4
Suggesters: 0
Security: 0
Stars: 10
Watchers: 4
Forks: 2
Open Issues: 0
Requires
- php: >=7.4
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.19
- phpunit/phpunit: ^8.5
README
This is an(other) implementation of JSON Merge Patch written in PHP. For a PHP 5.3 compatible version please use the implementation by @clue.
Installation via Composer
composer require stolt/json-merge-patch
Usage
Now you can use JSON Merge Patch for PHP via the available Composer autoload file.
Apply a patch
<?php require_once 'vendor/autoload.php'; use Rs\Json\Merge\Patch; $targetDocument = json_decode('{"title":"Goodbye!","author":{"givenName":"John","familyName":"Doe"},"tags":["example","sample"],"content":"This will be unchanged"}'); $patchDocument = json_decode('{"title":"Hello!","phoneNumber":"+01-123-456-7890","author":{"familyName":null},"tags":["example"]}'); $patchedDocument = (new Patch())->apply( $targetDocument, $patchDocument ); // '{"title":"Hello!","author":{"givenName":"John"},"tags":["example"],"content":"This will be unchanged","phoneNumber":"+01-123-456-7890"}'
Generate a patch document
<?php require_once 'vendor/autoload.php'; use Rs\Json\Merge\Patch; $sourceDocument = json_decode('{"a":"b","b":"c"}'); $targetDocument = json_decode('{"b":"c"}'); $generatedPatchDocument = (new Patch())->generate( $sourceDocument, $targetDocument ); // '{"a":null}'
Merge patch documents
<?php require_once 'vendor/autoload.php'; use Rs\Json\Merge\Patch; $patchDocument1 = json_decode('{"a":"b"}'); $patchDocument2 = json_decode('{"b":"c"}'); $mergedPatchDocument = (new Patch())->merge( $patchDocument1, $patchDocument2 ); // '{"a":"b","b":"c"}'
Running tests
composer test
License
This library is licensed under the MIT license. Please see LICENSE for more details.
Changelog
Please see CHANGELOG for more details.
Contributing
Please see CONTRIBUTING for more details.