SendSmsAbstract.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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_Service
  17. * @subpackage DeveloperGarden
  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. /**
  23. * @see Zend_Service_DeveloperGarden_Request_RequestAbstract
  24. */
  25. require_once 'Zend/Service/DeveloperGarden/Request/RequestAbstract.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Service
  29. * @subpackage DeveloperGarden
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @author Marco Kaiser
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. abstract class Zend_Service_DeveloperGarden_Request_SendSms_SendSmsAbstract
  35. extends Zend_Service_DeveloperGarden_Request_RequestAbstract
  36. {
  37. /**
  38. * the number or numbers to receive this sms
  39. *
  40. * @var string
  41. */
  42. public $number = null;
  43. /**
  44. * the message of this sms
  45. *
  46. * @var string
  47. */
  48. public $message = null;
  49. /**
  50. * name of the sender
  51. *
  52. * @var string
  53. */
  54. public $originator = null;
  55. /**
  56. * account
  57. *
  58. * @var integer
  59. */
  60. public $account = null;
  61. /**
  62. * array of special chars that are used for counting
  63. * message length
  64. *
  65. * @var array
  66. */
  67. private $_specialChars = array(
  68. '|',
  69. '^',
  70. '{',
  71. '}',
  72. '[',
  73. ']',
  74. '~',
  75. '\\',
  76. "\n",
  77. // '€', removed because its counted in utf8 correctly
  78. );
  79. /**
  80. * what SMS type is it
  81. *
  82. * 1 = SMS
  83. * 2 = FlashSMS
  84. *
  85. * @var integer
  86. */
  87. protected $_smsType = 1;
  88. /**
  89. * the counter for increasing message count
  90. * if more than this 160 chars we send a 2nd or counting
  91. * sms message
  92. *
  93. * @var integer
  94. */
  95. protected $_smsLength = 153;
  96. /**
  97. * maximum length of an sms message
  98. *
  99. * @var integer
  100. */
  101. protected $_maxLength = 765;
  102. /**
  103. * the maximum numbers to send an sms
  104. *
  105. * @var integer
  106. */
  107. protected $_maxNumbers = 10;
  108. /**
  109. * returns the assigned numbers
  110. *
  111. * @return string $number
  112. */
  113. public function getNumber()
  114. {
  115. return $this->number;
  116. }
  117. /**
  118. * set a new number(s)
  119. *
  120. * @param string $number
  121. * @throws Zend_Service_DeveloperGarden_Request_Exception
  122. *
  123. * @return Zend_Service_DeveloperGarden_Request_SendSms_SendSmsAbstract
  124. */
  125. public function setNumber($number)
  126. {
  127. $this->number = $number;
  128. if ($this->getNumberCount() > $this->_maxNumbers) {
  129. require_once 'Zend/Service/DeveloperGarden/Request/Exception.php';
  130. throw new Zend_Service_DeveloperGarden_Request_Exception('The message is too long.');
  131. }
  132. return $this;
  133. }
  134. /**
  135. * returns the current message
  136. *
  137. * @return string $message
  138. */
  139. public function getMessage()
  140. {
  141. return $this->message;
  142. }
  143. /**
  144. * sets a new message
  145. *
  146. * @param string $message
  147. * @throws Zend_Service_DeveloperGarden_Request_Exception
  148. *
  149. * @return Zend_Service_DeveloperGarden_Request_SendSms_SendSmsAbstract
  150. */
  151. public function setMessage($message)
  152. {
  153. $this->message = $message;
  154. if ($this->getMessageLength() > $this->_maxLength) {
  155. require_once 'Zend/Service/DeveloperGarden/Request/Exception.php';
  156. throw new Zend_Service_DeveloperGarden_Request_Exception('The message is too long.');
  157. }
  158. return $this;
  159. }
  160. /**
  161. * returns the originator
  162. *
  163. * @return the $originator
  164. */
  165. public function getOriginator()
  166. {
  167. return $this->originator;
  168. }
  169. /**
  170. * the originator name
  171. *
  172. * @param string $originator
  173. * @return Zend_Service_DeveloperGarden_Request_SendSms_SendSmsAbstract
  174. */
  175. public function setOriginator($originator)
  176. {
  177. $this->originator = $originator;
  178. return $this;
  179. }
  180. /**
  181. * the account
  182. * @return integer $account
  183. */
  184. public function getAccount()
  185. {
  186. return $this->account;
  187. }
  188. /**
  189. * sets a new accounts
  190. *
  191. * @param int $account the $account to set
  192. * @return Zend_Service_DeveloperGarden_Request_SendSms_SendSmsAbstract
  193. */
  194. public function setAccount($account)
  195. {
  196. $this->account = $account;
  197. return $this;
  198. }
  199. /**
  200. * returns the calculated message length
  201. *
  202. * @return integer
  203. */
  204. public function getMessageLength()
  205. {
  206. $message = $this->getMessage();
  207. $length = strlen($message);
  208. foreach ($this->_specialChars as $char) {
  209. $c = (substr_count($message, $char) * 2) - 1;
  210. if ($c > 0) {
  211. $length += $c;
  212. }
  213. }
  214. return $length;
  215. }
  216. /**
  217. * returns the count of sms messages that would be send
  218. *
  219. * @return integer
  220. */
  221. public function getMessageCount()
  222. {
  223. $smsLength = $this->getMessageLength();
  224. $retValue = 1;
  225. if ($smsLength > 160) {
  226. $retValue = ceil($smsLength / $this->_smsLength);
  227. }
  228. return $retValue;
  229. }
  230. /**
  231. * returns the count of numbers in this sms
  232. *
  233. * @return integer
  234. */
  235. public function getNumberCount()
  236. {
  237. $number = $this->getNumber();
  238. $retValue = 0;
  239. if (!empty($number)) {
  240. $retValue = count(explode(',', $number));
  241. }
  242. return $retValue;
  243. }
  244. /**
  245. * returns the sms type
  246. * currently we have
  247. * 1 = Sms
  248. * 2 = FlashSms
  249. *
  250. * @return integer
  251. */
  252. public function getSmsType()
  253. {
  254. return $this->_smsType;
  255. }
  256. }