PHP code example of gamringer / php-json-patch

1. Go to this page and download the library: Download gamringer/php-json-patch library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gamringer / php-json-patch example snippets




$target = ['1', '2', '3'];
$operation = new \gamringer\JSONPatch\Operation\Test('/foo', 'bar');
$operation->apply($target);




$operation = \gamringer\JSONPatch\Operation\Test::fromDecodedJSON(json_decode('{"path":"/foo","value":"bar"}'));




$patch = new \gamringer\JSONPatch\Patch();

$patch->addOperation(new \gamringer\JSONPatch\Operation\Add('/foo', 'bar'));
$patch->addOperation(new \gamringer\JSONPatch\Operation\Test('/foo', 'bar'));



$patch = \gamringer\JSONPatch\Patch::fromJSON('[{"op":"add","path":"/foo","value":"bar"},{"op":"test","path":"/foo","value":"bar"}]');



$patch = \gamringer\JSONPatch\Patch::fromJSON('[{"op":"add","path":"/foo","value":"bar"},{"op":"test","path":"/foo","value":"bar"}]');

$target = [];
$patch->apply($target);

var_dump($target);

/* Results:

array(1) {
  'foo' =>
  string(3) "bar"
}

*/




$patch = \gamringer\JSONPatch\Patch::fromJSON('[{"op":"add","path":"/foo","value":"bar"},{"op":"test","path":"/foo","value":"baz"}]');

$target = [];

try {
    $patch->apply($target);
} catch (\gamringer\JSONPatch\Exception $e) {
    var_dump($target);
}

/* Results:

array(0) {}

*/