remorhaz / php-json-patch
JSON Patch (RFC-6902) PHP implementation
Installs: 59 037
Dependents: 0
Suggesters: 1
Security: 0
Stars: 4
Watchers: 3
Forks: 1
Open Issues: 0
Requires
- php: ~8.1.0 || ~8.2.0 || ~8.3.0
- ext-intl: *
- remorhaz/php-json-data: ^0.7
- remorhaz/php-json-pointer: ^0.7.1
Requires (Dev)
- bamarni/composer-bin-plugin: ^1.8.2
- phpunit/phpunit: ^10.1 || ^11
README
This library implements RFC6902-compliant JSON patch tool.
Requirements
- PHP 8.1.
- JSON extension (ext-json) - required by remorhaz/php-json-data to access JSON documents.
- Internationalization functions (ext-intl) - required by
remorhaz/php-json-data
to compare Unicode strings.
Installation
You will need composer 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:
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 use Remorhaz\JSON\Data\Value\EncodedJson; use Remorhaz\JSON\Patch\Processor\Processor; use Remorhaz\JSON\Patch\Query\QueryFactory; $encodedValueFactory = EncodedJson\NodeValueFactory::create(); $queryFactory = QueryFactory::create(); $processor = Processor::create(); $patch = $encodedValueFactory->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.