1
|
|
|
<?php |
2
|
|
|
namespace Kerox\Messenger\Model; |
3
|
|
|
|
4
|
|
|
use Kerox\Messenger\Model\Message\Attachment; |
5
|
|
|
use Kerox\Messenger\Model\Message\QuickReply; |
6
|
|
|
use Kerox\Messenger\ValidatorTrait; |
7
|
|
|
|
8
|
|
|
class Message implements \JsonSerializable |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
use ValidatorTrait; |
12
|
|
|
|
13
|
|
|
const TYPE_TEXT = 'text'; |
14
|
|
|
const TYPE_ATTACHMENT = 'attachment'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $type = self::TYPE_TEXT; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \Kerox\Messenger\Model\Message\Attachment|string |
23
|
|
|
*/ |
24
|
|
|
protected $message; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $quickReplies = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $metadata; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Message constructor. |
38
|
|
|
* |
39
|
|
|
* @param \Kerox\Messenger\Model\Message\Attachment|string $message |
40
|
|
|
*/ |
41
|
|
|
public function __construct($message) |
42
|
|
|
{ |
43
|
|
|
if (is_string($message)) { |
44
|
|
|
$this->isValidString($message, 320); |
45
|
|
|
} elseif ($message instanceof Attachment) { |
46
|
|
|
$this->type = self::TYPE_ATTACHMENT; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->message = $message; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param mixed $quickReplies |
54
|
|
|
* @return \Kerox\Messenger\Model\Message |
55
|
|
|
* @throws \Exception |
56
|
|
|
*/ |
57
|
|
|
public function setQuickReplies(array $quickReplies): Message |
58
|
|
|
{ |
59
|
|
|
$this->isValidArray($quickReplies, 10); |
60
|
|
|
$this->quickReplies = $quickReplies; |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param \Kerox\Messenger\Model\Message\QuickReply $quickReply |
67
|
|
|
* @return \Kerox\Messenger\Model\Message |
68
|
|
|
*/ |
69
|
|
|
public function addQuickReply(QuickReply $quickReply): Message |
70
|
|
|
{ |
71
|
|
|
$this->quickReplies[] = $quickReply; |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param mixed $metadata |
78
|
|
|
* @return Message |
79
|
|
|
*/ |
80
|
|
|
public function setMetadata(string $metadata): Message |
81
|
|
|
{ |
82
|
|
|
$this->isValidString($metadata, 1000); |
83
|
|
|
$this->metadata = $metadata; |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
public function jsonSerialize(): array |
92
|
|
|
{ |
93
|
|
|
$json = [ |
94
|
|
|
$this->type => $this->message, |
95
|
|
|
'quick_replies' => $this->quickReplies, |
96
|
|
|
'metadata' => $this->metadata, |
97
|
|
|
]; |
98
|
|
|
|
99
|
|
|
return array_filter($json); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|