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
<?php
/**
* Nexmo Client Library for PHP
*
* @copyright Copyright (c) 2016 Nexmo, Inc. (http://nexmo.com)
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
*/
namespace Nexmo\Numbers;
use Nexmo\Application\Application;
use Nexmo\Entity\EntityInterface;
use Nexmo\Entity\JsonResponseTrait;
use Nexmo\Entity\JsonSerializableInterface;
use Nexmo\Entity\JsonSerializableTrait;
use Nexmo\Entity\JsonUnserializableInterface;
use Nexmo\Entity\NoRequestResponseTrait;
class Number implements EntityInterface, JsonSerializableInterface, JsonUnserializableInterface
{
use JsonSerializableTrait;
use NoRequestResponseTrait;
use JsonResponseTrait;
const TYPE_MOBILE = 'mobile-lvn';
const TYPE_FIXED = 'landline';
const FEATURE_VOICE = 'VOICE';
const FEATURE_SMS = 'SMS';
const WEBHOOK_MESSAGE = 'moHttpUrl';
const WEBHOOK_VOICE_STATUS = 'voiceStatusCallbackUrl';
const ENDPOINT_SIP = 'sip';
const ENDPOINT_TEL = 'tel';
const ENDPOINT_VXML = 'vxml';
const ENDPOINT_APP = 'app';
protected $data = [];
public function __construct($number = null, $country = null)
{
$this->data['msisdn'] = $number;
$this->data['country'] = $country;
}
public function getId()
{
return $this->fromData('msisdn');
}
public function getMsisdn()
{
return $this->getId();
}
public function getNumber()
{
return $this->getId();
}
public function getCountry()
{
return $this->fromData('country');
}
public function getType()
{
return $this->fromData('type');
}
public function hasFeature($feature)
{
if(!isset($this->data['features'])){
return false;
}
return in_array($feature, $this->data['features']);
}
public function getFeatures()
{
return $this->fromData('features');
}
public function setWebhook($type, $url)
{
if(!in_array($type, [self::WEBHOOK_MESSAGE, self::WEBHOOK_VOICE_STATUS])){
throw new \InvalidArgumentException("invalid webhook type `$type`");
}
$this->data[$type] = $url;
return $this;
}
public function getWebhook($type)
{
return $this->fromData($type);
}
public function hasWebhook($type)
{
return isset($this->data[$type]);
}
public function setVoiceDestination($endpoint, $type = null)
{
if(is_null($type)){
$type = $this->autoType($endpoint);
}
if(self::ENDPOINT_APP == $type AND !($endpoint instanceof Application)){
$endpoint = new Application($endpoint);
}
$this->data['voiceCallbackValue'] = $endpoint;
$this->data['voiceCallbackType'] = $type;
return $this;
}
protected function autoType($endpoint)
{
if($endpoint instanceof Application){
return self::ENDPOINT_APP;
}
if(false !== strpos($endpoint, '@')){
return self::ENDPOINT_SIP;
}
if(0 === strpos(strtolower($endpoint), 'http')){
return self::ENDPOINT_VXML;
}
if(preg_match('#[a-z]+#', $endpoint)){
return self::ENDPOINT_APP;
}
return self::ENDPOINT_TEL;
}
public function getVoiceDestination()
{
return $this->fromData('voiceCallbackValue');
}
public function getVoiceType()
{
if(!isset($this->data['voiceCallbackType'])){
return null;
}
return $this->data['voiceCallbackType'];
}
protected function fromData($name)
{
if(!isset($this->data[$name])){
throw new \RuntimeException("`{$name}` has not been set");
}
return $this->data[$name];
}
public function JsonUnserialize(array $json)
{
$this->data = $json;
}
function jsonSerialize()
{
$json = $this->data;
if(isset($json['voiceCallbackValue']) AND ($json['voiceCallbackValue'] instanceof Application)){
$json['voiceCallbackValue'] = $json['voiceCallbackValue']->getId();
}
return $json;
}
public function __toString()
{
return (string) $this->getId();
}
}