GitHub - remorhaz/php-json-patch: JSON Patch (RFC-6902) PHP implementation (original) (raw)

PHP JSON Patch

Latest Stable Version Build Scrutinizer Code Quality codecov Mutation testing badgeTotal Downloads License

This library implements RFC6902-compliant JSON patch tool.

Requirements

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′;<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>d</mi><mi>o</mi><mi>c</mi><mi>u</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mn>1</mn><mo>=</mo></mrow><annotationencoding="application/x−tex">document1=</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.6944em;"></span><spanclass="mordmathnormal">d</span><spanclass="mordmathnormal">oc</span><spanclass="mordmathnormal">u</span><spanclass="mordmathnormal">m</span><spanclass="mordmathnormal">e</span><spanclass="mordmathnormal">n</span><spanclass="mordmathnormal">t</span><spanclass="mord">1</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span></span></span></span>encodedValueFactory−>createValue(encodedJson = '{"a":1}'; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>d</mi><mi>o</mi><mi>c</mi><mi>u</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mn>1</mn><mo>=</mo></mrow><annotation encoding="application/x-tex">document1 = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">d</span><span class="mord mathnormal">oc</span><span class="mord mathnormal">u</span><span class="mord mathnormal">m</span><span class="mord mathnormal">e</span><span class="mord mathnormal">n</span><span class="mord mathnormal">t</span><span class="mord">1</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>encodedValueFactory->createValue(encodedJson="a":1;<spanclass="katex"><spanclass="katexmathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>d</mi><mi>o</mi><mi>c</mi><mi>u</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mn>1</mn><mo>=</mo></mrow><annotationencoding="application/xtex">document1=</annotation></semantics></math></span><spanclass="katexhtml"ariahidden="true"><spanclass="base"><spanclass="strut"style="height:0.6944em;"></span><spanclass="mordmathnormal">d</span><spanclass="mordmathnormal">oc</span><spanclass="mordmathnormal">u</span><spanclass="mordmathnormal">m</span><spanclass="mordmathnormal">e</span><spanclass="mordmathnormal">n</span><spanclass="mordmathnormal">t</span><spanclass="mord">1</span><spanclass="mspace"style="marginright:0.2778em;"></span><spanclass="mrel">=</span></span></span></span>encodedValueFactory>createValue(encodedJson);

// Creating document from decoded JSON data: $decodedValueFactory = DecodedJson\NodeValueFactory::create(); decodedJson=(object)[′a′=>1];<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>d</mi><mi>o</mi><mi>c</mi><mi>u</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mn>2</mn><mo>=</mo></mrow><annotationencoding="application/x−tex">document2=</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.6944em;"></span><spanclass="mordmathnormal">d</span><spanclass="mordmathnormal">oc</span><spanclass="mordmathnormal">u</span><spanclass="mordmathnormal">m</span><spanclass="mordmathnormal">e</span><spanclass="mordmathnormal">n</span><spanclass="mordmathnormal">t</span><spanclass="mord">2</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span></span></span></span>decodedValueFactory−>createValue(decodedJson = (object) ['a' => 1]; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>d</mi><mi>o</mi><mi>c</mi><mi>u</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mn>2</mn><mo>=</mo></mrow><annotation encoding="application/x-tex">document2 = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">d</span><span class="mord mathnormal">oc</span><span class="mord mathnormal">u</span><span class="mord mathnormal">m</span><span class="mord mathnormal">e</span><span class="mord mathnormal">n</span><span class="mord mathnormal">t</span><span class="mord">2</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>decodedValueFactory->createValue(decodedJson=(object)[a=>1];<spanclass="katex"><spanclass="katexmathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>d</mi><mi>o</mi><mi>c</mi><mi>u</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mn>2</mn><mo>=</mo></mrow><annotationencoding="application/xtex">document2=</annotation></semantics></math></span><spanclass="katexhtml"ariahidden="true"><spanclass="base"><spanclass="strut"style="height:0.6944em;"></span><spanclass="mordmathnormal">d</span><spanclass="mordmathnormal">oc</span><spanclass="mordmathnormal">u</span><spanclass="mordmathnormal">m</span><spanclass="mordmathnormal">e</span><spanclass="mordmathnormal">n</span><spanclass="mordmathnormal">t</span><spanclass="mord">2</span><spanclass="mspace"style="marginright:0.2778em;"></span><spanclass="mrel">=</span></span></span></span>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:

patch=patch = patch=encodedValueFactory->createValue('[{"op":"remove","path":"/0"}]'); query=query = query=queryFactory->createQuery($patch); document=document = document=encodedValueFactory->createValue('[1,2]'); result=result = 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 [](#license) PHP JSON Patch is licensed under [MIT license](/remorhaz/php-json-patch/blob/master/LICENSE).