1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright 2015 Alexey Maslov <[email protected]> |
4
|
|
|
* |
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
6
|
|
|
* you may not use this file except in compliance with the License. |
7
|
|
|
* You may obtain a copy of the License at |
8
|
|
|
* |
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
10
|
|
|
* |
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14
|
|
|
* See the License for the specific language governing permissions and |
15
|
|
|
* limitations under the License. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace alxmsl\Google\GCM\Message; |
19
|
|
|
|
20
|
|
|
use alxmsl\Google\GCM\Exception\GCMRegistrationIdsIncorrectForMessageType; |
21
|
|
|
use alxmsl\Google\GCM\Exportable; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class for payload GCM message |
25
|
|
|
* @author alxmsl |
26
|
|
|
* @date 5/26/13 |
27
|
|
|
*/ |
28
|
|
|
class PayloadMessage implements Exportable { |
29
|
|
|
/** |
30
|
|
|
* GCM messages content types constants |
31
|
|
|
*/ |
32
|
|
|
const TYPE_PLAIN = 0, |
33
|
|
|
TYPE_JSON = 1; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Maximum registration ids for multicast messages |
37
|
|
|
*/ |
38
|
|
|
const COUNT_REGISTRATION_IDS = 1000; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var null|array|string registration id or ids |
42
|
|
|
*/ |
43
|
|
|
private $registrationIds = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var int content type code |
47
|
|
|
*/ |
48
|
|
|
private $type = self::TYPE_PLAIN; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var bool do not send message when device is idle |
52
|
|
|
*/ |
53
|
|
|
private $delayWhileIdle = false; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var int TTL for the message, seconds |
57
|
|
|
*/ |
58
|
|
|
private $timeToLive = 0; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var string package name for delivery registration ids limitation |
62
|
|
|
*/ |
63
|
|
|
private $restrictedPackageName = ''; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var bool need dry run sending for the message |
67
|
|
|
*/ |
68
|
|
|
private $dryRun = false; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var null|PayloadData message payload data |
72
|
|
|
*/ |
73
|
|
|
private $Data = null; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Content type code setter |
77
|
|
|
* @param int $type content type code |
78
|
|
|
* @return PayloadMessage self |
79
|
|
|
* @throws GCMRegistrationIdsIncorrectForMessageType when registration ids array was empty |
80
|
|
|
*/ |
81
|
5 |
|
public function setType($type) { |
82
|
5 |
|
$registrationIds = $this->getRegistrationIds(); |
83
|
5 |
|
$this->type = $type; |
84
|
5 |
|
switch ($this->type) { |
85
|
5 |
|
case self::TYPE_PLAIN: |
86
|
4 |
|
if (is_array($registrationIds)) { |
87
|
3 |
|
if (count($registrationIds) <= 1) { |
88
|
3 |
|
$this->registrationIds = reset($registrationIds); |
89
|
3 |
|
} else { |
90
|
1 |
|
throw new GCMRegistrationIdsIncorrectForMessageType('cannot use multiple registration ids for plain text message'); |
91
|
|
|
} |
92
|
3 |
|
} |
93
|
4 |
|
break; |
94
|
5 |
|
case self::TYPE_JSON: |
95
|
4 |
|
$this->registrationIds = (array) $registrationIds; |
96
|
4 |
|
break; |
97
|
5 |
|
} |
98
|
5 |
|
if (!is_null($this->getData())) { |
99
|
1 |
|
$this->getData()->setType($this->getType()); |
100
|
1 |
|
} |
101
|
5 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Content type code getter |
106
|
|
|
* @return int content type code |
107
|
|
|
*/ |
108
|
6 |
|
public function getType() { |
109
|
6 |
|
return $this->type; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Set needing delay while idle |
114
|
|
|
* @param bool $delayWhileIdle need delay while idle |
115
|
|
|
* @return PayloadMessage self |
116
|
|
|
*/ |
117
|
1 |
|
public function setDelayWhileIdle($delayWhileIdle) { |
118
|
1 |
|
$this->delayWhileIdle = (bool) $delayWhileIdle; |
119
|
1 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Need delay while idle or not |
124
|
|
|
* @return bool need delay while idle or not |
125
|
|
|
*/ |
126
|
5 |
|
public function needDelayWhileIdle() { |
127
|
5 |
|
return $this->delayWhileIdle; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Dry run message delivery setter |
132
|
|
|
* @param boolean $dryRun need dry run message delivery or not |
133
|
|
|
* @return PayloadMessage self |
134
|
|
|
*/ |
135
|
1 |
|
public function setDryRun($dryRun) { |
136
|
1 |
|
$this->dryRun = (bool) $dryRun; |
137
|
1 |
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Dry run message delivery |
142
|
|
|
* @return boolean dry run or not |
143
|
|
|
*/ |
144
|
5 |
|
public function isDryRun() { |
145
|
5 |
|
return $this->dryRun; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Package name setter |
150
|
|
|
* @param string $restrictedPackageName package name |
151
|
|
|
* @return PayloadMessage self |
152
|
|
|
*/ |
153
|
1 |
|
public function setRestrictedPackageName($restrictedPackageName) { |
154
|
1 |
|
$this->restrictedPackageName = (string) $restrictedPackageName; |
155
|
1 |
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Package name getter |
160
|
|
|
* @return string package name |
161
|
|
|
*/ |
162
|
2 |
|
public function getRestrictedPackageName() { |
163
|
2 |
|
return $this->restrictedPackageName; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Has set package name or not |
168
|
|
|
* @return bool has package name or not |
169
|
|
|
*/ |
170
|
5 |
|
public function hasRestrictedPackageName() { |
171
|
5 |
|
return !empty($this->restrictedPackageName); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Registration id or ids setter |
176
|
|
|
* @param null|array|string $registrationIds registration id or ids |
177
|
|
|
* @return PayloadMessage self |
178
|
|
|
*/ |
179
|
3 |
|
public function setRegistrationIds($registrationIds) { |
180
|
3 |
|
switch ($this->getType()) { |
181
|
3 |
|
case self::TYPE_JSON: |
182
|
2 |
|
$this->registrationIds = (array) $registrationIds; |
183
|
2 |
|
break; |
184
|
3 |
|
case self::TYPE_PLAIN: |
185
|
3 |
|
$this->registrationIds = (string) $registrationIds; |
186
|
3 |
|
break; |
187
|
3 |
|
} |
188
|
3 |
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Registration id or ids getter |
193
|
|
|
* @return null|array|string registration id or ids for message delivery |
194
|
|
|
*/ |
195
|
8 |
|
public function getRegistrationIds() { |
196
|
8 |
|
return $this->registrationIds; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* TTL setter |
201
|
|
|
* @param int $timeToLive ttl value, seconds |
202
|
|
|
* @return PayloadMessage self |
203
|
|
|
*/ |
204
|
1 |
|
public function setTimeToLive($timeToLive) { |
205
|
1 |
|
$this->timeToLive = (int) $timeToLive; |
206
|
1 |
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* TTL getter |
211
|
|
|
* @return int ttl value |
212
|
|
|
*/ |
213
|
5 |
|
public function getTimeToLive() { |
214
|
5 |
|
return $this->timeToLive; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Payload data setter |
219
|
|
|
* @param PayloadData $Data payload data instance |
220
|
|
|
* @return PayloadMessage self |
221
|
|
|
*/ |
222
|
2 |
|
public function setData(PayloadData $Data) { |
223
|
2 |
|
$this->Data = $Data; |
224
|
2 |
|
$this->Data->setType($this->getType()); |
225
|
2 |
|
return $this; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Payload data getter |
230
|
|
|
* @return null|PayloadData payload data |
231
|
|
|
*/ |
232
|
8 |
|
public function getData() { |
233
|
8 |
|
return $this->Data; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Method for export instance data |
238
|
|
|
* @return array exported data |
239
|
|
|
*/ |
240
|
5 |
|
public function export() { |
241
|
5 |
|
$data = array(); |
242
|
5 |
|
if ($this->getRegistrationIds()) { |
243
|
2 |
|
switch ($this->getType()) { |
244
|
2 |
|
case self::TYPE_JSON: |
245
|
1 |
|
$data['registration_ids'] = (array) $this->getRegistrationIds(); |
246
|
1 |
|
break; |
247
|
1 |
|
case self::TYPE_PLAIN: |
248
|
1 |
|
$data['registration_id'] = $this->getRegistrationIds(); |
249
|
1 |
|
break; |
250
|
2 |
|
} |
251
|
2 |
|
} |
252
|
5 |
|
$data['delay_while_idle'] = $this->needDelayWhileIdle(); |
253
|
5 |
|
$data['dry_run'] = $this->isDryRun(); |
254
|
5 |
|
if ($this->getTimeToLive() > 0) { |
255
|
1 |
|
$data['time_to_live'] = $this->getTimeToLive(); |
256
|
1 |
|
} |
257
|
5 |
|
if ($this->hasRestrictedPackageName()) { |
258
|
1 |
|
$data['restricted_package_name'] = $this->getRestrictedPackageName(); |
259
|
1 |
|
} |
260
|
5 |
|
if (!is_null($this->getData())) { |
261
|
1 |
|
$data += $this->getData()->export(); |
262
|
1 |
|
} |
263
|
5 |
|
return $data; |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|