Apns.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. * Apns 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_Apns extends Zend_Mobile_Push_Message_Abstract
  35. {
  36. /**
  37. * Badge Number
  38. *
  39. * @var int
  40. */
  41. protected $_badge;
  42. /**
  43. * Alert
  44. *
  45. * @var array
  46. */
  47. protected $_alert = array();
  48. /**
  49. * Expiration
  50. *
  51. * @var int
  52. */
  53. protected $_expire;
  54. /**
  55. * Sound
  56. *
  57. * @var string
  58. */
  59. protected $_sound = 'default';
  60. /**
  61. * Custom Data
  62. *
  63. * @var array
  64. */
  65. protected $_custom = array();
  66. /**
  67. * Get Alert
  68. *
  69. * @return array
  70. */
  71. public function getAlert()
  72. {
  73. return $this->_alert;
  74. }
  75. /**
  76. * Set Alert
  77. *
  78. * @param string $text
  79. * @param string $actionLocKey
  80. * @param string $locKey
  81. * @param array $locArgs
  82. * @param string $launchImage
  83. * @return Zend_Mobile_Push_Message_Apns
  84. */
  85. public function setAlert($text, $actionLocKey=null, $locKey=null, $locArgs=null, $launchImage=null)
  86. {
  87. if ($text !== null && !is_string($text)) {
  88. throw new Zend_Mobile_Push_Message_Exception('$text must be a string');
  89. }
  90. if ($actionLocKey !== null && !is_string($actionLocKey)) {
  91. throw new Zend_Mobile_Push_Message_Exception('$actionLocKey must be a string');
  92. }
  93. if ($locKey !== null && !is_string($locKey)) {
  94. throw new Zend_Mobile_Push_Message_Exception('$locKey must be a string');
  95. }
  96. if ($locArgs !== null) {
  97. if (!is_array($locArgs)) {
  98. throw new Zend_Mobile_Push_Message_Exception('$locArgs must be an array of strings');
  99. } else {
  100. foreach ($locArgs as $str) {
  101. if (!is_string($str)) {
  102. throw new Zend_Mobile_Push_Message_Exception('$locArgs contains an item that is not a string');
  103. }
  104. }
  105. }
  106. }
  107. if (null !== $launchImage && !is_string($launchImage)) {
  108. throw new Zend_Mobile_Push_Message_Exception('$launchImage must be a string');
  109. }
  110. $this->_alert = array(
  111. 'body' => $text,
  112. 'action-loc-key' => $actionLocKey,
  113. 'loc-key' => $locKey,
  114. 'loc-args' => $locArgs,
  115. 'launch-image' => $launchImage,
  116. );
  117. return $this;
  118. }
  119. /**
  120. * Get Badge
  121. *
  122. * @return int
  123. */
  124. public function getBadge()
  125. {
  126. return $this->_badge;
  127. }
  128. /**
  129. * Set Badge
  130. *
  131. * @param int $badge
  132. * @return Zend_Mobile_Push_Message_Apns
  133. * @throws Zend_Mobile_Push_Message_Exception
  134. */
  135. public function setBadge($badge)
  136. {
  137. if (!is_null($badge) && !is_numeric($badge)) {
  138. throw new Zend_Mobile_Push_Message_Exception('$badge must be an integer');
  139. }
  140. if (!is_null($badge) && $badge < 0) {
  141. throw new Zend_Mobile_Push_Message_Exception('$badge must be greater or equal to 0');
  142. }
  143. $this->_badge = $badge;
  144. }
  145. /**
  146. * Get Expire
  147. *
  148. * @return int
  149. */
  150. public function getExpire()
  151. {
  152. return $this->_expire;
  153. }
  154. /**
  155. * Set Expire
  156. *
  157. * @param int $expire
  158. * @return Zend_Mobile_Push_Message_Apns
  159. * @throws Zend_Mobile_Push_Message_Exception
  160. */
  161. public function setExpire($expire)
  162. {
  163. if (!is_numeric($expire)) {
  164. throw new Zend_Mobile_Push_Message_Exception('$expire must be an integer');
  165. }
  166. $this->_expire = (int) $expire;
  167. return $this;
  168. }
  169. /**
  170. * Get Sound
  171. *
  172. * @return string
  173. */
  174. public function getSound()
  175. {
  176. return $this->_sound;
  177. }
  178. /**
  179. * Set Sound
  180. *
  181. * @param string $sound
  182. * @return Zend_Mobile_Push_Message_Apns
  183. * @throws Zend_Mobile_Push_Message_Exception
  184. */
  185. public function setSound($sound)
  186. {
  187. if (!is_string($sound)) {
  188. throw new Zend_Mobile_Push_Message_Exception('$sound must be a string');
  189. }
  190. $this->_sound = $sound;
  191. return $this;
  192. }
  193. /**
  194. * Add Custom Data
  195. *
  196. * @param string $key
  197. * @param mixed $value
  198. * @return Zend_Mobile_Push_Message_Apns
  199. * @throws Zend_Mobile_Push_Message_Exception
  200. */
  201. public function addCustomData($key, $value)
  202. {
  203. if (!is_string($key)) {
  204. throw new Zend_Mobile_Push_Message_Exception('$key is not a string');
  205. }
  206. if ($key == 'aps') {
  207. throw new Zend_Mobile_Push_Message_Exception('$key must not be aps as it is reserved by apple');
  208. }
  209. $this->_custom[$key] = $value;
  210. }
  211. /**
  212. * Clear Custom Data
  213. *
  214. * @return throw new Zend_Mobile_Push_Message_Apns
  215. */
  216. public function clearCustomData()
  217. {
  218. $this->_custom = array();
  219. return $this;
  220. }
  221. /**
  222. * Set Custom Data
  223. *
  224. * @param array $data
  225. * @return Zend_Mobile_Push_Message_Apns
  226. * @throws Zend_Mobile_Push_Message_Exception
  227. */
  228. public function setCustomData($array)
  229. {
  230. $this->_custom = array();
  231. foreach ($array as $k => $v) {
  232. $this->addCustomData($k, $v);
  233. }
  234. return $this;
  235. }
  236. /**
  237. * Get Custom Data
  238. *
  239. * @return array
  240. */
  241. public function getCustomData()
  242. {
  243. return $this->_custom;
  244. }
  245. /**
  246. * Validate this is a proper Apns message
  247. *
  248. * @return boolean
  249. */
  250. public function validate()
  251. {
  252. if (!is_string($this->_token) || strlen($this->_token) === 0) {
  253. return false;
  254. }
  255. if (null != $this->_id && !is_numeric($this->_id)) {
  256. return false;
  257. }
  258. return true;
  259. }
  260. }