forked from worldtext/Helper-Library-PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WorldTextGroup.class.php
200 lines (151 loc) · 4.53 KB
/
WorldTextGroup.class.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
<?php
class WorldTextGroup extends WorldText {
private $groupID;
private $numbers;
private $numberCount = NULL; // NULL not called, 0 empty, # number of rows.
private $retries = 0; // Instant fail on error
private $apiKey = '';
private $id = '';
// **
// @param $id World Text Account ID
// @param $apiKey secret API Key
// @param $grpName Group - created or loaded on construction.
public function __construct($id, $apiKey, $grpName, $srcAddr = NULL, $pin = NULL) {
parent::__construct($id, $apiKey);
if ($srcAddr) {
// Creating...
$this->groupID = $this->create($grpName, $srcAddr, $pin); //$grp = newGroup
} else {
$grp = $this->find($grpName);
if (!$grp) {
throw new Exception('No pre-existing group');
} else {
$this->groupID = $grp;
// Populate the group with its numbers...
$this->details();
}
}
}
// Static Factory Methods...
static public function CreateNewGroupInstance($id, $apiKey, $grpName, $srcAddr, $pin) {
return new WorldTextGroup($id, $apiKey, $grpName, $srcAddr, $pin);
}
static public function CreateExistingGroupInstance($id, $apiKey, $grpName) {
return new WorldTextGroup($id, $apiKey, $grpName, NULL, NULL);
}
// Group Methods ...
// Only meaningful after
// Construction with no group name parameter
// or
// After a destroy call
public function create($grpName, $srcAddr, $pin) {
if ($this->groupID) {
// Bad dog, no biscuit...
throw new Exception('Already have an active group - object must have no group set');
}
$data = array(
'name' => $grpName,
'srcaddr' => $srcAddr,
'pin' => $pin
);
$ret = $this->callResource(self::PUT, '/group/create', $data);
return $ret['data']['groupid'];
}
// Return an ID from name of Group, or NULL if not exist...
public function find($searchGrp) {
$ret = $this->callResource(self::GET, '/group/list');
for ($i = 0; $i < count($ret['data']['group']); $i++) {
if ($searchGrp == $ret['data']['group'][$i]['name']) {
return $ret['data']['group'][$i]['id'];
}
}
return NULL;
}
// Kill the group on the World Text servers...
//
// All group related object data is cleared after successful call:
//
// Create new group and populate it, or
// Create a new object
//
// ...to continue.
public function destroy() {
$data = array(
'grpid' => $this->groupID
);
$ret = $this->callResource(self::DELETE, '/group/destroy', $data);
// Best check it worked first :)
// Make sure people can't continue treating the group as active...
unset($this->numbers);
unset($this->groupID);
$this->numberCount = NULL;
}
public function entry($name, $number) {
$data = array(
'grpid' => $this->groupID,
'name' => $name,
'dstaddr' => $number
);
return $this->callResource(self::PUT, '/group/entry', $data);
}
public function entries($list) {
$data = array(
'members' => $list
);
return $this->callResource(self::PUT, '/group/entries', $data);
}
// Return the array of names/numbers in this group...
//
// If we don't already have the list, populate it.
public function details() {
if (isset($this->numberCount)) {
// Already got group contents, so just return the list...
return( $this->numbers );
}
$data = array(
'grpid' => $this->groupID
);
$ret = $this->callResource(self::GET, '/group/details', $data);
if ($ret['http_code'] == 200) {
// HTTP OK...
$this->numberCount = count($ret['data']['entry']);
// OK, but empty...
if (!$this->numberCount)
return NULL;
foreach ($ret['data']['entry'] as $val) {
$this->numbers[$val['number']] = $val['name'];
}
return $this->numbers;
} else {
// HTTP fail...
$this->lastError = $ret;
return NULL;
}
}
// Empties the group of names and numbers...
public function deleteContents() {
return( $this->callResource(self::DELETE, '/group/contents') );
}
// Returns cost in credits to send this group...
public function cost() {
return( $this->callResource(self::GET, '/group/cost', array('grpid' => $this->groupID)));
}
// Send a message to group ...
public function send($msg, $src = NULL, $multipart = NULL) {
$data = array(
'grpid' => $this->groupID,
'txt' => $msg
);
if ($src !== NULL) {
$data = array_merge($data, array('srcaddr' => $src));
}
if ($multipart) {
$data = array_merge($data, array('multipart' => $multipart));
}
// Unicode/UTF8 test
if (WorldText::isUTF8($msg)) {
$data = array_merge($data, array('enc' => "UnicodeBigUnmarked"));
}
return $this->callResource(self::PUT, '/group/send', $data);
}
}