|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace FondBot\Conversation\Traits; |
|
6
|
|
|
|
|
7
|
|
|
use RuntimeException; |
|
8
|
|
|
use InvalidArgumentException; |
|
9
|
|
|
use FondBot\Application\Kernel; |
|
10
|
|
|
use FondBot\Conversation\Intent; |
|
11
|
|
|
use FondBot\Conversation\Interaction; |
|
12
|
|
|
|
|
13
|
|
|
trait Transitions |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var Kernel */ |
|
16
|
|
|
protected $kernel; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Whether any transition run. |
|
20
|
|
|
* |
|
21
|
|
|
* @var bool |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $transitioned = false; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Jump to another interaction. |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $interaction |
|
29
|
|
|
* |
|
30
|
|
|
* @throws \InvalidArgumentException |
|
31
|
|
|
*/ |
|
32
|
3 |
|
protected function jump(string $interaction): void |
|
33
|
|
|
{ |
|
34
|
|
|
/** @var Interaction $instance */ |
|
35
|
3 |
|
$instance = $this->kernel->resolve($interaction); |
|
36
|
|
|
|
|
37
|
3 |
|
if (!$instance instanceof Interaction) { |
|
38
|
2 |
|
throw new InvalidArgumentException('Invalid interaction `'.$interaction.'`'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
// Run interaction |
|
42
|
1 |
|
$this->kernel->converse($instance); |
|
43
|
|
|
|
|
44
|
1 |
|
$this->transitioned = true; |
|
45
|
1 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Restart current intent or interaction. |
|
49
|
|
|
*/ |
|
50
|
3 |
|
protected function restart(): void |
|
51
|
|
|
{ |
|
52
|
|
|
switch (true) { |
|
53
|
3 |
|
case $this instanceof Intent: |
|
54
|
1 |
|
$this->kernel->clearContext(); |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
1 |
|
$this->kernel->converse($this); |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
1 |
|
$this->transitioned = true; |
|
|
|
|
|
|
59
|
1 |
|
break; |
|
60
|
1 |
|
case $this instanceof Interaction: |
|
61
|
1 |
|
$context = $this->kernel->getContext(); |
|
|
|
|
|
|
62
|
1 |
|
$context->setInteraction(null); |
|
63
|
1 |
|
$context->setValues([]); |
|
64
|
1 |
|
$this->kernel->setContext($context); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
1 |
|
$this->transitioned = true; |
|
|
|
|
|
|
67
|
|
|
|
|
68
|
1 |
|
$this->kernel->converse($this); |
|
|
|
|
|
|
69
|
1 |
|
break; |
|
70
|
|
|
default: |
|
71
|
1 |
|
throw new RuntimeException('Only conversable instances can be restarted.'); |
|
72
|
|
|
} |
|
73
|
2 |
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
This check looks for access to properties that are not accessible from the current context.
If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.