2017 © Pedro PelĂĄez
 

library json-patch

Library to handle JSON patch requests compatibile with RFC 6902

image

holokron/json-patch

Library to handle JSON patch requests compatibile with RFC 6902

  • Tuesday, June 6, 2017
  • by michalv8
  • Repository
  • 1 Watchers
  • 0 Stars
  • 6 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Json Patch

Simple library to handle Json Patch requests according to RFC 6902, (*1)

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., (*2)

Getting started

Perequisites

  • PHP >= 7.0.0

Installing

composer require holokron/json-patch

Example

Configuration

Our example class with methods which should be called:, (*3)

<?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:, (*4)

<?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:, (*5)

[
    {
        "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:, (*6)

UserHandler::add({"name":"super handler"})
UserHandler::remove(123)
UserHandler::replace(456)({"foo":"bar"})

The Versions

06/06 2017

dev-master

9999999-dev

Library to handle JSON patch requests compatibile with RFC 6902

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

by MichaƂ TęczyƄski

json rfc6902 json-patch

06/06 2017

0.1.0

0.1.0.0

Library to handle JSON patch requests compatibile with RFC 6902

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

by MichaƂ TęczyƄski

json rfc6902 json-patch

06/06 2017

v0.1.x-dev

0.1.9999999.9999999-dev

Library to handle JSON patch requests compatibile with RFC 6902

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

by MichaƂ TęczyƄski

json rfc6902 json-patch