1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace FondBot\Conversation; |
6
|
|
|
|
7
|
|
|
use FondBot\Channels\Chat; |
8
|
|
|
use FondBot\Channels\User; |
9
|
|
|
use FondBot\Channels\Channel; |
10
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
11
|
|
|
use FondBot\Contracts\Conversation\Conversable; |
12
|
|
|
|
13
|
|
|
class Context implements Arrayable |
14
|
|
|
{ |
15
|
|
|
private $channel; |
16
|
|
|
private $chat; |
17
|
|
|
private $user; |
18
|
|
|
private $intent; |
19
|
|
|
private $interaction; |
20
|
|
|
private $items; |
21
|
|
|
|
22
|
3 |
|
public function __construct(Channel $channel, Chat $chat, User $user, array $items = []) |
23
|
|
|
{ |
24
|
3 |
|
$this->channel = $channel; |
25
|
3 |
|
$this->chat = $chat; |
26
|
3 |
|
$this->user = $user; |
27
|
3 |
|
$this->items = collect($items); |
28
|
3 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get channel. |
32
|
|
|
* |
33
|
|
|
* @return Channel |
34
|
|
|
*/ |
35
|
1 |
|
public function getChannel(): Channel |
36
|
|
|
{ |
37
|
1 |
|
return $this->channel; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get chat. |
42
|
|
|
* |
43
|
|
|
* @return Chat |
44
|
|
|
*/ |
45
|
1 |
|
public function getChat(): Chat |
46
|
|
|
{ |
47
|
1 |
|
return $this->chat; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get user. |
52
|
|
|
* |
53
|
|
|
* @return User |
54
|
|
|
*/ |
55
|
1 |
|
public function getUser(): User |
56
|
|
|
{ |
57
|
1 |
|
return $this->user; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get current intent. |
62
|
|
|
* |
63
|
|
|
* @return Intent|Conversable|null |
64
|
|
|
*/ |
65
|
1 |
|
public function getIntent(): ?Intent |
66
|
|
|
{ |
67
|
1 |
|
return $this->intent; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set intent. |
72
|
|
|
* |
73
|
|
|
* @param Intent $intent |
74
|
|
|
*/ |
75
|
|
|
public function setIntent(Intent $intent): void |
76
|
|
|
{ |
77
|
|
|
$this->intent = $intent; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get interaction. |
82
|
|
|
* |
83
|
|
|
* @return Interaction|Conversable|null |
84
|
|
|
*/ |
85
|
1 |
|
public function getInteraction(): ?Interaction |
86
|
|
|
{ |
87
|
1 |
|
return $this->interaction; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Set interaction. |
92
|
|
|
* |
93
|
|
|
* @param Interaction|null $interaction |
94
|
|
|
*/ |
95
|
|
|
public function setInteraction(?Interaction $interaction): void |
96
|
|
|
{ |
97
|
|
|
$this->interaction = $interaction; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get item. |
102
|
|
|
* |
103
|
|
|
* @param string $key |
104
|
|
|
* @param null $default |
105
|
|
|
* |
106
|
|
|
* @return mixed |
107
|
|
|
*/ |
108
|
3 |
|
public function get(string $key, $default = null) |
109
|
|
|
{ |
110
|
3 |
|
return $this->items->get($key, $default); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set item. |
115
|
|
|
* |
116
|
|
|
* @param string $key |
117
|
|
|
* @param mixed $value |
118
|
|
|
*/ |
119
|
2 |
|
public function set(string $key, $value): void |
120
|
|
|
{ |
121
|
2 |
|
$this->items->put($key, $value); |
122
|
2 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get the instance as an array. |
126
|
|
|
* |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
1 |
|
public function toArray(): array |
130
|
|
|
{ |
131
|
|
|
return [ |
132
|
1 |
|
'intent' => $this->intent ? get_class($this->intent) : null, |
133
|
1 |
|
'interaction' => $this->interaction ? get_class($this->interaction) : null, |
134
|
1 |
|
'items' => $this->items->toArray(), |
135
|
|
|
]; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|