123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- namespace App\Libs\Push\HuaWei\Admin\Msg\Apns;
- use App\Libs\Push\HuaWei\Admin\ApnConstant;
- use App\Libs\Push\HuaWei\Admin\Constants;
- use App\Libs\Push\HuaWei\Admin\Msg\ValidatorUtil;
- use App\Libs\Push\HuaWei\Admin\PushLogConfig;
- use Exception;
- class ApnsHeaders
- {
- private $authorization;
- private $apns_id;
- private $apns_expiration;
- private $apns_priority;
- private $apns_topic;
- private $apns_collapse_id;
- private $fields;
- public function authorization($value)
- {
- $this->authorization = $value;
- }
- public function apns_id($value)
- {
- $this->apns_id = $value;
- }
- public function apns_expiration($value)
- {
- $this->apns_expiration = $value;
- }
- public function apns_priority($value)
- {
- $this->apns_priority = $value;
- }
- public function apns_topic($value)
- {
- $this->apns_topic = $value;
- }
- public function apns_collapse_id($value)
- {
- $this->apns_collapse_id = $value;
- }
- public function getFields()
- {
- return $this->fields;
- }
- public function buildFields()
- {
- try {
- $this->check_parameter();
- } catch (Exception $e) {
- echo $e;
- }
- $keys = array(
- 'authorization',
- 'apns-id',
- 'apns-expiration',
- 'apns-priority',
- 'apns-topic',
- 'apns-collapse-id'
- );
- foreach ($keys as $key) {
- $value = str_replace('-', '_', $key);
- PushLogConfig::getSingleInstance()->LogMessage('[' . __CLASS__ . '][before key:' . $key . '][after key:' . $value . ']', Constants::HW_PUSH_LOG_DEBUG_LEVEL);
- if (isset($this->$value)) {
- $this->fields[$key] = $this->$value;
- }
- }
- PushLogConfig::getSingleInstance()->LogMessage('[' . __CLASS__ . '][buildFields result:' . json_encode($this->fields), Constants::HW_PUSH_LOG_DEBUG_LEVEL);
- }
- private function check_parameter()
- {
- if (!empty($this->authorization)) {
- if (FALSE == ValidatorUtil::validatePattern(Constants::APN_AUTHORIZATION_PATTERN, $this->authorization)) {
- throw new Exception("authorization must start with bearer");
- }
- }
- if (!empty($this->apns_id)) {
- if (FALSE == ValidatorUtil::validatePattern(Constants::APN_ID_PATTERN, $this->apns_id)) {
- throw new Exception("apns-id format error");
- }
- }
- if (!empty($this->apns_priority)) {
- if (!in_array($this->apns_priority, array(
- ApnConstant::ANP_PRIORITY_SEND_BY_GROUP,
- ApnConstant::ANP_PRIORITY_SEND_IMMEDIATELY
- ))) {
- throw new Exception("apns-priority should be SEND_BY_GROUP:5 or SEND_IMMEDIATELY:10");
- }
- }
- if (!empty($this->apns_collapse_id)) {
- if (strlen($this->apns_priority) >= 64) {
- throw new Exception("Number of apnsCollapseId bytes should be less than 64");
- }
- }
- }
- }
|