GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - raphaelstolt/php-jsonpatch: JSON Patch implementation for PHP

BranchesTags
Open more actions menu

Latest commit

 

History

147 Commits
147 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
src/Rs/Json
src/Rs/Json
 
 
tests
tests
 
 
.editorconfig
.editorconfig
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
.php-cs-fixer.php
.php-cs-fixer.php
 
 
CHANGELOG.md
CHANGELOG.md
 
 
CONTRIBUTING.md
CONTRIBUTING.md
 
 
LICENSE.md
LICENSE.md
 
 
README.md
README.md
 
 
composer.json
composer.json
 
 
phpstan.neon.dist
phpstan.neon.dist
 
 
phpunit.xml.dist
phpunit.xml.dist
 
 

Repository files navigation

JSON Patch for PHP

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

About

JSON Patch implementation for PHP

Resources

Readme
MIT license

Contributing

Contributing
Activity

Stars

99 stars

Watchers

5 watching

Forks

24 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.