Skip to content

Commit

Permalink
Created the "Evaluator" and "Exception" interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
smortensen committed Aug 17, 2015
1 parent 6e11cef commit e1e7123
Show file tree
Hide file tree
Showing 16 changed files with 354 additions and 203 deletions.
20 changes: 8 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
## Features

* Fully compliant with the [JSON-RPC 2.0 specifications](http://www.jsonrpc.org/specification) (with 100% unit-test coverage)
* Flexible: you can choose your own system for interpreting the JSON-RPC method strings
* Minimalistic: just two tiny files
* Flexible: you can use your own code to evaluate the JSON-RPC methods
* Ultra-lightweight

## Requirements

Expand All @@ -21,20 +21,17 @@ This package is released under an open-source license: [LGPL-3.0](https://www.gn
```php
$client = new Client();

$client->query(1, 'Math/subtract', array(5, 3));
$client->query(1, 'add', array(1, 2));

$request = $client->encode(); // {"jsonrpc":"2.0","id":1,"method":"Math\/subtract","params":[5,3]}
$message = $client->encode(); // {"jsonrpc":"2.0","id":1,"method":"add","params":[1,2]}
```

### Server

```php
$translator = new Translator();
$server = new Server($translator);
$server = new Server(new Api());

$request = '{"jsonrpc":"2.0","id":1,"method":"Math\/subtract","params":[5,3]}';

$reply = $server->reply($request); // {"jsonrpc":"2.0","id":1,"result":2}
$reply = $server->reply($message); // {"jsonrpc":"2.0","id":1,"result":3}
```

*See the "examples" folder for ready-to-use examples.*
Expand All @@ -45,7 +42,7 @@ If you're using [Composer](https://getcomposer.org/), you can use this package
([datto/json-rpc](https://packagist.org/packages/datto/json-rpc))
by inserting a line into the "require" section of your "composer.json" file:
```
"datto/json-rpc": "~2.0"
"datto/json-rpc": "~3.0"
```

## Getting started
Expand All @@ -56,8 +53,7 @@ by inserting a line into the "require" section of your "composer.json" file:
php examples/server.php
```

2. Take a look at the examples in the "tests" directory, and then replace them with
your own code.
2. Take a look at the code "examples/src"--then replace it with your own!

## Unit tests

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"autoload-dev": {
"psr-4": {
"Datto\\JsonRpc\\Examples\\": "examples",
"Datto\\JsonRpc\\Examples\\": "examples/src",
"Datto\\JsonRpc\\Tests\\": "tests"
}
}
Expand Down
9 changes: 5 additions & 4 deletions examples/client.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<?php

use Datto\JsonRpc\Client;

require_once __DIR__ . '/../vendor/autoload.php';

use Datto\JsonRpc\Client;

$client = new Client();

$client->query(1, 'add', array(1, 2));

$request = $client->encode();
$message = $client->encode();


echo $request, "\n";
// {"jsonrpc":"2.0","id":1,"method":"add","params":[1,2]}
echo $message, "\n"; // {"jsonrpc":"2.0","id":1,"method":"add","params":[1,2]}
15 changes: 6 additions & 9 deletions examples/server.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
<?php

require_once __DIR__ . '/../vendor/autoload.php';

use Datto\JsonRpc\Server;
use Datto\JsonRpc\Examples\Api;

$interpreter = function ($method) {
// Convert a JSON-RPC string method name into an actual callable method
return array('\\Datto\\JsonRpc\\Examples\\Application\\Math', $method);
};
require_once __DIR__ . '/../vendor/autoload.php';

$server = new Server($interpreter);

$server = new Server(new Api());

$reply = $server->reply('{"jsonrpc":"2.0","id":1,"method":"add","params":[1,2]}');

echo $reply, "\n";
// {"jsonrpc":"2.0","id":1,"result":3}

echo $reply, "\n"; // {"jsonrpc":"2.0","id":1,"result":3}
52 changes: 52 additions & 0 deletions examples/src/Api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/**
* Copyright (C) 2015 Datto, Inc.
*
* This file is part of PHP JSON-RPC.
*
* PHP JSON-RPC is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License, version 3,
* as published by the Free Software Foundation.
*
* PHP JSON-RPC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with PHP JSON-RPC. If not, see <http://www.gnu.org/licenses/>.
*
* @author Spencer Mortensen <[email protected]>
* @license http://www.gnu.org/licenses/lgpl-3.0.html LGPL-3.0
* @copyright 2015 Datto, Inc.
*/

namespace Datto\JsonRpc\Examples;

use Datto\JsonRpc\Evaluator;
use Datto\JsonRpc\Exception;
use Datto\JsonRpc\Examples\Library\Math;

class Api implements Evaluator
{
public function evaluate($method, $arguments)
{
if ($method === 'add') {
return self::add($arguments);
}

throw new Exception\Method();
}

private static function add($arguments)
{
@list($a, $b) = $arguments;

if (!is_int($a) || !is_int($b)) {
throw new Exception\Argument();
}

return Math::add($a, $b);
}
}
17 changes: 6 additions & 11 deletions examples/Application/Math.php → examples/src/Library/Math.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,21 @@
* @copyright 2015 Datto, Inc.
*/

namespace Datto\JsonRpc\Examples\Application;
namespace Datto\JsonRpc\Examples\Library;

class Math
{
/**
* Returns the value $a + $b.
*
* @param mixed $a
* @param mixed $b
* @param int $a
* @param int $b
*
* @return int|null
* Returns $a + $b if both $a and $b are integers.
* Returns null otherwise.
* @return int
* Returns the value $a + $b.
*/
public static function add($a, $b)
{
if (is_int($a) && is_int($b)) {
return $a + $b;
}

return null;
return $a + $b;
}
}
6 changes: 3 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" ?>
<phpunit bootstrap="./vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="json-rpc tests">
<directory>tests</directory>
<testsuite name="tests">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>
<logging>
Expand All @@ -11,7 +11,7 @@
<filter>
<blacklist>
<directory>examples</directory>
<directory>tests/Example</directory>
<directory>tests</directory>
<directory>vendor</directory>
</blacklist>
</filter>
Expand Down
35 changes: 35 additions & 0 deletions src/Evaluator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/**
* Copyright (C) 2015 Datto, Inc.
*
* This file is part of PHP JSON-RPC.
*
* PHP JSON-RPC is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License, version 3,
* as published by the Free Software Foundation.
*
* PHP JSON-RPC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with PHP JSON-RPC. If not, see <http://www.gnu.org/licenses/>.
*
* @author Spencer Mortensen <[email protected]>
* @license http://www.gnu.org/licenses/lgpl-3.0.html LGPL-3.0
* @copyright 2015 Datto, Inc.
*/

namespace Datto\JsonRpc;

interface Evaluator
{
/**
* @param string $method
* @param array $arguments
* @return mixed
*/
public function evaluate($method, $arguments);
}
29 changes: 29 additions & 0 deletions src/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* Copyright (C) 2015 Datto, Inc.
*
* This file is part of PHP JSON-RPC.
*
* PHP JSON-RPC is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License, version 3,
* as published by the Free Software Foundation.
*
* PHP JSON-RPC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with PHP JSON-RPC. If not, see <http://www.gnu.org/licenses/>.
*
* @author Spencer Mortensen <[email protected]>
* @license http://www.gnu.org/licenses/lgpl-3.0.html LGPL-3.0
* @copyright 2015 Datto, Inc.
*/

namespace Datto\JsonRpc;

interface Exception
{
}
36 changes: 36 additions & 0 deletions src/Exception/Argument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* Copyright (C) 2015 Datto, Inc.
*
* This file is part of PHP JSON-RPC.
*
* PHP JSON-RPC is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License, version 3,
* as published by the Free Software Foundation.
*
* PHP JSON-RPC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with PHP JSON-RPC. If not, see <http://www.gnu.org/licenses/>.
*
* @author Spencer Mortensen <[email protected]>
* @license http://www.gnu.org/licenses/lgpl-3.0.html LGPL-3.0
* @copyright 2015 Datto, Inc.
*/

namespace Datto\JsonRpc\Exception;

use Exception;
use Datto\JsonRpc;

class Argument extends Exception implements JsonRpc\Exception
{
public function __construct()
{
parent::__construct('Invalid params', -32602);
}
}
54 changes: 54 additions & 0 deletions src/Exception/Evaluation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/**
* Copyright (C) 2015 Datto, Inc.
*
* This file is part of PHP JSON-RPC.
*
* PHP JSON-RPC is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License, version 3,
* as published by the Free Software Foundation.
*
* PHP JSON-RPC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with PHP JSON-RPC. If not, see <http://www.gnu.org/licenses/>.
*
* @author Spencer Mortensen <[email protected]>
* @license http://www.gnu.org/licenses/lgpl-3.0.html LGPL-3.0
* @copyright 2015 Datto, Inc.
*/

namespace Datto\JsonRpc\Exception;

use Exception;
use Datto\JsonRpc;

class Evaluation extends Exception implements JsonRpc\Exception
{
public function __construct($message = '', $code = 0)
{
if (!self::isValidCode($code)) {
$code = 0;
}

if (!self::isValidMessage($message)) {
$message = '';
}

parent::__construct($message, $code);
}

private static function isValidCode($code)
{
return is_int($code) && (($code < -32768) || (-32000 < $code));
}

private static function isValidMessage($message)
{
return is_string($message);
}
}
Loading

0 comments on commit e1e7123

Please sign in to comment.