|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cmobi\RabbitmqBundle\Rpc\Response; |
|
4
|
|
|
|
|
5
|
|
|
use Cmobi\RabbitmqBundle\Rpc\Exception\RpcGenericErrorException; |
|
6
|
|
|
use Cmobi\RabbitmqBundle\Rpc\Exception\RpcInvalidResponseException; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; |
|
8
|
|
|
|
|
9
|
|
|
class RpcResponse implements RpcResponseInterface |
|
10
|
|
|
{ |
|
11
|
|
|
const VERSION = '2.0'; |
|
12
|
|
|
|
|
13
|
|
|
public $id; |
|
14
|
|
|
public $attributes; |
|
15
|
|
|
public $error; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct($id = null, array $attributes = [], RpcGenericErrorException $error = null) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->id = $id; |
|
20
|
|
|
$this->error = $error; |
|
21
|
|
|
$this->attributes = new ParameterBag($attributes); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @return int|string |
|
26
|
|
|
*/ |
|
27
|
|
|
public function getId() |
|
28
|
|
|
{ |
|
29
|
|
|
return $this->id; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param int|string $id |
|
34
|
|
|
*/ |
|
35
|
|
|
public function setId($id) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->id = $id; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return string |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getVersion() |
|
44
|
|
|
{ |
|
45
|
|
|
return self::VERSION; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return null|array |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getError() |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->error; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param RpcGenericErrorException $error |
|
58
|
|
|
*/ |
|
59
|
|
|
public function setError(RpcGenericErrorException $error) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->error = $error; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param $key |
|
66
|
|
|
* @return mixed |
|
67
|
|
|
*/ |
|
68
|
|
|
public function get($key) |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->attributes->get($key); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return array |
|
75
|
|
|
*/ |
|
76
|
|
|
public function toArray() |
|
77
|
|
|
{ |
|
78
|
|
|
$rpc = [ |
|
79
|
|
|
'id' => $this->id, |
|
80
|
|
|
'jsonrpc' => self::VERSION, |
|
81
|
|
|
'result' => $this->attributes->all() |
|
82
|
|
|
]; |
|
83
|
|
|
|
|
84
|
|
|
if ($this->error instanceof RpcGenericErrorException) { |
|
85
|
|
|
$rpc['error'] = $this->error; |
|
86
|
|
|
unset($rpc['result']); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $rpc; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param array $response |
|
94
|
|
|
* @return $this |
|
95
|
|
|
* @throws RpcInvalidResponseException |
|
96
|
|
|
*/ |
|
97
|
|
|
public function fromArray(array $response) |
|
98
|
|
|
{ |
|
99
|
|
|
$this->validate($response); |
|
100
|
|
|
$this->id = $response['id']; |
|
101
|
|
|
|
|
102
|
|
|
if (isset($response['error'])) { |
|
103
|
|
|
$this->error = $response['error']; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if (isset($response['result'])) { |
|
107
|
|
|
|
|
108
|
|
|
$result = $response['result']; |
|
109
|
|
|
|
|
110
|
|
|
if (!is_array($result)) { |
|
111
|
|
|
$result = [$result]; |
|
112
|
|
|
} |
|
113
|
|
|
$this->attributes = new ParameterBag($result); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $this; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param array $response |
|
121
|
|
|
* @throws RpcInvalidResponseException |
|
122
|
|
|
*/ |
|
123
|
|
|
public function validate(array $response) |
|
124
|
|
|
{ |
|
125
|
|
|
if ( |
|
126
|
|
|
!array_key_exists(['id', 'jsonrpc'], $response) |
|
127
|
|
|
|| (!isset($response['result']) |
|
128
|
|
|
&& !isset($response['error'])) |
|
129
|
|
|
) { |
|
130
|
|
|
throw new RpcInvalidResponseException(); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
} |