|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Mobile |
|
17 * @subpackage Zend_Mobile_Push |
|
18 * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 * @version $Id$ |
|
21 */ |
|
22 |
|
23 /** Zend_Mobile_Push_Message_Abstract **/ |
|
24 require_once 'Zend/Mobile/Push/Message/Abstract.php'; |
|
25 |
|
26 /** |
|
27 * Gcm Message |
|
28 * |
|
29 * @category Zend |
|
30 * @package Zend_Mobile |
|
31 * @subpackage Zend_Mobile_Push |
|
32 * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) |
|
33 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
34 * @version $Id$ |
|
35 * @method array getToken() |
|
36 */ |
|
37 class Zend_Mobile_Push_Message_Gcm extends Zend_Mobile_Push_Message_Abstract |
|
38 { |
|
39 |
|
40 /** |
|
41 * Tokens |
|
42 * |
|
43 * @var array |
|
44 */ |
|
45 protected $_token = array(); |
|
46 |
|
47 /** |
|
48 * Data key value pairs |
|
49 * |
|
50 * @var array |
|
51 */ |
|
52 protected $_data = array(); |
|
53 |
|
54 /** |
|
55 * Delay while idle |
|
56 * |
|
57 * @var boolean |
|
58 */ |
|
59 protected $_delay = false; |
|
60 |
|
61 /** |
|
62 * Time to live in seconds |
|
63 * |
|
64 * @var int |
|
65 */ |
|
66 protected $_ttl = 0; |
|
67 |
|
68 /** |
|
69 * Add a Token |
|
70 * |
|
71 * @param string $token |
|
72 * @return Zend_Mobile_Push_Message_Gcm |
|
73 * @throws Zend_Mobile_Push_Message_Exception |
|
74 */ |
|
75 public function addToken($token) |
|
76 { |
|
77 if (!is_string($token)) { |
|
78 throw new Zend_Mobile_Push_Message_Exception('$token must be a string'); |
|
79 } |
|
80 if (!in_array($token, $this->_token)) { |
|
81 $this->_token[] = $token; |
|
82 } |
|
83 return $this; |
|
84 } |
|
85 |
|
86 /** |
|
87 * Set Token |
|
88 * |
|
89 * @param string|array $token |
|
90 * @return Zend_Mobile_Push_Message_Gcm |
|
91 * @throws Zend_Mobile_Push_Message_Exception |
|
92 */ |
|
93 public function setToken($token) |
|
94 { |
|
95 $this->clearToken(); |
|
96 if (is_string($token)) { |
|
97 $this->addToken($token); |
|
98 } else if (is_array($token)) { |
|
99 foreach ($token as $t) { |
|
100 $this->addToken($t); |
|
101 } |
|
102 } |
|
103 return $this; |
|
104 } |
|
105 |
|
106 /** |
|
107 * Clear Tokens |
|
108 * |
|
109 * @return Zend_Mobile_Push_Message_Gcm |
|
110 */ |
|
111 public function clearToken() |
|
112 { |
|
113 $this->_token = array(); |
|
114 return $this; |
|
115 } |
|
116 |
|
117 |
|
118 /** |
|
119 * Add Data |
|
120 * |
|
121 * @param string $key |
|
122 * @param string $value |
|
123 * @return Zend_Mobile_Push_Message_Gcm |
|
124 * @throws Zend_Mobile_Push_Message_Exception |
|
125 */ |
|
126 public function addData($key, $value) |
|
127 { |
|
128 if (!is_string($key)) { |
|
129 throw new Zend_Mobile_Push_Message_Exception('$key is not a string'); |
|
130 } |
|
131 if (!is_scalar($value)) { |
|
132 throw new Zend_Mobile_Push_Message_Exception('$value is not a string'); |
|
133 } |
|
134 $this->_data[$key] = $value; |
|
135 return $this; |
|
136 } |
|
137 |
|
138 /** |
|
139 * Set Data |
|
140 * |
|
141 * @param array $data |
|
142 * @return Zend_Mobile_Push_Message_Gcm |
|
143 * @throws Zend_Mobile_Push_Message_Exception |
|
144 */ |
|
145 public function setData(array $data) |
|
146 { |
|
147 $this->clearData(); |
|
148 foreach ($data as $k => $v) { |
|
149 $this->addData($k, $v); |
|
150 } |
|
151 return $this; |
|
152 } |
|
153 |
|
154 /** |
|
155 * Clear Data |
|
156 * |
|
157 * @return Zend_Mobile_Push_Message_Gcm |
|
158 */ |
|
159 public function clearData() |
|
160 { |
|
161 $this->_data = array(); |
|
162 return $this; |
|
163 } |
|
164 |
|
165 /** |
|
166 * Get Data |
|
167 * |
|
168 * @return array |
|
169 */ |
|
170 public function getData() |
|
171 { |
|
172 return $this->_data; |
|
173 } |
|
174 |
|
175 /** |
|
176 * Set Delay While Idle |
|
177 * |
|
178 * @param boolean $delay |
|
179 * @return Zend_Mobile_Push_Message_Gcm |
|
180 * @throws Zend_Mobile_Push_Message_Exception |
|
181 */ |
|
182 public function setDelayWhileIdle($delay) |
|
183 { |
|
184 if (!is_bool($delay)) { |
|
185 throw new Zend_Mobile_Push_Message_Exception('$delay must be boolean'); |
|
186 } |
|
187 $this->_delay = $delay; |
|
188 return $this; |
|
189 } |
|
190 |
|
191 /** |
|
192 * Get Delay While Idle |
|
193 * |
|
194 * @return boolean |
|
195 */ |
|
196 public function getDelayWhileIdle() |
|
197 { |
|
198 return $this->_delay; |
|
199 } |
|
200 |
|
201 /** |
|
202 * Set time to live |
|
203 * If $secs is set to 0 it will be handled as |
|
204 * not being set. |
|
205 * |
|
206 * @param int $secs |
|
207 * @return Zend_Mobile_Push_Message_Gcm |
|
208 */ |
|
209 public function setTtl($secs) |
|
210 { |
|
211 if (!is_numeric($secs)) { |
|
212 throw new Zend_Mobile_Push_Message_Exception('$secs must be numeric'); |
|
213 } |
|
214 $this->_ttl = (int) $secs; |
|
215 return $this; |
|
216 } |
|
217 |
|
218 /** |
|
219 * Get time to live |
|
220 * |
|
221 * @return int |
|
222 */ |
|
223 public function getTtl() |
|
224 { |
|
225 return $this->_ttl; |
|
226 } |
|
227 |
|
228 /** |
|
229 * Validate this is a proper Gcm message |
|
230 * Does not validate size. |
|
231 * |
|
232 * @return boolean |
|
233 */ |
|
234 public function validate() |
|
235 { |
|
236 if (!is_array($this->_token) || empty($this->_token)) { |
|
237 return false; |
|
238 } |
|
239 if ($this->_ttl > 0 && |
|
240 (!is_scalar($this->_id) || |
|
241 strlen($this->_id) === 0)) { |
|
242 return false; |
|
243 } |
|
244 return true; |
|
245 } |
|
246 |
|
247 /** |
|
248 * To Json utility method |
|
249 * Takes the data and properly assigns it to |
|
250 * a json encoded array to match the Gcm format. |
|
251 * |
|
252 * @return string |
|
253 */ |
|
254 public function toJson() |
|
255 { |
|
256 $json = array(); |
|
257 if ($this->_token) { |
|
258 $json['registration_ids'] = $this->_token; |
|
259 } |
|
260 if ($this->_id) { |
|
261 $json['collapse_key'] = (string) $this->_id; |
|
262 } |
|
263 if ($this->_data) { |
|
264 $json['data'] = $this->_data; |
|
265 } |
|
266 if ($this->_delay) { |
|
267 $json['delay_while_idle'] = $this->_delay; |
|
268 } |
|
269 if ($this->_ttl) { |
|
270 $json['time_to_live'] = $this->_ttl; |
|
271 } |
|
272 return json_encode($json); |
|
273 } |
|
274 } |