https://github.com/lindelius/php-json-patch
A zero-dependency PHP implementation of JSON Patch
https://github.com/lindelius/php-json-patch
json json-patch
Last synced: 13 days ago
JSON representation
A zero-dependency PHP implementation of JSON Patch
- Host: GitHub
- URL: https://github.com/lindelius/php-json-patch
- Owner: lindelius
- License: apache-2.0
- Created: 2021-05-23T20:30:54.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-12-18T23:45:20.000Z (about 4 years ago)
- Last Synced: 2025-11-22T01:19:54.961Z (2 months ago)
- Topics: json, json-patch
- Language: PHP
- Homepage:
- Size: 75.2 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# php-json-patch
[](https://circleci.com/gh/lindelius/php-json-patch)
[](https://coveralls.io/github/lindelius/php-json-patch?branch=master)
A zero-dependency PHP implementation of JSON Patch ([RFC 6902](https://tools.ietf.org/html/rfc6902)).
## Table of Contents
- [Requirements](#requirements)
- [Installation](#installation)
- [Usage](#usage)
- [Protected Paths](#protected-paths)
## Requirements
- PHP 7.4, or higher
## Installation
If you are using Composer, you may install the latest version of this library by running the following command from your project's root folder:
```
composer require lindelius/php-json-patch
```
You may also manually download the library by navigating to the "Releases" page and then expanding the "Assets" section
of the latest release.
## Usage
Given a set of JSON Patch operations...
```json
[
{ "op": "test", "path": "/name", "value": "Anakin Skywalker" },
{ "op": "replace", "path": "/name", "value": "Darth Vader" },
{ "op": "add", "path": "/order", "value": "Sith" },
{ "op": "move", "from": "/friends", "path": "/foes" }
]
```
And a document...
```json
{
"name": "Anakin Skywalker",
"friends": ["Obi-Wan Kenobi", "Ahsoka Tano"],
"order": "Jedi"
}
```
You can (atomically) apply the patches through one of the [`PatcherInterface`](src/PatcherInterface.php) methods...
```php
// Option 1: Provide the raw JSON string
$newDocument = $patcher->patchFromJson($documentAsArray, $operationsAsJson);
// Option 2: Provide the JSON Patch operations in array format
$newDocument = $patcher->patch($documentAsArray, $operationsAsArray);
```
And get a new document back :)
```json
{
"name": "Darth Vader",
"foes": ["Obi-Wan Kenobi", "Ahsoka Tano"],
"order": "Sith"
}
```
### Protected Paths
This library has built-in support for registering "protected paths", which are paths that may not be modified by any patch operation. Protected paths will indirectly also block modifications to their parent path(s) and any child paths.
```php
$patcher->addProtectedPath("/id");
$patcher->addProtectedPath("/created_at");
$patcher->addProtectedPath("/some/nested/path");
```
Please note that "test" operations can still operate on a protected path since they are not actually modifying the document.