holokron/json-patch
Composer 安装命令:
composer require holokron/json-patch
包简介
Library to handle JSON patch requests compatibile with RFC 6902
关键字:
README 文档
README
Simple library to handle Json Patch requests according to RFC 6902
Other libraries are just changing one JSON string to another but the main purpose of this code is to execute particular action (callback defined in configuration) given particular JSON.
Getting started
Perequisites
- PHP >= 7.0.0
Installing
composer require holokron/json-patch
Example
Configuration
Our example class with methods which should be called:
<?php namespace Example\Handler; use Example\Entity\Account; class UserHandler { public function add($value) { $value = json_encode($value); echo "UserHandler::add($value)\n"; } public function remove(int $id) { echo "UserHandler::remove($id)\n"; } public function replace(string $id, array $value) { $value = json_encode($value); echo "UserHandler::replace($id)($value)\n"; } }
Configuration of our patcher:
<?php use Example\Handler\UserHandler; use Holokron\JsonPatch as JsonPatch; $accountHandler = new UserHandler(); $builder = new JsonPatch\Definition\Builder(); $definitions = $builder ->op('add') ->path('/users') ->callback([$handler, 'add']) ->add() ->op('remove') ->path('/users/:userId') ->callback([$handler, 'remove']) ->requirement('userId', '[1-9]+\d*') ->add() ->op('replace') ->path('/users/:userId') ->callback([$handler, 'replace']) ->requirement('userId', '\w+') ->add() ->get(); $patcherFactory = new JsonPatch\Factory(); $patcher = $patcherFactory ->setMatcher(new JsonPatch\Matcher\Matcher($definitions)) ->create();
Then applying our example JSON:
[
{
"op": "add",
"path": "/users",
"value": {
"name": "super handler"
}
},
{
"op": "remove",
"path": "/users/123"
},
{
"op": "replace",
"path": "/users/456",
"value": {
"foo": "bar"
}
}
]
We will get result:
UserHandler::add({"name":"super handler"})
UserHandler::remove(123)
UserHandler::replace(456)({"foo":"bar"})
统计信息
- 总下载量: 37
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-06-06