IOSBuilder.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * IOS设备的消息体.
  4. * @author wangkuiwei
  5. * @name IOSBuilder
  6. * @desc 构建发送给IOS设备的Message对象。
  7. *
  8. */
  9. class IOSBuilder extends Message {
  10. const soundUrl = 'sound_url';
  11. const badge = 'badge';
  12. public function __construct() {
  13. parent::__construct();
  14. }
  15. public function description($description) {
  16. $this->description = $description;
  17. }
  18. public function timeToLive($ttl) {
  19. $this->time_to_live = $ttl;
  20. }
  21. public function timeToSend($timeToSend) {
  22. $this->time_to_send = $timeToSend;
  23. }
  24. public function soundUrl($url) {
  25. $this->extra(\IOSBuilder::soundUrl, $url);
  26. }
  27. public function badge($badge) {
  28. $this->extra(\IOSBuilder::badge, $badge);
  29. }
  30. public function extra($key, $value) {
  31. $this->extra[$key] = $value;
  32. }
  33. public function build() {
  34. $keys = array(
  35. 'description', 'time_to_live', 'time_to_send'
  36. );
  37. foreach ($keys as $key) {
  38. if (isset($this->$key)) {
  39. $this->fields[$key] = $this->$key;
  40. $this->json_infos[$key] = $this->$key;
  41. }
  42. }
  43. //单独处理extra
  44. $JsonExtra = array();
  45. if (count($this->extra) > 0) {
  46. foreach ($this->extra as $extraKey => $extraValue) {
  47. $this->fields[\Message::EXTRA_PREFIX . $extraKey] = $extraValue;
  48. $JsonExtra[$extraKey] = $extraValue;
  49. }
  50. }
  51. $this->json_infos['extra'] = $JsonExtra;
  52. }
  53. }
  54. ?>