1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: batanov.pavel |
5
|
|
|
* Date: 11.02.2016 |
6
|
|
|
* Time: 15:25 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Bankiru\Api\JsonRpc\Specification; |
10
|
|
|
|
11
|
|
|
use Bankiru\Api\JsonRpc\Exception\InvalidRequestException; |
12
|
|
|
use Bankiru\Api\JsonRpc\Exception\JsonRpcException; |
13
|
|
|
use Bankiru\Api\JsonRpc\JsonRpcBundle; |
14
|
|
|
use ScayTrase\Api\JsonRpc\JsonRpcError; |
15
|
|
|
use ScayTrase\Api\JsonRpc\JsonRpcRequestInterface; |
16
|
|
|
|
17
|
|
|
final class JsonRpcRequest implements JsonRpcRequestInterface |
18
|
|
|
{ |
19
|
|
|
/** @var string|null */ |
20
|
|
|
private $id; |
21
|
|
|
/** @var string */ |
22
|
|
|
private $method; |
23
|
|
|
/** @var mixed|\stdClass */ |
24
|
|
|
private $parameters; |
25
|
|
|
|
26
|
|
|
/** @return bool True if request should not receive response from the server */ |
27
|
7 |
|
public function isNotification() |
28
|
|
|
{ |
29
|
7 |
|
return null === $this->id; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param \stdClass $source |
34
|
|
|
* |
35
|
|
|
* @return JsonRpcRequestInterface |
36
|
|
|
* @throws JsonRpcException |
37
|
|
|
*/ |
38
|
7 |
|
public static function fromStdClass(\stdClass $source) |
39
|
|
|
{ |
40
|
7 |
|
$request = new static; |
41
|
|
|
|
42
|
7 |
|
$missing = []; |
43
|
|
|
|
44
|
7 |
|
foreach (['method', 'jsonrpc'] as $field) { |
45
|
7 |
|
if (!isset($source->$field)) { |
46
|
|
|
$missing[] = $field; |
47
|
|
|
} |
48
|
7 |
|
} |
49
|
7 |
|
if (count($missing) > 0) { |
50
|
|
|
throw InvalidRequestException::missingFields($missing); |
51
|
|
|
} |
52
|
|
|
|
53
|
7 |
|
if (JsonRpcBundle::VERSION !== $source->jsonrpc) { |
54
|
|
|
throw InvalidRequestException::invalidVersion(JsonRpcBundle::VERSION, $source->jsonrpc); |
55
|
|
|
} |
56
|
|
|
|
57
|
7 |
|
$request->id = isset($source->id) ? $source->id : null; |
58
|
7 |
|
$request->method = $source->method; |
59
|
7 |
|
$request->parameters = isset($source->params) ? json_decode(json_encode($source->params), true) : null; |
60
|
|
|
|
61
|
7 |
|
return $request; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** {@inheritdoc} */ |
65
|
|
View Code Duplication |
public function jsonSerialize() |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
return [ |
68
|
|
|
self::VERSION_FIELD => $this->getVersion(), |
69
|
|
|
self::METHOD_FIELD => $this->getMethod(), |
70
|
|
|
self::PARAMETERS_FIELD => $this->getParameters(), |
71
|
|
|
self::ID_FIELD => $this->getId(), |
72
|
|
|
]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** {@inheritdoc} */ |
76
|
|
|
public function getVersion() |
77
|
|
|
{ |
78
|
|
|
return JsonRpcBundle::VERSION; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** @return string */ |
82
|
7 |
|
public function getMethod() |
83
|
|
|
{ |
84
|
7 |
|
return $this->method; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** @return array */ |
88
|
6 |
|
public function getParameters() |
89
|
|
|
{ |
90
|
6 |
|
return $this->parameters; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** @return int|null Id. if not a notification and id is not set - id should be automatically generated */ |
94
|
7 |
|
public function getId() |
95
|
|
|
{ |
96
|
7 |
|
return $this->id; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.