|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace drupol\Yaroc\Plugin; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
6
|
|
|
|
|
7
|
|
|
abstract class MethodPluginBase implements MethodPluginInterface { |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* The method name. |
|
11
|
|
|
*/ |
|
12
|
|
|
const METHOD = 'BASE'; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* The parameters. |
|
16
|
|
|
* |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $parameters; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The API Key. |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $apiKey; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The API Version. |
|
30
|
|
|
* |
|
31
|
|
|
* @var int |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $apiVersion; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
|
|
*/ |
|
38
|
24 |
|
public function getMethod() { |
|
39
|
24 |
|
return $this::METHOD; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc} |
|
44
|
|
|
*/ |
|
45
|
24 |
|
public function setParameters(array $parameters = array()) { |
|
46
|
24 |
|
$this->parameters = $parameters; |
|
47
|
|
|
|
|
48
|
24 |
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
12 |
|
public function getDefaultParameters() { |
|
55
|
12 |
|
return []; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
24 |
|
public function getParameters() { |
|
62
|
24 |
|
$defaultParameters = $this->getDefaultParameters(); |
|
63
|
|
|
|
|
64
|
24 |
|
$params = []; |
|
65
|
24 |
|
foreach ($this->getDefaultParameters() as $key => $parameter) { |
|
66
|
24 |
|
if (in_array($this->getApiVersion(), (array) $defaultParameters[$key]['api'])) { |
|
67
|
24 |
|
if (!is_null($parameter['value'])) { |
|
68
|
24 |
|
$params[$key] = $parameter['value']; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
24 |
|
foreach ((array) $this->parameters as $key => $value) { |
|
74
|
24 |
|
if (array_key_exists($key, $defaultParameters)) { |
|
75
|
24 |
|
if (in_array($this->getApiVersion(), (array) $defaultParameters[$key]['api'])) { |
|
76
|
24 |
|
$params[$key] = $value; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
24 |
|
$this->setParameters($params); |
|
82
|
|
|
|
|
83
|
24 |
|
return array_filter([ |
|
84
|
24 |
|
'jsonrpc' => '2.0', |
|
85
|
24 |
|
'id' => $this->getRandomId(), |
|
86
|
24 |
|
'method' => $this->getMethod(), |
|
87
|
24 |
|
'params' => $params, |
|
88
|
|
|
]); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get a random ID. |
|
93
|
|
|
* |
|
94
|
|
|
* @return int |
|
95
|
|
|
*/ |
|
96
|
12 |
|
public function getRandomId() { |
|
97
|
12 |
|
return mt_rand(1, 10^6); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritdoc} |
|
102
|
|
|
*/ |
|
103
|
12 |
|
public function setApiVersion($apiVersion) { |
|
104
|
12 |
|
$this->apiVersion = $apiVersion; |
|
105
|
|
|
|
|
106
|
12 |
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* {@inheritdoc} |
|
111
|
|
|
*/ |
|
112
|
12 |
|
public function getApiVersion() { |
|
113
|
12 |
|
return $this->apiVersion; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* {@inheritdoc} |
|
118
|
|
|
*/ |
|
119
|
13 |
|
public function getResult(ResponseInterface $response) { |
|
120
|
13 |
|
$body = json_decode($response->getBody(), TRUE); |
|
121
|
13 |
|
$response->getBody()->rewind(); |
|
122
|
13 |
|
return $body['result']; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
|