PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/www/libs/Zend/Mobile/Push/Message/Gcm.php

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