Skip to content

Commit

Permalink
FastJsonPatch is a class to handle JSON Patch operations as per RFC 6902
Browse files Browse the repository at this point in the history
  • Loading branch information
blancks committed Aug 21, 2024
0 parents commit 761f25a
Show file tree
Hide file tree
Showing 19 changed files with 1,333 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.idea
vendor
.phpunit.result.cache
php_errors.log
composer.lock
9 changes: 9 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2024 - Salvatore Rotondo

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
97 changes: 97 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
PHP Fast JSON Patch
=====================

FastJsonPatch is designed to handle JSON Patch operations in accordance with the [RFC 6902](http://tools.ietf.org/html/rfc6902) specification.

JSON Patch is a format for expressing a sequence of operations to be applied to a JSON document. This class provides methods to parse, validate, and apply these operations, allowing you to modify JSON objects or arrays programmatically.


## Installation via Composer

``` bash
composer require blancks/fast-jsonpatch-php
```

## Key features

1. **Apply JSON Patch Operations:**
- The class can apply a series of JSON Patch operations to a target JSON document.
- The operations are performed sequentially, modifying the document as specified in the patch.


2. **Operation Types:**
- **add**: Adds a value to a specific location in the JSON document.
- **copy**: Copies a value from one location to another within the JSON document.
- **move**: Moves a value from one location to another within the JSON document.
- **remove**: Removes a value from a specific location in the JSON document.
- **replace**: Replaces the value at a specific location with a new value.
- **test**: Tests whether a specified value is present at a specific location in the JSON document.


3. **Path Parsing:**
- The class uses JSON Pointer ([RFC 6901](https://datatracker.ietf.org/doc/html/rfc6901)) notation to identify locations within the JSON document. It correctly handles the path syntax, including edge cases such as escaping special characters.


4. **Validation:**
- The class ensures that the provided patch document conforms to the JSON Patch specification, validating the structure and types of operations before applying them.


5. **Performance:**
- The class is optimized for performance, ensuring that operations are applied efficiently even on large JSON documents.


6. **Tests:**
- Extensive unit testing ensures that everything is robust and works as intended.

## Basic Usage

``` php
<?php require_once 'vendor/autoload.php';

use blancks\JsonPatch\FastJsonPatch;

$json = '{"name": "John", "age": 30}';
$patch = '[
{"op": "replace", "path": "/name", "value": "Jane"},
{"op": "add", "path": "/email", "value": "[email protected]"}
]';

echo FastJsonPatch::apply($json, $patch);
// Output: {"name": "Jane", "age": 30, "email": "[email protected]"}
```

## Methods Overview

- `apply(string $json, string $patch): string` Applies the $patch operations to the provided $json document and returns the updated json document string.


- `applyDecoded(string $json, string $patch): mixed` Same as **apply** but returns instead the decoded document instead of a json string


- `applyByReference(array|\stdClass &$document, array $patch): void` References your in-memory representation of the document and applies the patch in place.


- `parsePath(string $json, string $pointer): mixed` Returns the value located by the given $pointer from the $json string document


- `parsePathByReference(array|\stdClass &$document, string $pointer): mixed` Same as **parsePath** but finds the location from your in-memory document


- `validatePatch(string $patch): void` Checks if the provided $patch is structurally valid

## Dependencies

- PHP >= 8.1
- JSON extension enabled in PHP

## Running tests

``` bash
composer test
```

Test cases comes from [json-patch/json-patch-tests](https://github.com/json-patch/json-patch-tests) and extended furthermore.

## License

This software is licensed under the [MIT License](LICENSE.md).
33 changes: 33 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "blancks/fast-jsonpatch-php",
"description": "PHP implementation of JSON Patch that runs fast with optimized memory usage",
"keywords": ["php","json", "json patch"],
"homepage": "https://github.com/blancks/fast-jsonpatch-php",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Salvatore Rotondo",
"email": "[email protected]"
}
],
"support": {
"issues": "https://github.com/blancks/fast-jsonpatch-php/issues"
},
"autoload": {
"psr-4": {
"blancks\\JsonPatch\\": "src"
}
},
"require": {
"php": ">=8.1"
},
"require-dev": {
"phpunit/phpunit": "^11",
"phpstan/phpstan": "^1.11"
},
"scripts": {
"test": "phpunit --no-configuration --test-suffix FastJsonPatchTest.php tests/ --colors=always",
"static-analyse": "phpstan analyse --configuration phpstan.neon.dist"
}
}
11 changes: 11 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
parameters:
level: max
excludePaths:
- test
paths:
- src
reportUnmatchedIgnoredErrors: false
ignoreErrors:
- '#recursiveKeySort#'
- '#array_splice#'
- '#Cannot access offset string on array#'
11 changes: 11 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="FastJsonPatchTest" type="PHPUnitRunConfigurationType" factoryName="PHPUnit">
<CommandLine>
<PhpTestInterpreterSettings>
<option name="interpreterName" value="PHP 8.2.2" />
</PhpTestInterpreterSettings>
</CommandLine>
<TestRunner class="FastJsonPatchTest" file="$PROJECT_DIR$/test/FastJsonPatchTest.php" scope="Class" />
<method v="2" />
</configuration>
</component>
Loading

0 comments on commit 761f25a

Please sign in to comment.