holokron/json-patch 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-06-06

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固