|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Guzzle HTTP JSON-RPC |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2014 Nature Delivered Ltd. <http://graze.com> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* @see http://github.com/graze/guzzle-jsonrpc/blob/master/LICENSE |
|
12
|
|
|
* @link http://github.com/graze/guzzle-jsonrpc |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Graze\GuzzleHttp\JsonRpc\Message; |
|
16
|
|
|
|
|
17
|
|
|
use Graze\GuzzleHttp\JsonRpc; |
|
18
|
|
|
use Psr\Http\Message\RequestInterface as HttpRequestInterface; |
|
19
|
|
|
use Psr\Http\Message\ResponseInterface as HttpResponseInterface; |
|
20
|
|
|
|
|
21
|
|
|
class MessageFactory implements MessageFactoryInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @param string $method |
|
25
|
|
|
* @param string $uri |
|
26
|
|
|
* @param array $headers |
|
27
|
|
|
* @param array $options |
|
28
|
|
|
* |
|
29
|
|
|
* @return RequestInterface |
|
30
|
|
|
*/ |
|
31
|
1 |
|
public function createRequest($method, $uri, array $headers = [], array $options = []) |
|
32
|
|
|
{ |
|
33
|
1 |
|
$body = JsonRpc\json_encode($this->addIdToRequest($method, $options)); |
|
34
|
|
|
|
|
35
|
1 |
|
return new Request('POST', $uri, $headers, $body === false ? null : $body); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param int $statusCode |
|
40
|
|
|
* @param array $headers |
|
41
|
|
|
* @param array $options |
|
42
|
|
|
* |
|
43
|
|
|
* @return ResponseInterface |
|
44
|
|
|
*/ |
|
45
|
2 |
|
public function createResponse($statusCode, array $headers = [], array $options = []) |
|
46
|
|
|
{ |
|
47
|
2 |
|
$body = JsonRpc\json_encode($options); |
|
48
|
|
|
|
|
49
|
2 |
|
return new Response($statusCode, $headers, $body === false ? null : $body); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param HttpRequestInterface $request |
|
54
|
|
|
* |
|
55
|
|
|
* @return RequestInterface |
|
56
|
|
|
*/ |
|
57
|
|
|
public function fromRequest(HttpRequestInterface $request) |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->createRequest( |
|
60
|
|
|
$request->getMethod(), |
|
61
|
|
|
$request->getUri(), |
|
62
|
|
|
$request->getHeaders(), |
|
63
|
|
|
JsonRpc\json_decode((string) $request->getBody(), true) ?: [] |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param HttpResponseInterface $response |
|
69
|
|
|
* |
|
70
|
|
|
* @return ResponseInterface |
|
71
|
|
|
*/ |
|
72
|
|
|
public function fromResponse(HttpResponseInterface $response) |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->createResponse( |
|
75
|
|
|
$response->getStatusCode(), |
|
76
|
|
|
$response->getHeaders(), |
|
77
|
|
|
JsonRpc\json_decode((string) $response->getBody(), true) ?: [] |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $method |
|
83
|
|
|
* @param array $data |
|
84
|
|
|
* |
|
85
|
|
|
* @return array |
|
86
|
|
|
*/ |
|
87
|
1 |
|
protected function addIdToRequest($method, array $data) |
|
88
|
|
|
{ |
|
89
|
1 |
|
if (RequestInterface::REQUEST === $method && ! isset($data['id'])) { |
|
90
|
1 |
|
$data['id'] = uniqid(true); |
|
|
|
|
|
|
91
|
1 |
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
return $data; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|