55[ ![ Scrutinizer Code Quality] ( https://scrutinizer-ci.com/g/remorhaz/php-json-patch/badges/quality-score.png?b=master )] ( https://scrutinizer-ci.com/g/remorhaz/php-json-patch/?branch=master )
66[ ![ codecov] ( https://codecov.io/gh/remorhaz/php-json-patch/branch/master/graph/badge.svg )] ( https://codecov.io/gh/remorhaz/php-json-patch )
77[ ![ Infection MSI] ( https://badge.stryker-mutator.io/github.com/remorhaz/php-json-patch/master )] ( https://infection.github.io )
8+ [ ![ Total Downloads] ( https://poser.pugx.org/remorhaz/php-json-patch/downloads )] ( https://packagist.org/packages/remorhaz/php-json-patch )
89[ ![ License] ( https://poser.pugx.org/remorhaz/php-json-patch/license )] ( https://packagist.org/packages/remorhaz/php-json-patch )
910
1011This library implements [ RFC6902] ( https://tools.ietf.org/html/rfc6902 ) -compliant JSON patch tool.
1112
1213## Requirements
1314- PHP 7.3+
15+ - [ 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.
1416- [ 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.
1517
1618# Installation
@@ -20,43 +22,45 @@ composer require remorhaz/php-json-patch
2022```
2123
2224# Documentation
23- ## Data accessors
24- Patch tool utilizes JSON data accessor interfaces defined in package
25- ** [ remorhaz/php-json-data] ( https://github.com/remorhaz/php-json-data ) ** . Read more about them in package documentation.
26- There is a ready-to-work implementation in that package that works with native PHP structures (like the ones you get as
27- a result of ` json_decode ` function). You can use ` Remorhaz\JSON\Data\Reference\Selector ` class to bind to patch data and
28- ` Remorhaz\JSON\Data\Reference\Writer ` class to bind to the document that is to be patched. You can also implement your own accessors
29- if you need to work with another sort of data (like unparsed JSON text, for example).
30-
31- ## Using patch tool
32- To apply JSON Patch to the JSON document you need just 4 simple steps:
33-
34- 1 . Create an instance of read-only accessor bound to your patch data.
35- 2 . Create an instance of writabe accessor bound to your document.
36- 3 . Create an object of ` \Remorhaz\JSON\Patch\Patch ` by calling it's constructor with a document accessor as an argument.
37- 4 . Call its ` apply() ` method with patch accessor as an argument.
38-
39- ## Example of usage
25+ ### Accessing JSON document
26+ You can create accessible JSON document either from encoded JSON string or from decoded JSON data using corresponding _ node value factory_ :
4027``` php
41- <?php
28+ use Remorhaz\JSON\Data\Value\EncodedJson;
29+ use Remorhaz\JSON\Data\Value\DecodedJson;
30+
31+ // Creating document from JSON-encoded string:
32+ $encodedValueFactory = EncodedJson\NodeValueFactory::create();
33+ $encodedJson = '{"a":1}';
34+ $document1 = $encodedValueFactory->createValue($encodedJson);
35+
36+ // Creating document from decoded JSON data:
37+ $decodedValueFactory = DecodedJson\NodeValueFactory::create();
38+ $decodedJson = (object) ['a' => 1];
39+ $document2 = $decodedValueFactory->createValue($decodedJson);
40+ ```
4241
43- use Remorhaz\JSON\Data\Reference\Selector;
44- use Remorhaz\JSON\Data\Reference\Writer;
42+ ## Creating and processing query
43+ You should use _ query factory_ to create query from JSON Patch document. Then you should use _ processor_ to apply that query:
44+ ``` php
45+ <?php
46+ use Remorhaz\JSON\Data\Value\EncodedJson;
4547use Remorhaz\JSON\Patch\Processor\Processor;
48+ use Remorhaz\JSON\Patch\Query\QueryFactory;
49+
50+ $encodedValueFactory = EncodedJson\NodeValueFactory::create();
51+ $queryFactory = QueryFactory::create();
52+ $processor = Processor::create();
4653
47- // Setting up document.
48- $data = (object) ['a' => (object) ['b' => 'c', 'd' => 'e']];
49- $dataWriter = new Writer($data);
54+ $patch = $encodedValueFactory->createValue('[{"op":"remove","path":"/0"}]');
55+ $query = $queryFactory->createQuery($patch);
5056
51- // Setting up patch.
52- $patchData = [
53- (object) ['op' => 'add', 'path' => '/a/f', 'value' => 'g'],
54- ];
55- $patchSelector = new Selector($patchData);
57+ $document = $encodedValueFactory->createValue('[1,2]');
58+ $result = $processor->apply($query, $document);
5659
57- // Applying the patch.
58- (new Processor($dataWriter))->apply($patchSelector) ; // $data->a->f property is added and set to 'g'
60+ var_dump($result->encode()); // string: '[2]'
61+ var_dump($result->decode()) ; // array: [2]
5962```
63+ Note that result can be exported either to JSON-encoded string or to raw PHP value.
6064
6165# License
62- PHP JSON Patch is licensed under MIT license.
66+ PHP JSON Patch is licensed under [ MIT license] ( ./LICENSE ) .
0 commit comments