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 $type |
|
|
|
|
35
|
|
|
* @param string|Attachment $data |
36
|
|
|
*/ |
37
|
18 |
|
public function __construct($data) |
38
|
|
|
{ |
39
|
18 |
|
if (is_string($data)) { |
40
|
12 |
|
$this->type = self::TYPE_TEXT; |
41
|
18 |
|
} elseif ($data instanceOf Attachment) { |
42
|
5 |
|
$this->type = self::TYPE_ATTACHMENT; |
43
|
5 |
|
} else { |
44
|
1 |
|
throw new \InvalidArgumentException('Invalid $data. It must be a string or an Attachment'); |
45
|
|
|
} |
46
|
|
|
|
47
|
17 |
|
$this->data = $data; |
48
|
17 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
1 |
|
public function getType() |
54
|
|
|
{ |
55
|
1 |
|
return $this->type; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return string|Attachment |
60
|
|
|
*/ |
61
|
4 |
|
public function getData() |
62
|
|
|
{ |
63
|
4 |
|
return $this->data; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return null|QuickReply[] |
68
|
|
|
*/ |
69
|
3 |
|
public function getQuickReplies() |
70
|
|
|
{ |
71
|
3 |
|
return $this->quickReplies; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return null|string |
76
|
|
|
*/ |
77
|
2 |
|
public function getMetadata() |
78
|
|
|
{ |
79
|
2 |
|
return $this->metadata; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param null|QuickReply[] $quickReplies |
84
|
|
|
*/ |
85
|
4 |
|
public function setQuickReplies(array $quickReplies = null) |
86
|
|
|
{ |
87
|
4 |
|
if (count($quickReplies) > 10) { |
88
|
1 |
|
throw new \InvalidArgumentException('A message can not have more than 10 quick replies.'); |
89
|
|
|
} |
90
|
|
|
|
91
|
3 |
|
$this->quickReplies = empty($quickReplies) ? null : $quickReplies; |
92
|
3 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param QuickReply $quickReplies |
|
|
|
|
96
|
|
|
*/ |
97
|
3 |
|
public function addQuickReply(QuickReply $quickReply) |
98
|
|
|
{ |
99
|
3 |
|
if (count($this->quickReplies) >= 10) { |
100
|
1 |
|
throw new \InvalidArgumentException('A message can not have more than 10 quick replies.'); |
101
|
|
|
} |
102
|
|
|
|
103
|
2 |
|
if (!is_array($this->quickReplies)) { |
104
|
1 |
|
$this->quickReplies = []; |
105
|
1 |
|
} |
106
|
|
|
|
107
|
2 |
|
$this->quickReplies[] = $quickReply; |
108
|
2 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param null|string $metadata |
112
|
|
|
*/ |
113
|
3 |
|
public function setMetadata($metadata = null) |
114
|
|
|
{ |
115
|
3 |
|
if ($metadata !== null && mb_strlen($metadata) > 1000) { |
116
|
1 |
|
throw new \InvalidArgumentException('$metadata should not exceed 1000 characters.'); |
117
|
|
|
} |
118
|
|
|
|
119
|
2 |
|
$this->metadata = empty($metadata) ? null : $metadata; |
120
|
2 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Check if the message contains file |
124
|
|
|
* |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
5 |
|
public function hasFileToUpload() |
128
|
|
|
{ |
129
|
5 |
|
if (!$this->data instanceof File) { |
130
|
4 |
|
return false; |
131
|
|
|
} |
132
|
|
|
|
133
|
1 |
|
return !$this->data->isRemoteFile(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Return a stream of the local file attachment |
138
|
|
|
* |
139
|
|
|
* @return resource|null |
140
|
|
|
*/ |
141
|
1 |
|
public function getFileStream() |
142
|
|
|
{ |
143
|
1 |
|
if (!$this->data instanceof File) { |
144
|
|
|
return null; |
145
|
|
|
} |
146
|
|
|
|
147
|
1 |
|
return $this->data->getStream(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
2 |
|
public function jsonSerialize() |
154
|
|
|
{ |
155
|
|
|
return [ |
156
|
2 |
|
$this->type => $this->data, |
157
|
2 |
|
'quick_replies' => $this->quickReplies, |
158
|
2 |
|
'metadata' => $this->metadata, |
159
|
2 |
|
]; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.