1
|
|
|
<?php |
2
|
|
|
namespace nstdio\notymo; |
3
|
|
|
|
4
|
|
|
use nstdio\notymo\exception\UnsupportedNotificationTypeException; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Message |
8
|
|
|
* |
9
|
|
|
* @package nstdio\notymo |
10
|
|
|
* @author Edgar Asatryan <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class Message implements MessageInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var string Push notification message. |
16
|
|
|
*/ |
17
|
|
|
private $message; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string|array User device tokens |
21
|
|
|
*/ |
22
|
|
|
private $token; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private $customData = array(); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
private $badge = 0; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string Sound of notification. |
36
|
|
|
*/ |
37
|
|
|
private $sound; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var int |
41
|
|
|
*/ |
42
|
|
|
private $type; |
43
|
|
|
|
44
|
16 |
|
public function __construct($type = null) |
45
|
|
|
{ |
46
|
16 |
|
if ($type !== null) { |
47
|
3 |
|
$this->setType($type); |
48
|
3 |
|
} |
49
|
16 |
|
} |
50
|
|
|
|
51
|
7 |
|
public function getMessage() |
52
|
|
|
{ |
53
|
7 |
|
return $this->message; |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
public function setMessage($message) |
57
|
|
|
{ |
58
|
1 |
|
$this->message = $message; |
59
|
1 |
|
} |
60
|
|
|
|
61
|
5 |
|
public function getToken() |
62
|
|
|
{ |
63
|
5 |
|
return $this->token; |
64
|
|
|
} |
65
|
|
|
|
66
|
5 |
|
public function setToken($token) |
67
|
|
|
{ |
68
|
5 |
|
if (is_array($token)) { |
69
|
3 |
|
$this->token = $token; |
70
|
3 |
|
} else { |
71
|
3 |
|
$this->token = strval($token); |
72
|
|
|
} |
73
|
5 |
|
} |
74
|
|
|
|
75
|
1 |
|
public function getCustomData() |
76
|
|
|
{ |
77
|
1 |
|
return $this->customData; |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
public function setCustomData(array $data) |
81
|
|
|
{ |
82
|
1 |
|
$this->customData = $data; |
83
|
1 |
|
} |
84
|
|
|
|
85
|
6 |
|
public function getBadge() |
86
|
|
|
{ |
87
|
6 |
|
return $this->badge; |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
public function setBadge($badge) |
91
|
|
|
{ |
92
|
1 |
|
$this->badge = $badge; |
93
|
1 |
|
} |
94
|
|
|
|
95
|
6 |
|
public function getSound() |
96
|
|
|
{ |
97
|
6 |
|
return $this->sound; |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
public function setSound($sound) |
101
|
|
|
{ |
102
|
1 |
|
$this->sound = $sound; |
103
|
1 |
|
} |
104
|
|
|
|
105
|
2 |
|
public function getType() |
106
|
|
|
{ |
107
|
2 |
|
return $this->type; |
108
|
|
|
} |
109
|
|
|
|
110
|
5 |
|
public function setType($type) |
111
|
|
|
{ |
112
|
5 |
|
if ($type !== self::TYPE_IOS && $type !== self::TYPE_ANDROID) { |
113
|
1 |
|
throw new UnsupportedNotificationTypeException("Invalid message type : $type, please use one of MessageInterface constants."); |
114
|
|
|
} |
115
|
4 |
|
$this->type = $type; |
116
|
4 |
|
} |
117
|
|
|
|
118
|
3 |
|
public function addToken($token) |
119
|
|
|
{ |
120
|
3 |
|
if ($this->token === null) { |
121
|
1 |
|
$this->token = array(); |
122
|
1 |
|
$this->merge($token); |
123
|
3 |
|
} elseif (is_string($this->token)) { |
124
|
1 |
|
$oldToken = $this->token; |
125
|
1 |
|
$this->token = array(); |
126
|
1 |
|
array_push($this->token, $oldToken, $token); |
127
|
3 |
|
} elseif (is_array($this->token)) { |
128
|
2 |
|
$this->merge($token); |
129
|
2 |
|
} |
130
|
|
|
|
131
|
3 |
|
} |
132
|
|
|
|
133
|
3 |
|
public function isMultiple() |
134
|
|
|
{ |
135
|
3 |
|
return is_array($this->token); |
136
|
|
|
} |
137
|
|
|
|
138
|
2 |
|
public function cloneWith($type, $tokens) |
139
|
|
|
{ |
140
|
2 |
|
if ($type === $this->type) { |
141
|
1 |
|
return $this; |
142
|
|
|
} |
143
|
1 |
|
$cloned = clone $this; |
144
|
|
|
|
145
|
1 |
|
$cloned->setType($type); |
146
|
1 |
|
$cloned->token = $tokens; |
147
|
|
|
|
148
|
1 |
|
return $cloned; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param $token |
153
|
|
|
*/ |
154
|
2 |
|
private function merge($token) |
155
|
|
|
{ |
156
|
2 |
|
if (is_array($token)) { |
157
|
1 |
|
$this->token = array_merge($this->token, $token); |
158
|
1 |
|
} else { |
159
|
2 |
|
$this->token[] = $token; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |