ApnsHeaders.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace App\Libs\Push\HuaWei\Admin\Msg\Apns;
  3. use App\Libs\Push\HuaWei\Admin\ApnConstant;
  4. use App\Libs\Push\HuaWei\Admin\Constants;
  5. use App\Libs\Push\HuaWei\Admin\Msg\ValidatorUtil;
  6. use App\Libs\Push\HuaWei\Admin\PushLogConfig;
  7. use Exception;
  8. class ApnsHeaders
  9. {
  10. private $authorization;
  11. private $apns_id;
  12. private $apns_expiration;
  13. private $apns_priority;
  14. private $apns_topic;
  15. private $apns_collapse_id;
  16. private $fields;
  17. public function authorization($value)
  18. {
  19. $this->authorization = $value;
  20. }
  21. public function apns_id($value)
  22. {
  23. $this->apns_id = $value;
  24. }
  25. public function apns_expiration($value)
  26. {
  27. $this->apns_expiration = $value;
  28. }
  29. public function apns_priority($value)
  30. {
  31. $this->apns_priority = $value;
  32. }
  33. public function apns_topic($value)
  34. {
  35. $this->apns_topic = $value;
  36. }
  37. public function apns_collapse_id($value)
  38. {
  39. $this->apns_collapse_id = $value;
  40. }
  41. public function getFields()
  42. {
  43. return $this->fields;
  44. }
  45. public function buildFields()
  46. {
  47. try {
  48. $this->check_parameter();
  49. } catch (Exception $e) {
  50. echo $e;
  51. }
  52. $keys = array(
  53. 'authorization',
  54. 'apns-id',
  55. 'apns-expiration',
  56. 'apns-priority',
  57. 'apns-topic',
  58. 'apns-collapse-id'
  59. );
  60. foreach ($keys as $key) {
  61. $value = str_replace('-', '_', $key);
  62. PushLogConfig::getSingleInstance()->LogMessage('[' . __CLASS__ . '][before key:' . $key . '][after key:' . $value . ']', Constants::HW_PUSH_LOG_DEBUG_LEVEL);
  63. if (isset($this->$value)) {
  64. $this->fields[$key] = $this->$value;
  65. }
  66. }
  67. PushLogConfig::getSingleInstance()->LogMessage('[' . __CLASS__ . '][buildFields result:' . json_encode($this->fields), Constants::HW_PUSH_LOG_DEBUG_LEVEL);
  68. }
  69. private function check_parameter()
  70. {
  71. if (!empty($this->authorization)) {
  72. if (FALSE == ValidatorUtil::validatePattern(Constants::APN_AUTHORIZATION_PATTERN, $this->authorization)) {
  73. throw new Exception("authorization must start with bearer");
  74. }
  75. }
  76. if (!empty($this->apns_id)) {
  77. if (FALSE == ValidatorUtil::validatePattern(Constants::APN_ID_PATTERN, $this->apns_id)) {
  78. throw new Exception("apns-id format error");
  79. }
  80. }
  81. if (!empty($this->apns_priority)) {
  82. if (!in_array($this->apns_priority, array(
  83. ApnConstant::ANP_PRIORITY_SEND_BY_GROUP,
  84. ApnConstant::ANP_PRIORITY_SEND_IMMEDIATELY
  85. ))) {
  86. throw new Exception("apns-priority should be SEND_BY_GROUP:5 or SEND_IMMEDIATELY:10");
  87. }
  88. }
  89. if (!empty($this->apns_collapse_id)) {
  90. if (strlen($this->apns_priority) >= 64) {
  91. throw new Exception("Number of apnsCollapseId bytes should be less than 64");
  92. }
  93. }
  94. }
  95. }