File indexing completed on 2025-03-02 05:29:36
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Mobile 0017 * @subpackage Zend_Mobile_Push 0018 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0019 * @license http://framework.zend.com/license/new-bsd New BSD License 0020 * @version $Id$ 0021 */ 0022 0023 /** Zend_Mobile_Push_Message_Abstract **/ 0024 // require_once 'Zend/Mobile/Push/Message/Abstract.php'; 0025 0026 /** 0027 * Gcm Message 0028 * 0029 * @category Zend 0030 * @package Zend_Mobile 0031 * @subpackage Zend_Mobile_Push 0032 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0033 * @license http://framework.zend.com/license/new-bsd New BSD License 0034 * @version $Id$ 0035 * @method array getToken() 0036 */ 0037 class Zend_Mobile_Push_Message_Gcm extends Zend_Mobile_Push_Message_Abstract 0038 { 0039 0040 /** 0041 * Tokens 0042 * 0043 * @var array 0044 */ 0045 protected $_token = array(); 0046 0047 /** 0048 * Data key value pairs 0049 * 0050 * @var array 0051 */ 0052 protected $_data = array(); 0053 0054 /** 0055 * Delay while idle 0056 * 0057 * @var boolean 0058 */ 0059 protected $_delay = false; 0060 0061 /** 0062 * Time to live in seconds 0063 * 0064 * @var int 0065 */ 0066 protected $_ttl = 2419200; 0067 0068 /** 0069 * Add a Token 0070 * 0071 * @param string $token 0072 * @return Zend_Mobile_Push_Message_Gcm 0073 * @throws Zend_Mobile_Push_Message_Exception 0074 */ 0075 public function addToken($token) 0076 { 0077 if (!is_string($token)) { 0078 throw new Zend_Mobile_Push_Message_Exception('$token must be a string'); 0079 } 0080 if (!in_array($token, $this->_token)) { 0081 $this->_token[] = $token; 0082 } 0083 return $this; 0084 } 0085 0086 /** 0087 * Set Token 0088 * 0089 * @param string|array $token 0090 * @return Zend_Mobile_Push_Message_Gcm 0091 * @throws Zend_Mobile_Push_Message_Exception 0092 */ 0093 public function setToken($token) 0094 { 0095 $this->clearToken(); 0096 if (is_string($token)) { 0097 $this->addToken($token); 0098 } else if (is_array($token)) { 0099 foreach ($token as $t) { 0100 $this->addToken($t); 0101 } 0102 } 0103 return $this; 0104 } 0105 0106 /** 0107 * Clear Tokens 0108 * 0109 * @return Zend_Mobile_Push_Message_Gcm 0110 */ 0111 public function clearToken() 0112 { 0113 $this->_token = array(); 0114 return $this; 0115 } 0116 0117 0118 /** 0119 * Add Data 0120 * 0121 * @param string $key 0122 * @param string $value 0123 * @return Zend_Mobile_Push_Message_Gcm 0124 * @throws Zend_Mobile_Push_Message_Exception 0125 */ 0126 public function addData($key, $value) 0127 { 0128 if (!is_string($key)) { 0129 throw new Zend_Mobile_Push_Message_Exception('$key is not a string'); 0130 } 0131 if (!is_scalar($value)) { 0132 throw new Zend_Mobile_Push_Message_Exception('$value is not a string'); 0133 } 0134 $this->_data[$key] = $value; 0135 return $this; 0136 } 0137 0138 /** 0139 * Set Data 0140 * 0141 * @param array $data 0142 * @return Zend_Mobile_Push_Message_Gcm 0143 * @throws Zend_Mobile_Push_Message_Exception 0144 */ 0145 public function setData(array $data) 0146 { 0147 $this->clearData(); 0148 foreach ($data as $k => $v) { 0149 $this->addData($k, $v); 0150 } 0151 return $this; 0152 } 0153 0154 /** 0155 * Clear Data 0156 * 0157 * @return Zend_Mobile_Push_Message_Gcm 0158 */ 0159 public function clearData() 0160 { 0161 $this->_data = array(); 0162 return $this; 0163 } 0164 0165 /** 0166 * Get Data 0167 * 0168 * @return array 0169 */ 0170 public function getData() 0171 { 0172 return $this->_data; 0173 } 0174 0175 /** 0176 * Set Delay While Idle 0177 * 0178 * @param boolean $delay 0179 * @return Zend_Mobile_Push_Message_Gcm 0180 * @throws Zend_Mobile_Push_Message_Exception 0181 */ 0182 public function setDelayWhileIdle($delay) 0183 { 0184 if (!is_bool($delay)) { 0185 throw new Zend_Mobile_Push_Message_Exception('$delay must be boolean'); 0186 } 0187 $this->_delay = $delay; 0188 return $this; 0189 } 0190 0191 /** 0192 * Get Delay While Idle 0193 * 0194 * @return boolean 0195 */ 0196 public function getDelayWhileIdle() 0197 { 0198 return $this->_delay; 0199 } 0200 0201 /** 0202 * Set time to live. 0203 * 0204 * @param int $secs 0205 * @throws Zend_Mobile_Push_Message_Exception 0206 * @return Zend_Mobile_Push_Message_Gcm 0207 */ 0208 public function setTtl($secs) 0209 { 0210 if (!is_numeric($secs)) { 0211 throw new Zend_Mobile_Push_Message_Exception('$secs must be numeric'); 0212 } 0213 $this->_ttl = (int) $secs; 0214 return $this; 0215 } 0216 0217 /** 0218 * Get time to live 0219 * 0220 * @return int 0221 */ 0222 public function getTtl() 0223 { 0224 return $this->_ttl; 0225 } 0226 0227 /** 0228 * Validate this is a proper Gcm message 0229 * Does not validate size. 0230 * 0231 * @return boolean 0232 */ 0233 public function validate() 0234 { 0235 if (!is_array($this->_token) || empty($this->_token)) { 0236 return false; 0237 } 0238 if ($this->_ttl !== 2419200 && 0239 (!is_scalar($this->_id) || 0240 strlen($this->_id) === 0)) { 0241 return false; 0242 } 0243 return true; 0244 } 0245 0246 /** 0247 * To Json utility method 0248 * Takes the data and properly assigns it to 0249 * a json encoded array to match the Gcm format. 0250 * 0251 * @return string 0252 */ 0253 public function toJson() 0254 { 0255 $json = array(); 0256 if ($this->_token) { 0257 $json['registration_ids'] = $this->_token; 0258 } 0259 if ($this->_id) { 0260 $json['collapse_key'] = (string) $this->_id; 0261 } 0262 if ($this->_data) { 0263 $json['data'] = $this->_data; 0264 } 0265 if ($this->_delay) { 0266 $json['delay_while_idle'] = $this->_delay; 0267 } 0268 if ($this->_ttl !== 2419200) { 0269 $json['time_to_live'] = $this->_ttl; 0270 } 0271 if (version_compare(PHP_VERSION, '5.4.0') >= 0) { 0272 return json_encode($json, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); 0273 } else { 0274 return json_encode($json); 0275 } 0276 } 0277 }