Giter VIP home page Giter VIP logo

php-jsonpatch's Introduction

JSON Patch for PHP

Build Status Version PHP Version

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 just 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.

php-jsonpatch's People

Contributors

deltachaos avatar greydnls avatar hannesvdvreken avatar mazubieta avatar megazoll avatar narcoticfresh avatar phatj avatar raphaelstolt avatar smoskalenko avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.