ClickAction.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Libs\Push\HuaWei\Admin\Msg\Android;
  3. use App\Libs\Push\HuaWei\Admin\Msg\ValidatorUtil;
  4. use Exception;
  5. class ClickAction
  6. {
  7. private $type;
  8. private $intent;
  9. private $url;
  10. private $rich_resource;
  11. // added
  12. private $action;
  13. private $fields;
  14. public function __construct()
  15. {
  16. $this->rich_resource = null;
  17. $this->url = null;
  18. }
  19. public function type($value)
  20. {
  21. $this->type = $value;
  22. }
  23. public function intent($value)
  24. {
  25. $this->intent = $value;
  26. }
  27. public function url($value)
  28. {
  29. $this->url = $value;
  30. }
  31. public function rich_resource($value)
  32. {
  33. $this->rich_resource = $value;
  34. }
  35. // added
  36. public function action($value)
  37. {
  38. $this->action = $value;
  39. }
  40. public function getFields()
  41. {
  42. return $this->fields;
  43. }
  44. public function buildFields()
  45. {
  46. try {
  47. $this->check_parameter();
  48. } catch (Exception $e) {
  49. echo $e;
  50. }
  51. $keys = array(
  52. 'type',
  53. 'intent',
  54. 'url',
  55. 'rich_resource',
  56. // add
  57. 'action'
  58. );
  59. foreach ($keys as $key) {
  60. if (isset($this->$key)) {
  61. $this->fields[$key] = $this->$key;
  62. }
  63. }
  64. }
  65. private function check_parameter()
  66. {
  67. if (($this->type) && (gettype($this->type) != "integer")) {
  68. throw new Exception("type of type is wrong.");
  69. }
  70. if (!in_array($this->type, array(1, 2, 3, 4))) {
  71. throw new Exception("click type should be one of 1: customize action, 2: open url, 3: open app, 4: open rich media");
  72. }
  73. $PATTERN = "/^https.*/";
  74. switch ($this->type) {
  75. case 1:
  76. {
  77. if (is_null($this->intent) && is_null($this->action)) {
  78. throw new Exception("when type=1,intent/action at least one is valid");
  79. }
  80. }
  81. break;
  82. case 2:
  83. {
  84. if (is_null($this->url)) {
  85. throw new Exception("url is required when click type=2");
  86. }
  87. if (FALSE == ValidatorUtil::validatePattern($PATTERN, $this->url)) {
  88. throw new Exception("url must start with https");
  89. }
  90. }
  91. break;
  92. case 4:
  93. {
  94. if (is_null($this->richResource)) {
  95. throw new Exception("richResource is required when click type=4");
  96. }
  97. if (FALSE == ValidatorUtil::validatePattern($PATTERN, $this->richResource)) {
  98. throw new Exception("richResource must start with https");
  99. }
  100. }
  101. break;
  102. }
  103. }
  104. }