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
|
|
|
private $attempts = 0; |
21
|
|
|
|
22
|
9 |
|
public function __construct(Channel $channel, Chat $chat, User $user, array $items = []) |
23
|
|
|
{ |
24
|
9 |
|
$this->channel = $channel; |
25
|
9 |
|
$this->chat = $chat; |
26
|
9 |
|
$this->user = $user; |
27
|
9 |
|
$this->items = collect($items); |
28
|
9 |
|
} |
29
|
|
|
|
30
|
4 |
|
public function getChannel(): Channel |
31
|
|
|
{ |
32
|
4 |
|
return $this->channel; |
33
|
|
|
} |
34
|
|
|
|
35
|
4 |
|
public function getChat(): Chat |
36
|
|
|
{ |
37
|
4 |
|
return $this->chat; |
38
|
|
|
} |
39
|
|
|
|
40
|
4 |
|
public function getUser(): User |
41
|
|
|
{ |
42
|
4 |
|
return $this->user; |
43
|
|
|
} |
44
|
|
|
|
45
|
2 |
|
public function getIntent(): ?Intent |
46
|
|
|
{ |
47
|
2 |
|
return $this->intent; |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
public function setIntent(Intent $intent): Context |
51
|
|
|
{ |
52
|
2 |
|
$this->intent = $intent; |
53
|
|
|
|
54
|
2 |
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
2 |
|
public function getInteraction(): ?Interaction |
58
|
|
|
{ |
59
|
2 |
|
return $this->interaction; |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
public function setInteraction(?Interaction $interaction): Context |
63
|
|
|
{ |
64
|
2 |
|
$this->interaction = $interaction; |
65
|
|
|
|
66
|
2 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
5 |
|
public function getItem(string $key, $default = null) |
70
|
|
|
{ |
71
|
5 |
|
return $this->items->get($key, $default); |
72
|
|
|
} |
73
|
|
|
|
74
|
3 |
|
public function setItem(string $key, $value): Context |
75
|
|
|
{ |
76
|
3 |
|
$this->items->put($key, $value); |
77
|
|
|
|
78
|
3 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function incrementAttempts(): Context |
82
|
|
|
{ |
83
|
|
|
$this->attempts++; |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function attempts(): int |
89
|
|
|
{ |
90
|
|
|
return $this->attempts; |
91
|
|
|
} |
92
|
|
|
|
93
|
2 |
|
public function toArray(): array |
94
|
|
|
{ |
95
|
|
|
return [ |
96
|
2 |
|
'intent' => $this->intent ? get_class($this->intent) : null, |
97
|
2 |
|
'interaction' => $this->interaction ? get_class($this->interaction) : null, |
98
|
2 |
|
'items' => $this->items->toArray(), |
99
|
2 |
|
'attempts' => $this->attempts, |
100
|
|
|
]; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|