gamringer/php-json-patch

PHP JSON Patch (RFC6902) 实现

1.0 2015-09-19 02:00 UTC

This package is auto-updated.

Last update: 2024-09-06 23:25:26 UTC


README

License Latest Stable Version Latest Unstable Version Total Downloads

SensioLabsInsight

Build Status

Build Status Code Coverage Scrutinizer Code Quality

一个符合RFC6902的JSON Patch PHP实现

#许可证 JSONPatch遵循MIT许可证。

#安装

composer require gamringer/php-json-patch

##测试

composer install
phpunit

#文档

##操作可以独立构建和应用

<?php

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

##操作也可以从JSON字符串构建

<?php

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

##可以从一系列操作构建补丁

<?php

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

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

##可以从JSON字符串构建补丁

<?php

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

##可以应用补丁

<?php

$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"
}

*/

如果补丁失败,它将被完全回滚并抛出异常。

<?php

$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) {}

*/