Skip to content

Commit f431de7

Browse files
committed
Updated method overview, contribution informations and improved usage example in README.md
1 parent 04cf380 commit f431de7

File tree

1 file changed

+33
-59
lines changed

1 file changed

+33
-59
lines changed

README.md

Lines changed: 33 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,33 @@ Below is an example of how to use the `FastJsonPatch` class to apply a patch to
3131

3232
```php
3333
use blancks\JsonPatch\FastJsonPatch;
34+
use blancks\JsonPatch\exceptions\FastJsonPatchException;
3435

3536
$document = '{
36-
"addressbook":[
37+
"contacts":[
3738
{"name":"John","number":"-"},
3839
{"name":"Dave","number":"+1 222 333 4444"}
3940
]
4041
}';
4142

4243
$patch = '[
43-
{"op":"add","path":"/addressbook/-","value":{"name":"Jane", "number":"+1 353 644 2121"}},
44-
{"op":"replace","path":"/addressbook/0/number","value":"+1 212 555 1212"},
45-
{"op":"remove","path":"/addressbook/1"}
44+
{"op":"add","path":"/contacts/-","value":{"name":"Jane", "number":"+1 353 644 2121"}},
45+
{"op":"replace","path":"/contacts/0/number","value":"+1 212 555 1212"},
46+
{"op":"remove","path":"/contacts/1"}
4647
]';
4748

4849
$FastJsonPatch = FastJsonPatch::fromJson($document);
49-
$FastJsonPatch->apply($patch);
50+
51+
try {
52+
53+
$FastJsonPatch->apply($patch);
54+
55+
} catch (FastJsonPatchException $e) {
56+
57+
// here if patch cannot be applied for some reason
58+
echo $e->getMessage(), "\n";
59+
60+
}
5061

5162
var_dump($FastJsonPatch->getDocument());
5263
```
@@ -55,7 +66,7 @@ var_dump($FastJsonPatch->getDocument());
5566

5667
```txt
5768
object(stdClass) (1) {
58-
["addressbook"]=>
69+
["contacts"]=>
5970
array(2) {
6071
[0]=>
6172
object(stdClass) (2) {
@@ -77,67 +88,21 @@ object(stdClass) (1) {
7788

7889
The expected workflow is that once you got a `FastJsonPatch` instance you can call the `apply` method each time a new patch is received and this is particularly handy in long-running context like a websocket client/server.
7990

80-
81-
### Error Handling Example
82-
83-
All exceptions implement the interface `blancks\JsonPatch\exceptions\FastJsonPatchException` and so catching an error is pretty straightforward:
84-
85-
```php
86-
use blancks\JsonPatch\FastJsonPatch;
87-
use blancks\JsonPatch\exceptions\FastJsonPatchException;
88-
89-
$document = '{"addressbook":[{"name":"John","number":"-"}]}';
90-
91-
// Trying to remove an item that not exists will make the patch application to fail
92-
$patch = '[
93-
{"op":"replace","path":"/addressbook/0/number","value":"+1 212 555 1212"},
94-
{"op":"remove","path":"/addressbook/5"}
95-
]';
96-
97-
$FastJsonPatch = FastJsonPatch::fromJson($document);
98-
99-
try {
100-
$FastJsonPatch->apply($patch);
101-
} catch (FastJsonPatchException $e) {
102-
// something wrong while applying the patch
103-
echo $e->getMessage(), "\n";
104-
}
105-
106-
var_dump($FastJsonPatch->getDocument());
107-
```
108-
109-
**Expected Output:**
110-
111-
```txt
112-
Unknown document path "/addressbook/5"
113-
object(stdClass) (1) {
114-
["addressbook"]=>
115-
array(1) {
116-
[0]=>
117-
object(stdClass)#10 (2) {
118-
["name"]=>
119-
string(4) "John"
120-
["number"]=>
121-
string(1) "-"
122-
}
123-
}
124-
}
125-
```
126-
127-
Patch application is designed to be atomic. If any operation of a given patch fails the original document is restored.\
128-
In this example you can see that the number of "John" has not changed.
91+
Patch application is designed to be atomic. If any operation of a given patch fails the original document is restored, ensuring a consistent state of the document.
12992

13093

13194
## Constructor
13295

133-
#### `__construct(mixed &$document, ?JsonHandlerInterface $JsonHandler = null)`
96+
#### `__construct(mixed &$document, ?JsonHandlerInterface $JsonHandler = null, ?JsonPointerHandlerInterface $JsonPointerHandler = null)`
13497

13598
- **Description**: Initializes a new instance of the `FastJsonPatch` class.
13699
- **Parameters**:
137100
- `mixed &$document`: The decoded JSON document.
138101
- `?JsonHandlerInterface $JsonHandler`: An instance of the JSON handler which will be responsible for encoding/decoding and CRUD operations.\
139102
The default handler is `blancks\JsonPatch\json\handlers\BasicJsonHandler` and decodes json objects as php \stdClass instances. This is the recommended way.\
140103
If you application requires working with associative arrays only, you can pass a `blancks\JsonPatch\json\handlers\ArrayJsonHandler` instance instead.
104+
- `?JsonPointerHandlerInterface $JsonPointerHandler`: Instance of the object responsible to validate and parse JSON Pointers.\
105+
The default handler is `blancks\JsonPatch\json\pointer\JsonPointer6901`
141106
- **Returns**: Instance of the `FastJsonPatch` class.
142107
- **Example**:
143108
```php
@@ -147,14 +112,16 @@ In this example you can see that the number of "John" has not changed.
147112

