forked from fritz-payment/jsonrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Response.php
122 lines (111 loc) · 2.53 KB
/
Response.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/*
* This file is part of the Fritz Payment JSON RPC package.
*
* (c) Fritz Payment GmbH <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FritzPayment\JsonRpc;
use FritzPayment\JsonRpc\Request;
/**
* A JSON RPC response.
*
* A transport will call the setResponseBody() method to pass the raw response string.
* It is the responsibility of the response implementations to parse/create the applicable
* JSON RPC id, result and error objects.
*/
abstract class Response
{
/**
* @var Request
*/
protected $request;
/**
* @var mixed
*/
protected $id;
/**
* @var string
*/
protected $responseBody;
/**
* @var \stdClass
*/
protected $responseJson;
/**
* @var int
*/
protected $jsonLastError;
/**
* @var Error
*/
protected $error = null;
/**
* @param Request $request
*
* @return Response
*/
public function setRequest(Request $request) {
$this->request = $request;
return $this;
}
/**
* @return mixed
*/
public function getId() {
return $this->id;
}
/**
* Returns the JSON RPC protocol version.
*
* @return string
*/
abstract public function getVersion();
/**
* Called by client. Pass the raw body to the response
*
* @param $responseBody
*
* @return Response
*/
public function setResponseBody($responseBody) {
$this->responseBody = $responseBody;
return $this;
}
/**
* @return \stdClass|array
*/
abstract public function getResult();
/**
* @return bool
*/
public function isError() {
return $this->error !== null;
}
/**
* @return Error|null
*/
public function getError() {
return $this->error;
}
protected function parseResponseBody() {
$this->responseJson = json_decode($this->responseBody);
if ($this->responseJson === null) {
$this->jsonLastError = json_last_error();
return false;
}
return true;
}
/**
* Will be called by the client. This method should take the raw response body and
* create the applicable result objects.
*
* The concrete implementation should always check for the correctness
* of the JSON structure.
*
* @return bool
*/
abstract public function parseResponse();
}