Apns.php 6.8 KB

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