C2dm.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. * C2dm 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_C2dm extends Zend_Mobile_Push_Message_Abstract
  35. {
  36. /**
  37. * Data key value pairs
  38. *
  39. * @var array
  40. */
  41. protected $_data = array();
  42. /**
  43. * Delay While Idle
  44. *
  45. * @var boolean
  46. */
  47. protected $_delay = false;
  48. /**
  49. * Add Data
  50. *
  51. * @param string $key
  52. * @param string $value
  53. * @return Zend_Mobile_Push_Message_C2dm
  54. * @throws Zend_Mobile_Push_Message_Exception
  55. */
  56. public function addData($key, $value)
  57. {
  58. if (!is_string($key)) {
  59. throw new Zend_Mobile_Push_Message_Exception('$key is not a string');
  60. }
  61. if (!is_scalar($value)) {
  62. throw new Zend_Mobile_Push_Message_Exception('$value is not a string');
  63. }
  64. $this->_data[$key] = $value;
  65. return $this;
  66. }
  67. /**
  68. * Set Data
  69. *
  70. * @param array $data
  71. * @return Zend_Mobile_Push_Message_C2dm
  72. * @throws Zend_Mobile_Push_Message_Exception
  73. */
  74. public function setData(array $data)
  75. {
  76. $this->clearData();
  77. foreach ($data as $k => $v) {
  78. $this->addData($k, $v);
  79. }
  80. return $this;
  81. }
  82. /**
  83. * Clear Data
  84. *
  85. * @return Zend_Mobile_Push_Message_C2dm
  86. */
  87. public function clearData()
  88. {
  89. $this->_data = array();
  90. }
  91. /**
  92. * Get Data
  93. *
  94. * @return array
  95. */
  96. public function getData()
  97. {
  98. return $this->_data;
  99. }
  100. /**
  101. * Set Delay While Idle
  102. *
  103. * @param boolean $delay
  104. * @return Zend_Mobile_Push_Message_C2dm
  105. * @throws Zend_Mobile_Push_Message_Exception
  106. */
  107. public function setDelayWhileIdle($delay)
  108. {
  109. if (!is_bool($delay)) {
  110. throw new Zend_Mobile_Push_Message_Exception('$delay must be boolean');
  111. }
  112. $this->_delay = $delay;
  113. return $this;
  114. }
  115. /**
  116. * Get Delay While Idle
  117. *
  118. * @return boolean
  119. */
  120. public function getDelayWhileIdle()
  121. {
  122. return $this->_delay;
  123. }
  124. /**
  125. * Validate this is a proper C2dm message
  126. * Does not validate size.
  127. *
  128. * @return boolean
  129. */
  130. public function validate()
  131. {
  132. if (!is_string($this->_token) || strlen($this->_token) === 0) {
  133. return false;
  134. }
  135. if (!is_scalar($this->_id) || strlen($this->_id) === 0) {
  136. return false;
  137. }
  138. return true;
  139. }
  140. }