1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tgallice\FBMessenger\Model; |
4
|
|
|
|
5
|
|
|
use Tgallice\FBMessenger\Model\Attachment\File; |
6
|
|
|
|
7
|
|
|
class Message implements \JsonSerializable |
8
|
|
|
{ |
9
|
|
|
const TYPE_TEXT = 'text'; |
10
|
|
|
|
11
|
|
|
const TYPE_ATTACHMENT = 'attachment'; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var string|Attachment |
15
|
|
|
*/ |
16
|
|
|
private $type; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string|Attachment |
20
|
|
|
*/ |
21
|
|
|
private $data; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var null|QuickReply[] |
25
|
|
|
*/ |
26
|
|
|
private $quickReplies; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var null|string |
30
|
|
|
*/ |
31
|
|
|
private $metadata; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string|Attachment $data |
35
|
|
|
*/ |
36
|
18 |
|
public function __construct($data) |
37
|
|
|
{ |
38
|
18 |
|
if (is_string($data)) { |
39
|
12 |
|
$this->type = self::TYPE_TEXT; |
40
|
18 |
|
} elseif ($data instanceOf Attachment) { |
41
|
5 |
|
$this->type = self::TYPE_ATTACHMENT; |
42
|
5 |
|
} else { |
43
|
1 |
|
throw new \InvalidArgumentException('Invalid $data. It must be a string or an Attachment'); |
44
|
|
|
} |
45
|
|
|
|
46
|
17 |
|
$this->data = $data; |
47
|
17 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
1 |
|
public function getType() |
53
|
|
|
{ |
54
|
1 |
|
return $this->type; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return string|Attachment |
59
|
|
|
*/ |
60
|
4 |
|
public function getData() |
61
|
|
|
{ |
62
|
4 |
|
return $this->data; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return null|QuickReply[] |
67
|
|
|
*/ |
68
|
3 |
|
public function getQuickReplies() |
69
|
|
|
{ |
70
|
3 |
|
return $this->quickReplies; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return null|string |
75
|
|
|
*/ |
76
|
2 |
|
public function getMetadata() |
77
|
|
|
{ |
78
|
2 |
|
return $this->metadata; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param null|QuickReply[] $quickReplies |
83
|
|
|
*/ |
84
|
4 |
|
public function setQuickReplies(array $quickReplies = null) |
85
|
|
|
{ |
86
|
4 |
|
if (count($quickReplies) > 10) { |
87
|
1 |
|
throw new \InvalidArgumentException('A message can not have more than 10 quick replies.'); |
88
|
|
|
} |
89
|
|
|
|
90
|
3 |
|
$this->quickReplies = empty($quickReplies) ? null : $quickReplies; |
91
|
3 |
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param QuickReply $quickReply |
95
|
|
|
*/ |
96
|
3 |
|
public function addQuickReply(QuickReply $quickReply) |
97
|
|
|
{ |
98
|
3 |
|
if (count($this->quickReplies) >= 10) { |
99
|
1 |
|
throw new \InvalidArgumentException('A message can not have more than 10 quick replies.'); |
100
|
|
|
} |
101
|
|
|
|
102
|
2 |
|
if (!is_array($this->quickReplies)) { |
103
|
1 |
|
$this->quickReplies = []; |
104
|
1 |
|
} |
105
|
|
|
|
106
|
2 |
|
$this->quickReplies[] = $quickReply; |
107
|
2 |
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param null|string $metadata |
111
|
|
|
*/ |
112
|
3 |
|
public function setMetadata($metadata = null) |
113
|
|
|
{ |
114
|
3 |
|
if ($metadata !== null && mb_strlen($metadata) > 1000) { |
115
|
1 |
|
throw new \InvalidArgumentException('$metadata should not exceed 1000 characters.'); |
116
|
|
|
} |
117
|
|
|
|
118
|
2 |
|
$this->metadata = empty($metadata) ? null : $metadata; |
119
|
2 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Check if the message contains file |
123
|
|
|
* |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
5 |
|
public function hasFileToUpload() |
127
|
|
|
{ |
128
|
5 |
|
if (!$this->data instanceof File) { |
129
|
4 |
|
return false; |
130
|
|
|
} |
131
|
|
|
|
132
|
1 |
|
return !$this->data->isRemoteFile(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Return a stream of the local file attachment |
137
|
|
|
* |
138
|
|
|
* @return resource|null |
139
|
|
|
*/ |
140
|
1 |
|
public function getFileStream() |
141
|
|
|
{ |
142
|
1 |
|
if (!$this->data instanceof File) { |
143
|
|
|
return null; |
144
|
|
|
} |
145
|
|
|
|
146
|
1 |
|
return $this->data->getStream(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return array |
151
|
|
|
*/ |
152
|
2 |
|
public function jsonSerialize() |
153
|
|
|
{ |
154
|
|
|
return [ |
155
|
2 |
|
$this->type => $this->data, |
156
|
2 |
|
'quick_replies' => $this->quickReplies, |
157
|
2 |
|
'metadata' => $this->metadata, |
158
|
2 |
|
]; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|