forked from Bandwidth/php-bandwidth-iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTns.php
240 lines (202 loc) · 7.02 KB
/
Tns.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
namespace Iris;
final class Tns extends RestEntry {
protected $totalsTns = null;
public function __construct($parent, $client=null, $namespace="")
{
if($parent) {
$this->parent = $parent;
parent::_init($this->parent->get_rest_client(), $this->parent->get_relative_namespace());
}
else {
parent::_init($client, $namespace="");
}
}
public function get_from_sippeer() {
$tns = [];
$data = parent::_get('tns');
if($data['SipPeerTelephoneNumbers'] && $data['SipPeerTelephoneNumbers']["SipPeerTelephoneNumber"]) {
$items = $data['SipPeerTelephoneNumbers']["SipPeerTelephoneNumber"];
if($this->is_assoc($items))
$items = [ $items ];
foreach($items as $tn) {
$tns[] = new Tn($this, $tn);
}
}
return $tns;
}
public function getList($filters = Array()) {
if(isset($this->parent) && $this->parent instanceof \Iris\Sippeer) {
return $this->get_from_sippeer();
}
$tns = [];
$data = parent::_get('tns', $filters, Array("page"=> 1, "size" => 30), Array("page", "size"));
if (isset($data['TelephoneNumberCount']))
{
$this->totalsTns = (int) $data['TelephoneNumberCount'];
}
if(isset($data['TelephoneNumbers']) && isset($data['TelephoneNumbers']["TelephoneNumber"])) {
$items = $data['TelephoneNumbers']["TelephoneNumber"];
if($this->is_assoc($items))
$items = [ $items ];
foreach($items as $tn) {
$tns[] = new Tn($this, $tn);
}
}
return $tns;
}
public function totalTns()
{
if (is_null($this->totalsTns))
{
$this->getList(array('size' => 1));
}
return $this->totalsTns;
}
public function create($data) {
$tn = new Tn($this, $data);
return $tn;
}
public function tn($id) {
$tn = new Tn($this, array("FullNumber" => $id));
$tn->get();
return $tn;
}
public function get_rest_client() {
if(isset($this->parent)) {
return $this->parent->client;
} else {
return $this->client;
}
}
public function get_relative_namespace() {
return (isset($this->parent) ? $this->parent->get_relative_namespace() : '').'/tns';
}
}
final class Tn extends RestEntry{
use BaseModel;
protected $fields = array(
"City" => array(
"type" => "string"
),
"Lata" => array(
"type" => "string"
),
"State" => array(
"type" => "string"
),
"FullNumber" => array(
"type" => "string"
),
"Tier" => array(
"type" => "string"
),
"VendorId" => array(
"type" => "string"
),
"VendorName" => array(
"type" => "string"
),
"RateCenter" => array(
"type" => "string"
),
"Status" => array(
"type" => "string"
),
"AccountId" => array(
"type" => "string"
),
"LastModified" => array(
"type" => "string"
),
"OrderCreateDate" => array("type" => "string"),
"OrderId" => array("type" => "string"),
"OrderType" => array("type" => "string"),
"SiteId" => array("type" => "string"),
"Site" => array("type" => "string"),
"SipPeer" => array("type" => "string"),
"AccountId" => array("type" => "string"),
"Features" => array("type" => "\Iris\Features"),
"TnAttributes" => array("type" => "\Iris\TnAttributes"),
"MessagingSettings" => array("type" => "\Iris\MessageSettings"),
"TnOptions" => array("type" => "\Iris\TnOptionGroup")
);
public function __construct($tns, $data) {
$this->set_data($data);
$this->parent = $tns;
parent::_init($tns->get_rest_client(), $tns->get_relative_namespace());
}
public function get() {
$data = parent::_get($this->get_id());
if(isset($data["SipPeerTelephoneNumber"]))
$data = $data['SipPeerTelephoneNumber'];
$this->set_data($data);
}
public function tndetails() {
$url = sprintf("%s/%s", $this->get_id(), "tndetails");
$data = parent::_get($url);
if (isset($data['TelephoneNumberDetails']))
{
$this->set_data($data['TelephoneNumberDetails']);
}
}
public function site() {
if(!isset($this->AccountId))
$this->get();
$url = sprintf("%s/%s", $this->get_id(), "sites");
$data = parent::_get($url);
$account = new Account($this->AccountId, $this->parent->get_rest_client());
return $account->sites()->create($data, false);
}
public function sippeer() {
if(!isset($this->AccountId) || !isset($this->SiteId))
$this->get();
$url = sprintf("%s/%s", $this->get_id(), "sippeers");
$data = parent::_get($url);
$account = new Account($this->AccountId, $this->parent->get_rest_client());
return $account->sites()->create(["Id" => $this->SiteId], false)->sippeers()->create(["PeerId" => $data["Id"], "PeerName" => $data["Name"]], false);
}
public function tnreservation() {
if(!isset($this->AccountId) || !isset($this->SiteId))
$this->get();
$url = sprintf("%s/%s", $this->get_id(), "tnreservation");
$account = new Account($this->AccountId, $this->parent->get_rest_client());
$data = parent::_get($url);
return $account->tnsreservations()->create($data, false);
}
public function set_tn_options($data) {
$data = new \Iris\SipPeerTelephoneNumber($data);
if(!($this->parent->parent instanceof Sippeer))
throw new \Exception("You should get TN from sippeer");
parent::put($this->get_id(), "SipPeerTelephoneNumber", $data->to_array());
}
public function ratecenter() {
$url = sprintf("%s/%s", $this->get_id(), "ratecenter");
$data = parent::_get($url);
if($data['TelephoneNumberDetails']) {
return new TelephoneNumberDetail($data['TelephoneNumberDetails']);
}
return null;
}
public function lata() {
$url = sprintf("%s/%s", $this->get_id(), "lata");
$data = parent::_get($url);
if($data['TelephoneNumberDetails']) {
return new TelephoneNumberDetail($data['TelephoneNumberDetails']);
}
return null;
}
public function lca() {
$url = sprintf("%s/%s", $this->get_id(), "lca");
$data = parent::_get($url);
return new LcaSearch($data);
}
public function get_id() {
if(!isset($this->FullNumber))
throw new \Exception("You should set FullNumber");
return $this->FullNumber;
}
public function get_appendix() {
return '/'.$this->get_id();
}
}