|
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
|
|
|
|
|
12
|
|
|
class Context implements Arrayable |
|
13
|
|
|
{ |
|
14
|
|
|
private $channel; |
|
15
|
|
|
private $chat; |
|
16
|
|
|
private $user; |
|
17
|
|
|
private $intent; |
|
18
|
|
|
private $interaction; |
|
19
|
|
|
private $items; |
|
20
|
|
|
|
|
21
|
8 |
|
public function __construct(Channel $channel, Chat $chat, User $user, array $items = []) |
|
22
|
|
|
{ |
|
23
|
8 |
|
$this->channel = $channel; |
|
24
|
8 |
|
$this->chat = $chat; |
|
25
|
8 |
|
$this->user = $user; |
|
26
|
8 |
|
$this->items = collect($items); |
|
27
|
8 |
|
} |
|
28
|
|
|
|
|
29
|
4 |
|
public function getChannel(): Channel |
|
30
|
|
|
{ |
|
31
|
4 |
|
return $this->channel; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
4 |
|
public function getChat(): Chat |
|
35
|
|
|
{ |
|
36
|
4 |
|
return $this->chat; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
4 |
|
public function getUser(): User |
|
40
|
|
|
{ |
|
41
|
4 |
|
return $this->user; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
2 |
|
public function getIntent(): ?Intent |
|
45
|
|
|
{ |
|
46
|
2 |
|
return $this->intent; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
2 |
|
public function setIntent(Intent $intent): Context |
|
50
|
|
|
{ |
|
51
|
2 |
|
$this->intent = $intent; |
|
52
|
|
|
|
|
53
|
2 |
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
2 |
|
public function getInteraction(): ?Interaction |
|
57
|
|
|
{ |
|
58
|
2 |
|
return $this->interaction; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
2 |
|
public function setInteraction(?Interaction $interaction): Context |
|
62
|
|
|
{ |
|
63
|
2 |
|
$this->interaction = $interaction; |
|
64
|
|
|
|
|
65
|
2 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
4 |
|
public function get(string $key, $default = null) |
|
69
|
|
|
{ |
|
70
|
4 |
|
return $this->items->get($key, $default); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
2 |
|
public function set(string $key, $value): Context |
|
74
|
|
|
{ |
|
75
|
2 |
|
$this->items->put($key, $value); |
|
76
|
|
|
|
|
77
|
2 |
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
2 |
|
public function toArray(): array |
|
81
|
|
|
{ |
|
82
|
|
|
return [ |
|
83
|
2 |
|
'intent' => $this->intent ? get_class($this->intent) : null, |
|
84
|
2 |
|
'interaction' => $this->interaction ? get_class($this->interaction) : null, |
|
85
|
2 |
|
'items' => $this->items->toArray(), |
|
86
|
|
|
]; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|