148113
## Public Methods
149114

150-
#### `static function fromJson(string $patch, ?JsonHandlerInterface $JsonHandler = null) : void`
115+
#### `static function fromJson(string $patch, ?JsonHandlerInterface $JsonHandler = null, ?JsonPointerHandlerInterface $JsonPointerHandler = null) : void`
151116

152117
- **Description**: Returns a new instance of the `FastJsonPatch` class.
153118
- **Parameters**:
154119
- `string $document`: A json encoded document to which the patches will be applied
155120
- `?JsonHandlerInterface $JsonHandler`: An instance of the JSON handler which will be responsible for encoding/decoding and CRUD operations.\
156121
The default handler is `blancks\JsonPatch\json\handlers\BasicJsonHandler` and decodes json objects as php \stdClass instances. This is the recommended way.\
157122
If you application requires working with associative arrays only, you can pass a `blancks\JsonPatch\json\handlers\ArrayJsonHandler` instance instead.
123+
- `?JsonPointerHandlerInterface $JsonPointerHandler`: Instance of the object responsible to validate and parse JSON Pointers.\
124+
The default handler is `blancks\JsonPatch\json\pointer\JsonPointer6901`
158125
- **Example**:
159126
```php
160127
$FastJsonPatch = FastJsonPatch::fromJson('{"foo":"bar","baz":["qux","quux"]}');
@@ -163,7 +130,7 @@ In this example you can see that the number of "John" has not changed.
163130

164131
#### `function apply(string $patch) : void`
165132

166-
- **Description**: Applies a series of patch operations to the specified JSON document. Ensures atomicity by applying all operations successfully or making no changes at all if any operation fails.
133+
- **Description**: Applies a series of patch operations to the specified JSON document. Ensures atomicity by applying all operations successfully or restoring the original document if any operation fails.
167134
- **Parameters**:
168135
- `string $patch`: A json-encoded array of patch operations.
169136
- **Exceptions**:
@@ -311,7 +278,14 @@ Test cases comes from [json-patch/json-patch-tests](https://github.com/json-patc
311278
Contributions are welcome! 🎉\
312279
Feel free to fork the repository if you'd like to contribute to this project!
313280

314-
Please ensure your contributions align with the project's goals and code style. If you have any questions or need guidance, feel free to open an issue or start a discussion.
281+
Please ensure your contributions align with the project's goals and code style:
282+
* Use `composer run static-analyse` for static analysis
283+
* Use `composer run pretty-php` to reformat your code before commit
284+
* Provide unit tests for the code you wrote, this project targets 100% coverage
285+
* It is ok to write alternative handlers to address slower operations, such as working with huge json files
286+
* Any changes that hurts the package performance in its default configuration must be motivated with a good reason
287+
288+
If you have any questions, need guidance, reporting a bug or suggesting an improvement feel free to open an issue. Every bit helps!
315289

316290
Thank you for helping to improve this project!
317291

0 commit comments

Comments
 (0)