123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- <?php
- namespace App\Libs\Push\HuaWei\Admin\Msg\Android;
- use App\Libs\Push\HuaWei\Admin\Msg\ValidatorUtil;
- use Exception;
- class ClickAction
- {
- private $type;
- private $intent;
- private $url;
- private $rich_resource;
- // added
- private $action;
- private $fields;
- public function __construct()
- {
- $this->rich_resource = null;
- $this->url = null;
- }
- public function type($value)
- {
- $this->type = $value;
- }
- public function intent($value)
- {
- $this->intent = $value;
- }
- public function url($value)
- {
- $this->url = $value;
- }
- public function rich_resource($value)
- {
- $this->rich_resource = $value;
- }
- // added
- public function action($value)
- {
- $this->action = $value;
- }
- public function getFields()
- {
- return $this->fields;
- }
- public function buildFields()
- {
- try {
- $this->check_parameter();
- } catch (Exception $e) {
- echo $e;
- }
- $keys = array(
- 'type',
- 'intent',
- 'url',
- 'rich_resource',
- // add
- 'action'
- );
- foreach ($keys as $key) {
- if (isset($this->$key)) {
- $this->fields[$key] = $this->$key;
- }
- }
- }
- private function check_parameter()
- {
- if (($this->type) && (gettype($this->type) != "integer")) {
- throw new Exception("type of type is wrong.");
- }
- if (!in_array($this->type, array(1, 2, 3, 4))) {
- throw new Exception("click type should be one of 1: customize action, 2: open url, 3: open app, 4: open rich media");
- }
- $PATTERN = "/^https.*/";
- switch ($this->type) {
- case 1:
- {
- if (is_null($this->intent) && is_null($this->action)) {
- throw new Exception("when type=1,intent/action at least one is valid");
- }
- }
- break;
- case 2:
- {
- if (is_null($this->url)) {
- throw new Exception("url is required when click type=2");
- }
- if (FALSE == ValidatorUtil::validatePattern($PATTERN, $this->url)) {
- throw new Exception("url must start with https");
- }
- }
- break;
- case 4:
- {
- if (is_null($this->richResource)) {
- throw new Exception("richResource is required when click type=4");
- }
- if (FALSE == ValidatorUtil::validatePattern($PATTERN, $this->richResource)) {
- throw new Exception("richResource must start with https");
- }
- }
- break;
- }
- }
- }
|