holokron / json-patch
Library to handle JSON patch requests compatibile with RFC 6902
0.1.0
2017-06-06 19:20 UTC
Requires
- php: >=7.0.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ~2.3
- michalv8/xpmock: ^1.2
- phpmetrics/phpmetrics: ^2.2.0
- phpstan/phpstan: ^0.6.4
- phpunit/phpunit: ^6.0
This package is not auto-updated.
Last update: 2025-05-02 22:34:23 UTC
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"})