|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the KongAdminApi package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Unikorp <https://github.com/unikorp> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Unikorp\KongAdminApi\Document; |
|
13
|
|
|
|
|
14
|
|
|
use Unikorp\KongAdminApi\AbstractDocument; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* plugin document |
|
18
|
|
|
* |
|
19
|
|
|
* @author VEBER Arnaud <https://github.com/VEBERArnaud> |
|
20
|
|
|
*/ |
|
21
|
|
|
class PluginDocument extends AbstractDocument |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* name |
|
25
|
|
|
* @var string $name |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $name = null; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* consumer id |
|
31
|
|
|
* @var string $consumerId |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $consumerId = null; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* config |
|
37
|
|
|
* @var array $config |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $config = []; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* set name |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $name |
|
45
|
|
|
* |
|
46
|
|
|
* @return this |
|
47
|
|
|
*/ |
|
48
|
1 |
|
public function setName(string $name): self |
|
49
|
|
|
{ |
|
50
|
1 |
|
$this->name = $name; |
|
51
|
|
|
|
|
52
|
1 |
|
return $this; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* get name |
|
57
|
|
|
* |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public function getName(): string |
|
61
|
|
|
{ |
|
62
|
1 |
|
return $this->name; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* set consumer id |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $consumerId |
|
69
|
|
|
* |
|
70
|
|
|
* @return this |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function setConsumerId(string $consumerId): self |
|
73
|
|
|
{ |
|
74
|
1 |
|
$this->consumerId = $consumerId; |
|
75
|
|
|
|
|
76
|
1 |
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* get consumer id |
|
81
|
|
|
* |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
1 |
|
public function getConsumerId(): string |
|
85
|
|
|
{ |
|
86
|
1 |
|
return $this->consumerId; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* add config |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $name |
|
93
|
|
|
* @param mixed $value |
|
94
|
|
|
* |
|
95
|
|
|
* @return this |
|
96
|
|
|
*/ |
|
97
|
2 |
|
public function addConfig(string $name, $value): self |
|
98
|
|
|
{ |
|
99
|
2 |
|
if (isset($this->config[$name])) { |
|
100
|
1 |
|
throw new \RuntimeException(sprintf('Config for name `%1$s` already set', $name)); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
2 |
|
$this->config[$name] = $value; |
|
104
|
|
|
|
|
105
|
2 |
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* remove config |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $name |
|
112
|
|
|
* |
|
113
|
|
|
* @return this |
|
114
|
|
|
*/ |
|
115
|
1 |
|
public function removeConfig(string $name): self |
|
116
|
|
|
{ |
|
117
|
1 |
|
if (isset($this->config[$name])) { |
|
118
|
1 |
|
unset($this->config[$name]); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
1 |
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* get config |
|
126
|
|
|
* |
|
127
|
|
|
* @param string $name |
|
128
|
|
|
* |
|
129
|
|
|
* @return mixed |
|
130
|
|
|
*/ |
|
131
|
1 |
|
public function getConfig(string $name) |
|
132
|
|
|
{ |
|
133
|
1 |
|
return $this->config[$name]; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* get fields |
|
138
|
|
|
* |
|
139
|
|
|
* @return array |
|
140
|
|
|
*/ |
|
141
|
2 |
|
protected function getFields(): array |
|
142
|
|
|
{ |
|
143
|
|
|
return [ |
|
144
|
2 |
|
'name', |
|
145
|
|
|
'consumerId', |
|
146
|
|
|
'config', |
|
147
|
|
|
]; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* to request parameters |
|
152
|
|
|
* |
|
153
|
|
|
* @return array |
|
154
|
|
|
*/ |
|
155
|
2 |
|
public function toRequestParameters(): array |
|
156
|
|
|
{ |
|
157
|
2 |
|
$requestParameters = []; |
|
158
|
|
|
|
|
159
|
2 |
|
foreach (array_merge($this->getFields(), self::DEFAULT_FIELDS) as $field) { |
|
160
|
2 |
|
if (!is_null($value = $this->$field)) { |
|
161
|
2 |
|
if (is_array($value)) { |
|
162
|
2 |
|
foreach (array_keys($value) as $key) { |
|
163
|
2 |
|
$requestParameters[$this->toSnakeCase(sprintf('%1$s.%2$s', $field, $key))] = $value[$key]; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
2 |
|
continue; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
2 |
|
$requestParameters[$this->toSnakeCase($field)] = $this->$field; |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
2 |
|
return $requestParameters; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|