-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created the "Evaluator" and "Exception" interfaces
- Loading branch information
1 parent
6e11cef
commit e1e7123
Showing
16 changed files
with
354 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.