https://github.com/remorhaz/php-json-patch
JSON Patch (RFC-6902) PHP implementation
https://github.com/remorhaz/php-json-patch
json jsonpatch php rfc-6902
Last synced: about 1 month ago
JSON representation
JSON Patch (RFC-6902) PHP implementation
- Host: GitHub
- URL: https://github.com/remorhaz/php-json-patch
- Owner: remorhaz
- License: mit
- Created: 2016-11-08T22:56:22.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-02-21T21:33:07.000Z (about 1 year ago)
- Last Synced: 2025-03-26T10:01:28.258Z (about 2 months ago)
- Topics: json, jsonpatch, php, rfc-6902
- Language: PHP
- Size: 78.1 KB
- Stars: 5
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# PHP JSON Patch
[](https://packagist.org/packages/remorhaz/php-json-patch)
[](https://github.com/remorhaz/php-json-patch/actions/workflows/build.yml)
[](https://scrutinizer-ci.com/g/remorhaz/php-json-patch/?branch=master)
[](https://codecov.io/gh/remorhaz/php-json-patch)
[](https://dashboard.stryker-mutator.io/reports/github.com/remorhaz/php-json-patch/master)[](https://packagist.org/packages/remorhaz/php-json-patch)
[](https://packagist.org/packages/remorhaz/php-json-patch)This library implements [RFC6902](https://tools.ietf.org/html/rfc6902)-compliant JSON patch tool.
## Requirements
- PHP 8.1.
- [JSON extension](https://www.php.net/manual/en/book.json.php) (ext-json) - required by [remorhaz/php-json-data](https://github.com/remorhaz/php-json-data) to access JSON documents.
- [Internationalization functions](https://www.php.net/manual/en/book.intl.php) (ext-intl) - required by [`remorhaz/php-json-data`](https://github.com/remorhaz/php-json-data) to compare Unicode strings.## Installation
You will need [composer](https://getcomposer.org) to perform install.
```
composer require remorhaz/php-json-patch
```## Documentation
### Accessing JSON document
You can create accessible JSON document either from encoded JSON string or from decoded JSON data using corresponding _node value factory_:
```php
use Remorhaz\JSON\Data\Value\EncodedJson;
use Remorhaz\JSON\Data\Value\DecodedJson;// Creating document from JSON-encoded string:
$encodedValueFactory = EncodedJson\NodeValueFactory::create();
$encodedJson = '{"a":1}';
$document1 = $encodedValueFactory->createValue($encodedJson);// Creating document from decoded JSON data:
$decodedValueFactory = DecodedJson\NodeValueFactory::create();
$decodedJson = (object) ['a' => 1];
$document2 = $decodedValueFactory->createValue($decodedJson);
```### Creating and processing query
You should use _query factory_ to create query from JSON Patch document. Then you should use _processor_ to apply that query:
```php
createValue('[{"op":"remove","path":"/0"}]');
$query = $queryFactory->createQuery($patch);$document = $encodedValueFactory->createValue('[1,2]');
$result = $processor->apply($query, $document);var_dump($result->encode()); // string: '[2]'
var_dump($result->decode()); // array: [2]
```
Note that result can be exported either to JSON-encoded string or to raw PHP value.## License
PHP JSON Patch is licensed under [MIT license](./LICENSE).