|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NotificationChannels\Flock; |
|
4
|
|
|
use NotificationChannels\Flock\Exceptions\CouldNotSendNotification; |
|
5
|
|
|
use Closure; |
|
6
|
|
|
|
|
7
|
|
|
class FlockMessage |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var array Parameters payload |
|
11
|
|
|
*/ |
|
12
|
|
|
public $payload = []; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @param string $content |
|
16
|
|
|
* |
|
17
|
|
|
* @return static |
|
18
|
|
|
*/ |
|
19
|
2 |
|
public static function create($content = '') |
|
20
|
|
|
{ |
|
21
|
2 |
|
return new static($content); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Message constructor. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $content |
|
28
|
|
|
*/ |
|
29
|
5 |
|
public function __construct($content = '') |
|
30
|
|
|
{ |
|
31
|
5 |
|
$this->content($content); |
|
32
|
5 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Notification message. |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $content |
|
38
|
|
|
* |
|
39
|
|
|
* @return $this |
|
40
|
|
|
*/ |
|
41
|
5 |
|
public function content($content) |
|
42
|
|
|
{ |
|
43
|
5 |
|
$this->payload['text'] = $content; |
|
44
|
|
|
|
|
45
|
5 |
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Message body as FlockML. |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $content |
|
52
|
|
|
* |
|
53
|
|
|
* @return $this |
|
54
|
|
|
*/ |
|
55
|
|
|
public function flockml($content) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->payload['flockml'] = $content; |
|
58
|
|
|
|
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Text to be shown as the message's notification. (Default is text.) |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $notification |
|
66
|
|
|
* |
|
67
|
|
|
* @return $this |
|
68
|
|
|
*/ |
|
69
|
|
|
public function notification($notification) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->payload['notification'] = $notification; |
|
72
|
|
|
|
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* This field is used this if the sender would want to display |
|
78
|
|
|
* another name and image as the sender. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $name |
|
81
|
|
|
* @param string $profileImage |
|
82
|
|
|
* |
|
83
|
|
|
* @return $this |
|
84
|
|
|
*/ |
|
85
|
|
|
public function sendAs($name, $profileImage = null) |
|
86
|
|
|
{ |
|
87
|
|
|
if (!is_string($name)) { |
|
88
|
|
|
throw CouldNotSendNotification::flockMessageException('Name should be string in sendAs field'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if ($profileImage === null) { |
|
92
|
|
|
throw CouldNotSendNotification::flockMessageException('Profile image URL in sendAs field'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if (! filter_var($profileImage, FILTER_VALIDATE_URL)) { |
|
96
|
|
|
throw CouldNotSendNotification::flockMessageException('Profile image URL is invalid'); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$this->payload['sendAs']['name'] = $name; |
|
100
|
|
|
$this->payload['sendAs']['profileImage'] = $profileImage; |
|
101
|
|
|
|
|
102
|
|
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Returns params payload. |
|
107
|
|
|
* |
|
108
|
|
|
* @return array |
|
109
|
|
|
*/ |
|
110
|
2 |
|
public function toArray() |
|
111
|
|
|
{ |
|
112
|
2 |
|
return $this->payload; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Define an attachment for the message. |
|
117
|
|
|
* |
|
118
|
|
|
* @param \Closure $callback |
|
119
|
|
|
* @return $this |
|
120
|
|
|
*/ |
|
121
|
|
|
public function attachments(Closure $callback) |
|
122
|
|
|
{ |
|
123
|
|
|
$this->payload['attachments'][] = $attachment = new FlockAttachment; |
|
124
|
|
|
|
|
125
|
|
|
$callback($attachment); |
|
126
|
|
|
|
|
127
|
|
|
return $this; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|