##Router the router allows you to map a string to a method inside of the class. The @ symbol is used to separate the class from the method name. It really doesn't do much on its own, but is a requirement for the Listener
$router = new \Timmachine\PhpJsonRpc\Router();
$router->add("math.subtract", 'BaseController@subtract');
$router->add("math.add", 'BaseController@add');
The listener is the brains of the operation. This bad boy will take your Json request twist it around and execute the method you want to call and then return the data to you in a properly formatted JsonRPC 2.0 format
$json = '{"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1}';
$listener = new \Timmachine\PhpJsonRpc\Listener($routes);
try {
//validate our json
$listener->validateJson($json);
// process the method request
$listener->processRequest();
// return our json response
return $listener->getResponse();
} catch (\Timmachine\PhpJsonRpc\RpcExceptions $e) {
// even if there is an error you send a response back to your client that is properly formatted
return $listener->getResponse();
}
lets make sure that we are forward thinking a little
$listener = new \Timmachine\PhpJsonRpc\Listener($routes,'2.1');
Maybe your application has some custom requirements ?
$customRequirements = [
[
'key' => 'myCustomKey',
'value' => null //no defined required value,
'errorMessage' => 'my custom error message',
'errorCode' => -32600 // Invalid params
],
[
'key' => 'myCustomKey2',
'value' => '1237485' //defined required value,
'errorMessage' => 'myCustomKey2 is not set correctly or missing',
'errorCode' => -32600 // Invalid params
]
];
$mySpec = '3.0'
$listener = new \Timmachine\PhpJsonRpc\Listener($routes,$mySpec, $customRequirements);
class foo{
public function bar($a = 2,$b = 3, $c = 4)
{
return $a + $b + $c;
}
}
// example json to make the request
// '{"jsonrpc": "2.0", "method": "foo.bar", "params":[], "id": 1}';
//example response
//{"jsonrpc": "2.0", "result": 9, "id": 1}