1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Push notification services abstraction (http://github.com/juliangut/tify) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/juliangut/tify for the canonical source repository |
6
|
|
|
* |
7
|
|
|
* @license https://github.com/juliangut/tify/blob/master/LICENSE |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Jgut\Tify; |
11
|
|
|
|
12
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Push message. |
16
|
|
|
*/ |
17
|
|
|
class Message |
18
|
|
|
{ |
19
|
|
|
use ParameterTrait; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Default message parameters. |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $defaultParameters = [ |
27
|
|
|
'title' => null, |
28
|
|
|
'body' => null, |
29
|
|
|
|
30
|
|
|
// APNS |
31
|
|
|
//'launch_image' => null, |
32
|
|
|
//'action_loc_key' => null, |
33
|
|
|
//'title_loc_key' => null, |
34
|
|
|
//'title_loc_args' => null, |
35
|
|
|
//'loc_key' => null, |
36
|
|
|
//'loc_args' => null, |
37
|
|
|
|
38
|
|
|
// GCM |
39
|
|
|
//'icon' => null, |
40
|
|
|
//'sound' => 'default', |
41
|
|
|
//'tag' => null, |
42
|
|
|
//'color' => '#rrggbb', |
43
|
|
|
//'click_action' => null, |
44
|
|
|
//'title_loc_key' => null, |
45
|
|
|
//'title_loc_args' => null, |
46
|
|
|
//'body_loc_key' => null, |
47
|
|
|
//'body_loc_args' => null, |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Reserved payload keys. |
52
|
|
|
* |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
protected static $reservedKeyRegex = [ |
56
|
|
|
// APNS |
57
|
|
|
'/^apc$/', |
58
|
|
|
|
59
|
|
|
// GCM |
60
|
|
|
'/^(google|gcm)/', |
61
|
|
|
'/^from$/', |
62
|
|
|
'/^collapse_key$/', |
63
|
|
|
'/^delay_while_idle$/', |
64
|
|
|
'/^time_to_live$/', |
65
|
|
|
'/^restricted_package_name$/', |
66
|
|
|
'/^dry_run$/', |
67
|
|
|
'/^priority$/', |
68
|
|
|
'/^content_available$/', |
69
|
|
|
]; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Payload prefix |
73
|
|
|
* |
74
|
|
|
* @var string |
75
|
|
|
*/ |
76
|
|
|
protected $payloadPrefix = 'data_'; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Message payload. |
80
|
|
|
* |
81
|
|
|
* @var \Doctrine\Common\Collections\ArrayCollection |
82
|
|
|
*/ |
83
|
|
|
protected $payload; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Constructor. |
87
|
|
|
* |
88
|
|
|
* @param array $parameters |
89
|
|
|
*/ |
90
|
|
|
public function __construct(array $parameters = []) |
91
|
|
|
{ |
92
|
|
|
$this->setParameters(array_merge($this->defaultParameters, $parameters)); |
93
|
|
|
|
94
|
|
|
$this->payload = new ArrayCollection; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Convenience method to set message title. |
99
|
|
|
* |
100
|
|
|
* @param string $title |
101
|
|
|
* |
102
|
|
|
* @return $this |
103
|
|
|
*/ |
104
|
|
|
public function setTitle($title) |
105
|
|
|
{ |
106
|
|
|
$this->setParameter('title', $title); |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Convenience method to set message body. |
113
|
|
|
* |
114
|
|
|
* @param string $body |
115
|
|
|
* |
116
|
|
|
* @return $this |
117
|
|
|
*/ |
118
|
|
|
public function setBody($body) |
119
|
|
|
{ |
120
|
|
|
$this->setParameter('body', $body); |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Retrieve payload prefix. |
127
|
|
|
* |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
public function getPayloadPrefix() |
131
|
|
|
{ |
132
|
|
|
return $this->payloadPrefix; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Set payload prefix. |
137
|
|
|
* |
138
|
|
|
* @param string $prefix |
139
|
|
|
* |
140
|
|
|
* @return $this |
141
|
|
|
*/ |
142
|
|
|
public function setPayloadPrefix($prefix) |
143
|
|
|
{ |
144
|
|
|
$this->payloadPrefix = trim($prefix); |
145
|
|
|
|
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get payload data. |
151
|
|
|
* |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
|
|
public function getPayloadData() |
155
|
|
|
{ |
156
|
|
|
return $this->payload->toArray(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Set payload data. |
161
|
|
|
* |
162
|
|
|
* @param array $data |
163
|
|
|
* |
164
|
|
|
* @throws \InvalidArgumentException |
165
|
|
|
* |
166
|
|
|
* @return $this |
167
|
|
|
*/ |
168
|
|
|
public function setPayloadData(array $data) |
169
|
|
|
{ |
170
|
|
|
$this->payload->clear(); |
171
|
|
|
|
172
|
|
|
foreach ($data as $key => $value) { |
173
|
|
|
$this->setPayload($key, $value); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Has payload data. |
181
|
|
|
* |
182
|
|
|
* @param string $key |
183
|
|
|
* |
184
|
|
|
* @throws \InvalidArgumentException |
185
|
|
|
* |
186
|
|
|
* @return bool |
187
|
|
|
*/ |
188
|
|
|
public function hasPayload($key) |
189
|
|
|
{ |
190
|
|
|
return $this->payload->containsKey($this->composePayloadKey($key)); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Get payload data. |
195
|
|
|
* |
196
|
|
|
* @param string $key |
197
|
|
|
* @param mixed $default |
198
|
|
|
* |
199
|
|
|
* @throws \InvalidArgumentException |
200
|
|
|
* |
201
|
|
|
* @return mixed |
202
|
|
|
*/ |
203
|
|
|
public function getPayload($key, $default = null) |
204
|
|
|
{ |
205
|
|
|
$key = $this->composePayloadKey($key); |
206
|
|
|
|
207
|
|
|
return $this->payload->containsKey($key) ? $this->payload->get($key) : $default; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Set payload data. |
212
|
|
|
* |
213
|
|
|
* @param string $key |
214
|
|
|
* @param mixed $value |
215
|
|
|
* |
216
|
|
|
* @throws \InvalidArgumentException |
217
|
|
|
* |
218
|
|
|
* @return $this |
219
|
|
|
*/ |
220
|
|
|
public function setPayload($key, $value) |
221
|
|
|
{ |
222
|
|
|
$key = $this->composePayloadKey($key); |
223
|
|
|
|
224
|
|
|
foreach (self::$reservedKeyRegex as $reservedKeyRegex) { |
225
|
|
|
if (preg_match($reservedKeyRegex, $key)) { |
226
|
|
|
throw new \InvalidArgumentException(sprintf( |
227
|
|
|
'"%s" can not be used as message payload key, starts or contains "%s"', |
228
|
|
|
$key, |
229
|
|
|
preg_replace('![/^$]!', '', $reservedKeyRegex) |
230
|
|
|
)); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
$this->payload->set($key, $value); |
235
|
|
|
|
236
|
|
|
return $this; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Compose payload key with prefix. |
241
|
|
|
* |
242
|
|
|
* @param string $key |
243
|
|
|
* |
244
|
|
|
* @throws \InvalidArgumentException |
245
|
|
|
* |
246
|
|
|
* @return string |
247
|
|
|
*/ |
248
|
|
|
protected function composePayloadKey($key) |
249
|
|
|
{ |
250
|
|
|
$key = trim($key); |
251
|
|
|
|
252
|
|
|
if ($key === '') { |
253
|
|
|
throw new \InvalidArgumentException('Payload parameter key can not be empty'); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
return $this->payloadPrefix . $key; |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|