1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the LineMob package. |
5
|
|
|
* |
6
|
|
|
* (c) Ishmael Doss <[email protected]> |
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 LineMob\Core\Command; |
13
|
|
|
|
14
|
|
|
use LineMob\Core\Input; |
15
|
|
|
use LineMob\Core\Storage\CommandDataInterface; |
16
|
|
|
use LineMob\Core\Template\TemplateInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @property boolean $active |
20
|
|
|
* @property string $logs |
21
|
|
|
* @property AbstractCommand|null $switchTo |
22
|
|
|
* |
23
|
|
|
* @author Ishmael Doss <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
abstract class AbstractCommand implements \ArrayAccess |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var string a persistent code |
29
|
|
|
*/ |
30
|
|
|
protected $code; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $name; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $description; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $cmd; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var CommandDataInterface |
49
|
|
|
*/ |
50
|
|
|
public $storage; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var Input |
54
|
|
|
*/ |
55
|
|
|
public $input; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var TemplateInterface |
59
|
|
|
*/ |
60
|
|
|
public $message; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
public $mode; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var string |
69
|
|
|
*/ |
70
|
|
|
public $to; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var string[] |
74
|
|
|
*/ |
75
|
|
|
public $tos; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var array |
79
|
|
|
*/ |
80
|
|
|
protected $data = []; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @var array |
84
|
|
|
*/ |
85
|
|
|
protected $logData = []; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param array $data |
89
|
|
|
*/ |
90
|
|
|
public function __construct(array $data = []) |
91
|
|
|
{ |
92
|
|
|
$this->setForzenParameters($data); |
93
|
|
|
|
94
|
|
|
$this->data = array_replace_recursive($this->data, $data); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param array $data |
99
|
|
|
*/ |
100
|
|
|
private function setForzenParameters(array &$data) |
101
|
|
|
{ |
102
|
|
|
foreach (['name', 'description', 'cmd'] as $value) { |
103
|
|
|
if (array_key_exists($value, $data)) { |
104
|
|
|
$this->$value = $data[$value]; |
105
|
|
|
unset($data[$value]); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function getCode() |
114
|
|
|
{ |
115
|
|
|
return $this->code; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
public function getName() |
122
|
|
|
{ |
123
|
|
|
return $this->name; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
public function getDescription() |
130
|
|
|
{ |
131
|
|
|
return $this->description; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
public function getCmd() |
138
|
|
|
{ |
139
|
|
|
return $this->cmd; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param array $data |
144
|
|
|
*/ |
145
|
|
|
public function merge(array $data) |
146
|
|
|
{ |
147
|
|
|
$this->data = array_replace_recursive($this->data, $data); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
|
|
public function offsetExists($offset) |
154
|
|
|
{ |
155
|
|
|
return array_key_exists($offset, $this->data); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
|
|
public function offsetGet($offset) |
162
|
|
|
{ |
163
|
|
|
if (!$this->offsetExists($offset)) { |
164
|
|
|
return null; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $this->data[$offset]; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* {@inheritdoc} |
172
|
|
|
*/ |
173
|
|
|
public function offsetSet($offset, $value) |
174
|
|
|
{ |
175
|
|
|
$this->data[$offset] = $value; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* {@inheritdoc} |
180
|
|
|
*/ |
181
|
|
|
public function offsetUnset($offset) |
182
|
|
|
{ |
183
|
|
|
unset($this->data[$offset]); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return array |
188
|
|
|
*/ |
189
|
|
|
public function getData() |
190
|
|
|
{ |
191
|
|
|
return $this->data; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* {@inheritdoc} |
196
|
|
|
*/ |
197
|
|
|
public function __get($name) |
198
|
|
|
{ |
199
|
|
|
if ('logs' === $name) { |
200
|
|
|
return $this->logData; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return $this->offsetGet($name); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* {@inheritdoc} |
208
|
|
|
*/ |
209
|
|
|
public function __set($name, $value) |
210
|
|
|
{ |
211
|
|
|
if ('logs' === $name) { |
212
|
|
|
if (empty($this->logData)) { |
213
|
|
|
$this->logData = []; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
array_push($this->logData, $value); |
217
|
|
|
|
218
|
|
|
return; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
$this->offsetSet($name, $value); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @param string $cmd |
226
|
|
|
* @return bool |
227
|
|
|
*/ |
228
|
|
|
public function supported($cmd) |
229
|
|
|
{ |
230
|
|
|
return $this->cmd === $cmd; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param string $commandClass |
235
|
|
|
* @param array $args |
236
|
|
|
* |
237
|
|
|
* @return AbstractCommand |
238
|
|
|
*/ |
239
|
|
|
public function switchTo($commandClass, array $args = []) |
240
|
|
|
{ |
241
|
|
|
$cmd = new $commandClass($args); |
242
|
|
|
$cmd->input = $this->input; |
243
|
|
|
$cmd->storage = $this->storage; |
244
|
|
|
$cmd->logs = $this->logs; |
245
|
|
|
$this->switchTo = $cmd; |
246
|
|
|
|
247
|
|
|
return $cmd; |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|