|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of JSON RPC Client. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Igor Lazarev <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Strider2038\JsonRpcClient\Request; |
|
12
|
|
|
|
|
13
|
|
|
use Strider2038\JsonRpcClient\Exception\InvalidRequestParamsException; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @author Igor Lazarev <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class RequestObjectFactory |
|
19
|
|
|
{ |
|
20
|
|
|
private IdGeneratorInterface $idGenerator; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(IdGeneratorInterface $idGenerator) |
|
23
|
56 |
|
{ |
|
24
|
|
|
$this->idGenerator = $idGenerator; |
|
25
|
56 |
|
} |
|
26
|
56 |
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param array|object|null $params |
|
29
|
|
|
* |
|
30
|
|
|
* @throws InvalidRequestParamsException |
|
31
|
|
|
*/ |
|
32
|
|
|
public function createRequest(string $method, $params = null): RequestObject |
|
33
|
40 |
|
{ |
|
34
|
|
|
$this->validateParams($params); |
|
35
|
40 |
|
$id = $this->idGenerator->generateId(); |
|
36
|
39 |
|
|
|
37
|
|
|
return new RequestObject($id, $method, $params); |
|
38
|
39 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param array|object|null $params |
|
42
|
|
|
* |
|
43
|
|
|
* @throws InvalidRequestParamsException |
|
44
|
|
|
*/ |
|
45
|
|
|
public function createNotification(string $method, $params = null): NotificationObject |
|
46
|
13 |
|
{ |
|
47
|
|
|
$this->validateParams($params); |
|
48
|
13 |
|
|
|
49
|
|
|
return new NotificationObject($method, $params); |
|
50
|
12 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
private function validateParams($params): void |
|
53
|
46 |
|
{ |
|
54
|
|
|
if (null !== $params && !is_object($params) && !is_array($params)) { |
|
55
|
46 |
|
throw new InvalidRequestParamsException($params); |
|
56
|
2 |
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|