stolt/json-merge-patch

Implementation of JSON Merge Patch (https://tools.ietf.org/html/rfc7396).

v2.0.1 2022-06-15 11:27 UTC

This package is auto-updated.

Last update: 2024-12-06 15:15:42 UTC


README

Test Version PHP Version

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.