|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tgallice\FBMessenger\Message; |
|
4
|
|
|
|
|
5
|
|
|
use Tgallice\FBMessenger\Attachment; |
|
6
|
|
|
use Tgallice\FBMessenger\Attachment\Image; |
|
7
|
|
|
use Tgallice\FBMessenger\NotificationType; |
|
8
|
|
|
|
|
9
|
|
|
class Message |
|
10
|
|
|
{ |
|
11
|
|
|
public static $recipient_value_type = 'id'; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
private $recipient; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var string|Attachment |
|
20
|
|
|
*/ |
|
21
|
|
|
private $messageData; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
private $quickReplies; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
private $notificationType; |
|
32
|
|
|
|
|
33
|
9 |
|
/** |
|
34
|
|
|
* @param string $recipientId Recipient Id |
|
35
|
9 |
|
* @param string|Attachment $messageData |
|
36
|
|
|
* @param array $quickReplies |
|
37
|
9 |
|
* @param string $notificationType |
|
38
|
1 |
|
*/ |
|
39
|
|
|
public function __construct($recipientId, $messageData, array $quickReplies = null, $notificationType = NotificationType::REGULAR) |
|
40
|
|
|
{ |
|
41
|
8 |
|
$this->recipient = $recipientId; |
|
42
|
8 |
|
|
|
43
|
8 |
|
if (is_string($messageData) && mb_strlen($messageData) > 320) { |
|
44
|
|
|
throw new \InvalidArgumentException('The text message should not exceed 320 characters'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
if(count($quickReplies) > 10) { |
|
48
|
1 |
|
throw new \InvalidArgumentException('The quick replies should not exceed 10 elements'); |
|
49
|
|
|
} |
|
50
|
1 |
|
|
|
51
|
|
|
$this->messageData = $messageData; |
|
52
|
|
|
$this->quickReplies = $quickReplies; |
|
|
|
|
|
|
53
|
|
|
$this->notificationType = $notificationType; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
2 |
|
/** |
|
57
|
|
|
* @return string |
|
58
|
2 |
|
*/ |
|
59
|
|
|
public function getRecipient() |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->recipient; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return string|Attachment |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getMessageData() |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->messageData; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
3 |
|
/** |
|
73
|
|
|
* @return array |
|
74
|
3 |
|
*/ |
|
75
|
|
|
public function getQuickReplies() |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->quickReplies; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return string |
|
82
|
2 |
|
*/ |
|
83
|
|
|
public function getNotificationType() |
|
84
|
2 |
|
{ |
|
85
|
|
|
return $this->notificationType; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
2 |
|
/** |
|
89
|
2 |
|
* @return bool |
|
90
|
|
|
*/ |
|
91
|
2 |
|
public function hasFileToUpload() |
|
92
|
2 |
|
{ |
|
93
|
2 |
|
return $this->messageData instanceof Image && !$this->messageData->isRemoteFile(); |
|
94
|
2 |
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Return the formatted message |
|
98
|
|
|
* |
|
99
|
|
|
* @return array |
|
100
|
|
|
*/ |
|
101
|
|
|
public function format() |
|
102
|
|
|
{ |
|
103
|
|
|
$messageType = is_string($this->messageData) ? 'text' : 'attachment'; |
|
104
|
|
|
|
|
105
|
|
|
$return = [ |
|
106
|
|
|
'recipient' => [ |
|
107
|
|
|
$this::$recipient_value_type => $this->recipient, |
|
108
|
|
|
], |
|
109
|
|
|
'message' => [ |
|
110
|
|
|
$messageType => $this->messageData, |
|
111
|
|
|
], |
|
112
|
|
|
'notification_type' => $this->notificationType, |
|
113
|
|
|
]; |
|
114
|
|
|
|
|
115
|
|
|
if($messageType === 'text' && count($this->quickReplies) > 0) { |
|
116
|
|
|
$return['quick_replies'] = $this->quickReplies; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $return; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.
To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.
The function can be called with either null or an array for the parameter
$needlebut will only accept an array as$haystack.