Gcm.php 6.1 KB

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