AndroidConfig.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace App\Libs\Push\HuaWei\Admin\Msg\Android;
  3. use App\Libs\Push\HuaWei\Admin\AndroidConfigDeliveryPriority;
  4. use App\Libs\Push\HuaWei\Admin\Msg\ValidatorUtil;
  5. use Exception;
  6. use App\Libs\Push\HuaWei\Admin\Constants;
  7. use App\Libs\Push\HuaWei\Admin\PushLogConfig;
  8. class AndroidConfig
  9. {
  10. private $collapse_key;
  11. private $category;
  12. private $urgency;
  13. private $ttl;
  14. private $bi_tag;
  15. private $fast_app_target;
  16. private $notification;
  17. private $fields;
  18. private $data;
  19. public function __construct()
  20. {
  21. $this->urgency = null;
  22. $this->notification = null;
  23. $this->fields = array();
  24. }
  25. public function getFields()
  26. {
  27. return $this->fields;
  28. }
  29. public function collapse_key($value)
  30. {
  31. $this->collapse_key = $value;
  32. }
  33. public function category($value)
  34. {
  35. $this->category = $value;
  36. }
  37. public function urgency($value)
  38. {
  39. $this->urgency = $value;
  40. }
  41. public function ttl($value)
  42. {
  43. $this->ttl = $value;
  44. }
  45. public function bi_tag($value)
  46. {
  47. $this->bi_tag = $value;
  48. }
  49. public function fast_app_target($value)
  50. {
  51. $this->fast_app_target = $value;
  52. }
  53. public function notification($value)
  54. {
  55. $this->notification = $value;
  56. }
  57. public function data($value)
  58. {
  59. $this->data = $value;
  60. }
  61. public function buildFields()
  62. {
  63. try {
  64. $this->check_parameter();
  65. } catch (Exception $e) {
  66. // echo $e;
  67. PushLogConfig::getSingleInstance()->LogMessage($e, Constants::HW_PUSH_LOG_ERROR_LEVEL);
  68. return;
  69. }
  70. $keys = array(
  71. 'collapse_key',
  72. 'category',
  73. 'urgency',
  74. 'ttl',
  75. 'bi_tag',
  76. 'fast_app_target',
  77. 'notification',
  78. 'data'
  79. );
  80. foreach ($keys as $key) {
  81. if (isset($this->$key)) {
  82. $this->fields[$key] = $this->$key;
  83. }
  84. }
  85. }
  86. private function check_parameter()
  87. {
  88. if (!empty($this->collapse_key) && ($this->collapse_key < -1 || $this->collapse_key > 100)) {
  89. throw new Exception("Collapse Key should be [-1, 100]");
  90. }
  91. if (!empty($this->fast_app_target) && !in_array($this->fast_app_target, array(
  92. 1,
  93. 2
  94. ))) {
  95. throw new Exception("Invalid fast app target type[one of 1 or 2]");
  96. }
  97. if (!empty($this->urgency) && !in_array($this->urgency, array(
  98. AndroidConfigDeliveryPriority::PRIORITY_HIGH,
  99. AndroidConfigDeliveryPriority::PRIORITY_NORMAL
  100. ))) {
  101. throw new Exception("delivery priority shouid be [HIGH, NOMAL]");
  102. }
  103. if (!empty($this->ttl)) {
  104. if (FALSE == ValidatorUtil::validatePattern(Constants::TTL_PATTERN, $this->ttl)) {
  105. throw new Exception("The TTL's format is wrong");
  106. }
  107. }
  108. }
  109. public function get_all_vars()
  110. {
  111. var_dump(get_object_vars($this));
  112. }
  113. }