Skip to content

Commit 8846af5

Browse files
committed
Add in constructors.
Cleanup code Prepare for version 1
1 parent fb14a2d commit 8846af5

File tree

5 files changed

+253
-66
lines changed

5 files changed

+253
-66
lines changed

src/EdgeTelemetrics/JSON_RPC/Error.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,36 @@
22

33
namespace EdgeTelemetrics\JSON_RPC;
44

5-
class Error implements \JsonSerializable
5+
use JsonSerializable;
6+
7+
/**
8+
* Class Error - Representation of RPC Error
9+
* @package EdgeTelemetrics\JSON_RPC
10+
*/
11+
class Error implements JsonSerializable
612
{
7-
/**
8-
* @var int
9-
*/
13+
/** Reserved error codes */
14+
const PARSE_ERROR = -32700;
15+
const INVALID_REQUEST = -32600;
16+
const METHOD_NOT_FOUND = -32601;
17+
const INVALID_PARAMS = -32602;
18+
const INTERNAL_ERROR = -32603;
19+
20+
const ERROR_MSG = [
21+
self::PARSE_ERROR => "Parse error",
22+
self::INVALID_REQUEST => "Invalid Request",
23+
self::METHOD_NOT_FOUND => "Method not found",
24+
self::INVALID_PARAMS => "Invalid params",
25+
self::INTERNAL_ERROR => "Internal error",
26+
];
27+
28+
/** @var int A Number that indicates the error type that occurred. */
1029
protected $code;
1130

12-
/**
13-
* @var string
14-
*/
31+
/** @var string A String providing a short description of the error. */
1532
protected $message;
1633

34+
/** @var mixed Additional information about the error */
1735
protected $data;
1836

1937
public function __construct(int $code, string $message, $data = null)

src/EdgeTelemetrics/JSON_RPC/Notification.php

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,97 @@
22

33
namespace EdgeTelemetrics\JSON_RPC;
44

5-
class Notification implements \JsonSerializable {
5+
use JsonSerializable;
66

7+
/**
8+
* Class Notification
9+
* @package EdgeTelemetrics\JSON_RPC
10+
*/
11+
class Notification implements JsonSerializable {
12+
13+
/** @var string JSONRPC Version String */
714
const JSONRPC_VERSION = '2.0';
815

916
/**
10-
* @var string
17+
* @var string A String containing the name of the method to be invoked
1118
*/
1219
protected $method = '';
1320

1421
/**
15-
* @var array
22+
* @var array A Structured value that holds the parameter values to be used during the invocation of the method
1623
*/
1724
protected $params = [];
1825

26+
/**
27+
* Notification constructor.
28+
* @param string $method
29+
* @param array $params
30+
*/
31+
public function __construct(string $method, array $params = [])
32+
{
33+
$this->setMethod($method);
34+
$this->setParams($params);
35+
}
36+
37+
/**
38+
* Set RPC method to be invoked
39+
* @param string $method
40+
*/
1941
public function setMethod(string $method)
2042
{
2143
$this->method = $method;
2244
}
2345

46+
/**
47+
* Get RPC method to be invoked
48+
* @return string
49+
*/
2450
public function getMethod() : string
2551
{
2652
return $this->method;
2753
}
2854

55+
/**
56+
* Set and replace parameters
57+
* @param array $params
58+
*/
2959
public function setParams(array $params)
3060
{
3161
$this->params = $params;
3262
}
3363

64+
/**
65+
* Set an single parameter
66+
* @param string $name
67+
* @param $value
68+
*/
3469
public function setParam(string $name, $value)
3570
{
3671
$this->params[$name] = $value;
3772
}
3873

74+
/**
75+
* Get all parameters
76+
* @return array
77+
*/
3978
public function getParams()
4079
{
4180
return $this->params;
4281
}
4382

83+
/**
84+
* Get parameter value by name
85+
* @param string $name
86+
* @return mixed
87+
*/
4488
public function getParam(string $name)
4589
{
4690
return $this->params[$name];
4791
}
4892

93+
/**
94+
* @return mixed
95+
*/
4996
public function jsonSerialize()
5097
{
5198
$record = ['jsonrpc' => self::JSONRPC_VERSION,

src/EdgeTelemetrics/JSON_RPC/React/Decoder.php

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use React\Stream\ReadableStreamInterface;
77
use React\Stream\WritableStreamInterface;
88
use React\Stream\Util;
9+
use Exception;
910
use RuntimeException;
1011
use Clue\React\NDJson\Decoder as NDJsonDecoder;
1112
use EdgeTelemetrics\JSON_RPC\Notification;
@@ -23,8 +24,15 @@ class Decoder extends EventEmitter implements ReadableStreamInterface
2324
*/
2425
protected $ndjson_decoder;
2526

27+
/**
28+
* @var bool Flag if stream is closed
29+
*/
2630
private $closed = false;
2731

32+
/**
33+
* Decoder constructor.
34+
* @param ReadableStreamInterface $input
35+
*/
2836
public function __construct(ReadableStreamInterface $input)
2937
{
3038
$this->ndjson_decoder = new NDJsonDecoder($input, true);
@@ -35,6 +43,9 @@ public function __construct(ReadableStreamInterface $input)
3543
$this->ndjson_decoder->on('close', array($this, 'close'));
3644
}
3745

46+
/**
47+
* Close the stream
48+
*/
3849
public function close()
3950
{
4051
$this->closed = true;
@@ -43,28 +54,45 @@ public function close()
4354
$this->removeAllListeners();
4455
}
4556

57+
/**
58+
* @return bool
59+
*/
4660
public function isReadable()
4761
{
4862
return $this->ndjson_decoder->isReadable();
4963
}
5064

65+
/**
66+
* Pause
67+
*/
5168
public function pause()
5269
{
5370
$this->ndjson_decoder->pause();
5471
}
5572

73+
/**
74+
* Resume
75+
*/
5676
public function resume()
5777
{
5878
$this->ndjson_decoder->resume();
5979
}
6080

81+
/**
82+
* Pipe output between up and $dest
83+
* @param WritableStreamInterface $dest
84+
* @param array $options
85+
* @return WritableStreamInterface
86+
*/
6187
public function pipe(WritableStreamInterface $dest, array $options = array())
6288
{
6389
Util::pipe($this, $dest, $options);
6490
return $dest;
6591
}
6692

67-
//@TODO Handle json-rpc batch (array of data)
93+
/**
94+
* @param $input
95+
*/
6896
public function handleData($input)
6997
{
7098
/** Check if we are batch request */
@@ -85,27 +113,15 @@ public function handleData($input)
85113

86114
if (isset($data['method'])) {
87115
if (isset($data['id'])) {
88-
$jsonrpc = new Request();
89-
$jsonrpc->setId($data['id']);
116+
$jsonrpc = new Request($data['method'], $data['params'] ?? [], $data['id']);
90117
} else {
91-
$jsonrpc = new Notification();
92-
}
93-
$jsonrpc->setMethod($data['method']);
94-
if (isset($data['params'])) {
95-
$jsonrpc->setParams($data['params']);
96-
}
97-
} elseif (isset($data['result']) || isset($data['error'])) {
98-
$jsonrpc = new Response();
99-
$jsonrpc->setId($data['id']);
100-
if (isset($data['result'])) {
101-
$jsonrpc->setResult($data['result']);
102-
} else {
103-
$error = new Error($data['error']['code'], $data['error']['message']);
104-
if (isset($data['error']['data'])) {
105-
$error->setData($data['error']['data']);
106-
}
107-
$jsonrpc->setError($error);
118+
$jsonrpc = new Notification($data['method'], $data['params']);
108119
}
120+
} elseif (isset($data['result'])) {
121+
$jsonrpc = new Response($data['id'], $data['result']);
122+
} elseif (isset($data['error'])) {
123+
$error = new Error($data['error']['code'], $data['error']['message'], $data['error']['data'] ?? null);
124+
$jsonrpc = new Response($data['id'], $error);
109125
} else {
110126
throw new RuntimeException('Unable to decode json rpc packet');
111127
}
@@ -122,8 +138,11 @@ public function handleEnd()
122138
}
123139
}
124140

125-
/** @internal */
126-
public function handleError(\Exception $error)
141+
/**
142+
* @param Exception $error
143+
* @internal
144+
*/
145+
public function handleError(Exception $error)
127146
{
128147
$this->emit('error', array($error));
129148
$this->close();

src/EdgeTelemetrics/JSON_RPC/Request.php

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,68 @@
22

33
namespace EdgeTelemetrics\JSON_RPC;
44

5-
class Request extends Notification implements \JsonSerializable {
5+
use JsonSerializable;
6+
use RuntimeException;
7+
8+
use function is_float;
9+
use function is_string;
10+
use function is_int;
11+
use function is_null;
12+
13+
/**
14+
* Class Request
15+
* @package EdgeTelemetrics\JSON_RPC
16+
*
17+
* Request extends Notification and includes the Id property
18+
*/
19+
class Request extends Notification implements JsonSerializable {
620

721
/**
8-
* @var string|int|null
22+
* @var string|int|null An identifier established by the Client that MUST contain a String, Number, or NULL value
923
*/
1024
protected $id;
1125

26+
/**
27+
* Request constructor.
28+
* @param string $method
29+
* @param array $params
30+
* @param null $id
31+
*/
32+
public function __construct(string $method, array $params = [], $id = null)
33+
{
34+
parent::__construct($method, $params);
35+
$this->setId($id);
36+
}
37+
38+
/**
39+
* Set the id for the request. This is used between the Client and Server to correlate requests with responses.
40+
* @param string|float|null $id
41+
*/
1242
public function setId($id)
1343
{
14-
$this->id = $id;
44+
/** JSONRPC Spec - Numbers SHOULD NOT contain fractional parts */
45+
if (is_float($id)) {
46+
$id = (int)$id;
47+
}
48+
/** String, Number, or NULL value */
49+
if (is_string($id) || is_int($id) || is_null($id)) {
50+
$this->id = $id;
51+
} else {
52+
throw new RuntimeException('Invalid Id format. Must be string, number or null');
53+
}
1554
}
1655

56+
/**
57+
* @return int|string|null
58+
*/
1759
public function getId()
1860
{
1961
return $this->id;
2062
}
2163

64+
/**
65+
* @return mixed
66+
*/
2267
public function jsonSerialize()
2368
{
2469
$record = parent::jsonSerialize();

0 commit comments

Comments
 (0)