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
|
4 |
|
public function __construct(Channel $channel, Chat $chat, User $user, array $items = []) |
23
|
|
|
{ |
24
|
4 |
|
$this->channel = $channel; |
25
|
4 |
|
$this->chat = $chat; |
26
|
4 |
|
$this->user = $user; |
27
|
4 |
|
$this->items = collect($items); |
28
|
4 |
|
} |
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
|
2 |
|
public function getIntent(): ?Intent |
66
|
|
|
{ |
67
|
2 |
|
return $this->intent; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set intent. |
72
|
|
|
* |
73
|
|
|
* @param Intent $intent |
74
|
|
|
* |
75
|
|
|
* @return Context |
76
|
|
|
*/ |
77
|
1 |
|
public function setIntent(Intent $intent): Context |
78
|
|
|
{ |
79
|
1 |
|
$this->intent = $intent; |
80
|
|
|
|
81
|
1 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get interaction. |
86
|
|
|
* |
87
|
|
|
* @return Interaction|Conversable|null |
88
|
|
|
*/ |
89
|
2 |
|
public function getInteraction(): ?Interaction |
90
|
|
|
{ |
91
|
2 |
|
return $this->interaction; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Set interaction. |
96
|
|
|
* |
97
|
|
|
* @param Interaction|null $interaction |
98
|
|
|
* |
99
|
|
|
* @return Context |
100
|
|
|
*/ |
101
|
1 |
|
public function setInteraction(?Interaction $interaction): Context |
102
|
|
|
{ |
103
|
1 |
|
$this->interaction = $interaction; |
104
|
|
|
|
105
|
1 |
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get item. |
110
|
|
|
* |
111
|
|
|
* @param string $key |
112
|
|
|
* @param null $default |
113
|
|
|
* |
114
|
|
|
* @return mixed |
115
|
|
|
*/ |
116
|
3 |
|
public function get(string $key, $default = null) |
117
|
|
|
{ |
118
|
3 |
|
return $this->items->get($key, $default); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Set item. |
123
|
|
|
* |
124
|
|
|
* @param string $key |
125
|
|
|
* @param mixed $value |
126
|
|
|
* |
127
|
|
|
* @return Context |
128
|
|
|
*/ |
129
|
2 |
|
public function set(string $key, $value): Context |
130
|
|
|
{ |
131
|
2 |
|
$this->items->put($key, $value); |
132
|
|
|
|
133
|
2 |
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get the instance as an array. |
138
|
|
|
* |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
1 |
|
public function toArray(): array |
142
|
|
|
{ |
143
|
|
|
return [ |
144
|
1 |
|
'intent' => $this->intent ? get_class($this->intent) : null, |
145
|
1 |
|
'interaction' => $this->interaction ? get_class($this->interaction) : null, |
146
|
1 |
|
'items' => $this->items->toArray(), |
147
|
|
|
]; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|