1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of Rivescript-php |
4
|
|
|
* |
5
|
|
|
* (c) Johnny Mast <[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 Axiom\Rivescript\Cortex\ResponseQueue; |
12
|
|
|
|
13
|
|
|
use Axiom\Rivescript\Cortex\Trigger; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* ResponseQueueItem class |
17
|
|
|
* |
18
|
|
|
* The ResponseQueueItem represents one response in the ResponseQueue. |
19
|
|
|
* |
20
|
|
|
* PHP version 7.4 and higher. |
21
|
|
|
* |
22
|
|
|
* @category Core |
23
|
|
|
* @package Cortext\ResponseQueue |
24
|
|
|
* @author Johnny Mast <[email protected]> |
25
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
26
|
|
|
* @link https://github.com/axiom-labs/rivescript-php |
27
|
|
|
* @since 0.4.0 |
28
|
|
|
*/ |
29
|
|
|
class ResponseQueueItem |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The command prefix. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
public string $command = ""; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The response string |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
public string $value = ""; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The original response string |
48
|
|
|
* |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
public string $original = ""; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The response type. |
55
|
|
|
* |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
public string $type = 'atomic'; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The sort order of the response. |
62
|
|
|
* |
63
|
|
|
* @var int |
64
|
|
|
*/ |
65
|
|
|
public int $order = 0; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Local parser options at this item. |
69
|
|
|
* |
70
|
|
|
* @var array<string, string> |
71
|
|
|
*/ |
72
|
|
|
public array $options = []; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Indicate the response changes topic. |
76
|
|
|
* |
77
|
|
|
* @var bool |
78
|
|
|
*/ |
79
|
|
|
private bool $topicIsChanged = false; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* This is the trigger for this response. |
83
|
|
|
* |
84
|
|
|
* @var string |
85
|
|
|
*/ |
86
|
|
|
public string $triggerString = ''; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* This is the topic for this response. |
90
|
|
|
* |
91
|
|
|
* @var string |
92
|
|
|
*/ |
93
|
|
|
public string $triggerTopic = ''; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Set a redirect target. |
97
|
|
|
* |
98
|
|
|
* @var string |
99
|
|
|
*/ |
100
|
|
|
protected string $redirect = ''; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* ResponseQueueItem constructor. |
104
|
|
|
* |
105
|
|
|
* @param string $command The command prefix. |
106
|
|
|
* @param string $value The command value. |
107
|
|
|
* @param string $type The type of response. |
108
|
|
|
* @param array<string,string> $options The local interpreter options. |
109
|
|
|
*/ |
110
|
|
|
public function __construct(string $command, string $value, string $type, Trigger $trigger, array $options = []) |
111
|
|
|
{ |
112
|
|
|
$this->command = $command; |
113
|
|
|
$this->value = $this->original = $value; |
114
|
|
|
$this->type = $type; |
115
|
|
|
$this->options = $options; |
116
|
|
|
$this->triggerString = $trigger->getText(); |
117
|
|
|
$this->triggerTopic = $trigger->getTopic(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Return the command string. |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public function getValue(): string |
126
|
|
|
{ |
127
|
|
|
return $this->value; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function setValue(string $value) |
131
|
|
|
{ |
132
|
|
|
$this->value = $value; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Return the command string. |
137
|
|
|
* |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
public function getCommand(): string |
141
|
|
|
{ |
142
|
|
|
return $this->command; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Return the trigger. |
147
|
|
|
* |
148
|
|
|
* @return array |
149
|
|
|
*/ |
150
|
|
|
public function getTrigger(): ?array |
151
|
|
|
{ |
152
|
|
|
return synapse()->brain->topic($this->triggerTopic)->triggers()->get($this->triggerString); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Return the order of this queue item. |
157
|
|
|
* |
158
|
|
|
* @return int |
159
|
|
|
*/ |
160
|
|
|
public function getOrder(): int |
161
|
|
|
{ |
162
|
|
|
return $this->order; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function isRedirect(): bool |
166
|
|
|
{ |
167
|
|
|
return ($this->getRedirect() !== ''); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function isPrevious(): bool |
171
|
|
|
{ |
172
|
|
|
return ($this->command == '%'); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function isTopicChanged(): bool |
176
|
|
|
{ |
177
|
|
|
return $this->topicIsChanged; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
private function reset(): void |
|
|
|
|
181
|
|
|
{ |
182
|
|
|
$this->source = $this->original; |
|
|
|
|
183
|
|
|
$this->topicIsChanged = false; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Parse the response through the available Tags. |
189
|
|
|
* |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
|
|
public function parse(): string |
193
|
|
|
{ |
194
|
|
|
// $this->reset(); |
195
|
|
|
|
196
|
|
|
$this->topicIsChanged = false; |
197
|
|
|
|
198
|
|
|
foreach (synapse()->tags as $tag) { |
|
|
|
|
199
|
|
|
$class = "\\Axiom\\Rivescript\\Cortex\\Tags\\{$tag}"; |
200
|
|
|
$instance = new $class("response"); |
201
|
|
|
|
202
|
|
|
$this->value = $instance->parse($this->value, synapse()->input); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
if ($this->triggerTopic !== $this->getTopic()) { |
206
|
|
|
$this->topicIsChanged = true; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
if ($this->command === '@') { |
210
|
|
|
$this->setRedirect($this->value); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return $this->value; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function setRedirect(string $redirect): void |
217
|
|
|
{ |
218
|
|
|
$this->redirect = $redirect; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function getRedirect(): string |
222
|
|
|
{ |
223
|
|
|
return $this->redirect; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function getTopic(): string |
227
|
|
|
{ |
228
|
|
|
return synapse()->memory->shortTerm()->get('topic') ?? "random"; |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.