1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace veejay\jsonrpc\batch; |
4
|
|
|
|
5
|
|
|
use stdClass; |
6
|
|
|
|
7
|
|
|
class Response extends Common |
8
|
|
|
{ |
9
|
|
|
const PARSE_ERROR = -32700; |
10
|
|
|
const INVALID_REQUEST = -32600; |
11
|
|
|
const METHOD_NOT_FOUND = -32601; |
12
|
|
|
const INVALID_PARAMS = -32602; |
13
|
|
|
const INTERNAL_ERROR = -32603; |
14
|
|
|
const LIMIT_EXCEEDED = -32000; |
15
|
|
|
const DUPLICATED_ID = -32001; |
16
|
|
|
|
17
|
|
|
const DEFAULT_ERROR = 'Server error'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var mixed |
21
|
|
|
*/ |
22
|
|
|
public $result; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var stdClass|null |
26
|
|
|
*/ |
27
|
|
|
public $error; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Errors list. |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
public static $errors = [ |
34
|
|
|
self::PARSE_ERROR => 'Parse error', |
35
|
|
|
self::INVALID_REQUEST => 'Invalid request', |
36
|
|
|
self::METHOD_NOT_FOUND => 'Method not found', |
37
|
|
|
self::INVALID_PARAMS => 'Invalid params', |
38
|
|
|
self::INTERNAL_ERROR => 'Internal error', |
39
|
|
|
self::LIMIT_EXCEEDED => 'Limit exceeded', |
40
|
|
|
self::DUPLICATED_ID => 'Duplicated ID', |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Set error status to current response. |
45
|
|
|
* @param int $code |
46
|
|
|
* @param string $message |
47
|
|
|
* @return static |
48
|
|
|
*/ |
49
|
|
|
public function setError(int $code, string $message = ''): self |
50
|
|
|
{ |
51
|
|
|
$error = new stdClass(); |
52
|
|
|
$error->code = $code; |
53
|
|
|
$error->message = $message ?: static::getErrorMessage($code); |
54
|
|
|
$this->error = $error; |
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Check if current response has an error. |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
public function hasError(): bool |
63
|
|
|
{ |
64
|
|
|
return $this->error !== null; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Reset error. |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
public function resetError() |
72
|
|
|
{ |
73
|
|
|
$this->error = null; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Is request for current response a notification. |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
public function isNotification(): bool |
81
|
|
|
{ |
82
|
|
|
return $this->id === null; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Return object with prepared data. |
87
|
|
|
* @return stdClass |
88
|
|
|
*/ |
89
|
|
|
public function getData(): stdClass |
90
|
|
|
{ |
91
|
|
|
$object = new stdClass; |
92
|
|
|
$object->jsonrpc = $this->jsonrpc; |
93
|
|
|
if ($this->hasError()) { |
94
|
|
|
$object->error = $this->error; |
95
|
|
|
} else { |
96
|
|
|
$object->result = $this->result; |
97
|
|
|
} |
98
|
|
|
$object->id = $this->id; |
99
|
|
|
return $object; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get error message by code. |
104
|
|
|
* @param int $code |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public static function getErrorMessage(int $code): string |
108
|
|
|
{ |
109
|
|
|
return array_key_exists($code, static::$errors) ? static::$errors[$code] : static::DEFAULT_ERROR; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|