Toast.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. */
  21. /** Zend_Mobile_Push_Message_Mpns **/
  22. require_once 'Zend/Mobile/Push/Message/Mpns.php';
  23. /**
  24. * Mpns Toast Message
  25. *
  26. * @category Zend
  27. * @package Zend_Mobile
  28. * @subpackage Zend_Mobile_Push
  29. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Mobile_Push_Message_Mpns_Toast extends Zend_Mobile_Push_Message_Mpns
  33. {
  34. /**
  35. * Mpns delays
  36. *
  37. * @var int
  38. */
  39. const DELAY_IMMEDIATE = 2;
  40. const DELAY_450S = 12;
  41. const DELAY_900S = 22;
  42. /**
  43. * Title
  44. *
  45. * @var string
  46. */
  47. protected $_title;
  48. /**
  49. * Message
  50. *
  51. * @var string
  52. */
  53. protected $_msg;
  54. /**
  55. * Params
  56. *
  57. * @var string
  58. */
  59. protected $_params;
  60. /**
  61. * Get Title
  62. *
  63. * @return string
  64. */
  65. public function getTitle()
  66. {
  67. return $this->_title;
  68. }
  69. /**
  70. * Set Title
  71. *
  72. * @param string $title
  73. * @return Zend_Mobile_Push_Message_Mpns_Toast
  74. * @throws Zend_Mobile_Push_Message_Exception
  75. */
  76. public function setTitle($title)
  77. {
  78. if (!is_string($title)) {
  79. throw new Zend_Mobile_Push_Message_Exception('$title must be a string');
  80. }
  81. $this->_title = $title;
  82. return $this;
  83. }
  84. /**
  85. * Get Message
  86. *
  87. * @return string
  88. */
  89. public function getMessage()
  90. {
  91. return $this->_msg;
  92. }
  93. /**
  94. * Set Message
  95. *
  96. * @param string $msg
  97. * @return Zend_Mobile_Push_Message_Mpns_Toast
  98. * @throws Zend_Mobile_Push_Message_Exception
  99. */
  100. public function setMessage($msg)
  101. {
  102. if (!is_string($msg)) {
  103. throw new Zend_Mobile_Push_Message_Exception('$msg must be a string');
  104. }
  105. $this->_msg = $msg;
  106. return $this;
  107. }
  108. /**
  109. * Get Params
  110. *
  111. * @return string
  112. */
  113. public function getParams()
  114. {
  115. return $this->_params;
  116. }
  117. /**
  118. * Set Params
  119. *
  120. * @param string $params
  121. * @return Zend_Mobile_Push_Message_Mpns_Toast
  122. * @throws Zend_Mobile_Push_Message_Exception
  123. */
  124. public function setParams($params)
  125. {
  126. if (!is_string($params)) {
  127. throw new Zend_Mobile_Push_Message_Exception('$params must be a string');
  128. }
  129. $this->_params = $params;
  130. return $this;
  131. }
  132. /**
  133. * Get Delay
  134. *
  135. * @return int
  136. */
  137. public function getDelay()
  138. {
  139. if (!$this->_delay) {
  140. return self::DELAY_IMMEDIATE;
  141. }
  142. return $this->_delay;
  143. }
  144. /**
  145. * Set Delay
  146. *
  147. * @param int $delay
  148. * @return Zend_Mobile_Push_Message_Mpns_Toast
  149. * @throws Zend_Mobile_Push_Message_Exception
  150. */
  151. public function setDelay($delay)
  152. {
  153. if (!in_array($delay, array(
  154. self::DELAY_IMMEDIATE,
  155. self::DELAY_450S,
  156. self::DELAY_900S
  157. ))) {
  158. throw new Zend_Mobile_Push_Message_Exception('$delay must be one of the DELAY_* constants');
  159. }
  160. $this->_delay = $delay;
  161. return $this;
  162. }
  163. /**
  164. * Get Notification Type
  165. *
  166. * @return string
  167. */
  168. public static function getNotificationType()
  169. {
  170. return 'toast';
  171. }
  172. /**
  173. * Get XML Payload
  174. *
  175. * @return string
  176. */
  177. public function getXmlPayload()
  178. {
  179. $ret = '<?xml version="1.0" encoding="utf-8"?>'
  180. . '<wp:Notification xmlns:wp="WPNotification">'
  181. . '<wp:Toast>'
  182. . '<wp:Text1>' . htmlspecialchars($this->_title) . '</wp:Text1>'
  183. . '<wp:Text2>' . htmlspecialchars($this->_msg) . '</wp:Text2>';
  184. if (!empty($this->_params)) {
  185. $ret .= '<wp:Param>' . htmlspecialchars($this->_params) . '</wp:Param>';
  186. }
  187. $ret .= '</wp:Toast>'
  188. . '</wp:Notification>';
  189. return $ret;
  190. }
  191. /**
  192. * Validate proper mpns message
  193. *
  194. * @return boolean
  195. */
  196. public function validate()
  197. {
  198. if (!isset($this->_token) || strlen($this->_token) === 0) {
  199. return false;
  200. }
  201. if (empty($this->_title)) {
  202. return false;
  203. }
  204. if (empty($this->_msg)) {
  205. return false;
  206. }
  207. return parent::validate();
  208. }
  209. }