forked from jpush/jpush-api-php-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDevicePayload.php
219 lines (186 loc) · 7.21 KB
/
DevicePayload.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
namespace JPush;
use InvalidArgumentException;
class DevicePayload {
private $client;
/**
* DevicePayload constructor.
* @param $client JPush
*/
public function __construct($client)
{
$this->client = $client;
}
public function getDevices($registrationId) {
$url = $this->client->makeURL('device') . $registrationId;
return Http::get($this->client, $url);
}
public function updateAlias($registration_id, $alias) {
return $this->updateDevice($registration_id, $alias);
}
public function addTags($registration_id, $tags) {
$tags = is_array($tags) ? $tags : array($tags);
return $this->updateDevice($registration_id, null, null, $tags);
}
public function removeTags($registration_id, $tags) {
$tags = is_array($tags) ? $tags : array($tags);
return $this->updateDevice($registration_id, null, null, null, $tags);
}
public function updateMoblie($registration_id, $mobile) {
return $this->updateDevice($registration_id, null, $mobile);
}
public function clearTags($registrationId) {
$url = $this->client->makeURL('device') . $registrationId;
return Http::post($this->client, $url, ['tags' => '']);
}
public function updateDevice($registrationId, $alias = null, $mobile = null, $addTags = null, $removeTags = null) {
$payload = array();
if (!is_string($registrationId)) {
throw new InvalidArgumentException('Invalid registration_id');
}
$aliasIsNull = is_null($alias);
$mobileIsNull = is_null($mobile);
$addTagsIsNull = is_null($addTags);
$removeTagsIsNull = is_null($removeTags);
if ($aliasIsNull && $addTagsIsNull && $removeTagsIsNull && $mobileIsNull) {
throw new InvalidArgumentException("alias, addTags, removeTags not all null");
}
if (!$aliasIsNull) {
if (is_string($alias)) {
$payload['alias'] = $alias;
} else {
throw new InvalidArgumentException("Invalid alias string");
}
}
if (!$mobileIsNull) {
if (is_string($mobile)) {
$payload['mobile'] = $mobile;
} else {
throw new InvalidArgumentException("Invalid mobile string");
}
}
$tags = array();
if (!$addTagsIsNull) {
if (is_array($addTags)) {
$tags['add'] = $addTags;
} else {
throw new InvalidArgumentException("Invalid addTags array");
}
}
if (!$removeTagsIsNull) {
if (is_array($removeTags)) {
$tags['remove'] = $removeTags;
} else {
throw new InvalidArgumentException("Invalid removeTags array");
}
}
if (count($tags) > 0) {
$payload['tags'] = $tags;
}
$url = $this->client->makeURL('device') . $registrationId;
return Http::post($this->client, $url, $payload);
}
public function getTags() {
$url = $this->client->makeURL('tag');
return Http::get($this->client, $url);
}
public function isDeviceInTag($registrationId, $tag) {
if (!is_string($registrationId)) {
throw new InvalidArgumentException("Invalid registration_id");
}
if (!is_string($tag)) {
throw new InvalidArgumentException("Invalid tag");
}
$url = $this->client->makeURL('tag') . $tag . '/registration_ids/' . $registration_id;
return Http::get($this->client, $url);
}
public function addDevicesToTag($tag, $addDevices) {
$device = is_array($addDevices) ? $addDevices : array($addDevices);
return $this->updateTag($tag, $device, null);
}
public function removeDevicesFromTag($tag, $removeDevices) {
$device = is_array($removeDevices) ? $removeDevices : array($removeDevices);
return $this->updateTag($tag, null, $device);
}
public function updateTag($tag, $addDevices = null, $removeDevices = null) {
if (!is_string($tag)) {
throw new InvalidArgumentException("Invalid tag");
}
$addDevicesIsNull = is_null($addDevices);
$removeDevicesIsNull = is_null($removeDevices);
if ($addDevicesIsNull && $removeDevicesIsNull) {
throw new InvalidArgumentException("Either or both addDevices and removeDevices must be set.");
}
$registrationId = array();
if (!$addDevicesIsNull) {
if (is_array($addDevices)) {
$registrationId['add'] = $addDevices;
} else {
throw new InvalidArgumentException("Invalid addDevices");
}
}
if (!$removeDevicesIsNull) {
if (is_array($removeDevices)) {
$registrationId['remove'] = $removeDevices;
} else {
throw new InvalidArgumentException("Invalid removeDevices");
}
}
$url = $this->client->makeURL('tag') . $tag;
$payload = array('registration_ids'=>$registrationId);
return Http::post($this->client, $url, $payload);
}
public function deleteTag($tag) {
if (!is_string($tag)) {
throw new InvalidArgumentException("Invalid tag");
}
$url = $this->client->makeURL('tag') . $tag;
return Http::delete($this->client, $url);
}
public function getAliasDevices($alias, $platform = null) {
if (!is_string($alias)) {
throw new InvalidArgumentException("Invalid alias");
}
$url = $this->client->makeURL('alias') . $alias;
if (!is_null($platform)) {
if (is_array($platform)) {
$isFirst = true;
foreach($platform as $item) {
if ($isFirst) {
$url = $url . '?platform=' . $item;
$isFirst = false;
} else {
$url = $url . ',' . $item;
}
}
} else if (is_string($platform)) {
$url = $url . '?platform=' . $platform;
} else {
throw new InvalidArgumentException("Invalid platform");
}
}
return Http::get($this->client, $url);
}
public function deleteAlias($alias) {
if (!is_string($alias)) {
throw new InvalidArgumentException("Invalid alias");
}
$url = $this->client->makeURL('alias') . $alias;
return Http::delete($this->client, $url);
}
public function getDevicesStatus($registrationId) {
if (!is_array($registrationId) && !is_string($registrationId)) {
throw new InvalidArgumentException('Invalid registration_id');
}
if (is_string($registrationId)) {
$registrationId = explode(',', $registrationId);
}
$payload = array();
if (count($registrationId) <= 0) {
throw new InvalidArgumentException('Invalid registration_id');
}
$payload['registration_ids'] = $registrationId;
$url = $this->client->makeURL('device') . 'status';
return Http::post($this->client, $url, $payload);
}
}