Skip to content

Commit 64368ae

Browse files
committed
Add support for number insights
* Basic * Standard * Standard CNAM * Advanced * Advanced CNAM * Advanced Async ArrayAccess/Objects are implemented for all top level keys. Nested keys are simple arrays for now (we can add objects later if required)
1 parent 3b8150d commit 64368ae

19 files changed

+831
-0
lines changed

src/Client.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public function __construct(CredentialsInterface $credentials, $options = array(
9191
}
9292

9393
$this->setFactory(new MapFactory([
94+
'insights' => 'Nexmo\Insights\Client',
9495
'message' => 'Nexmo\Message\Client',
9596
'verify' => 'Nexmo\Verify\Client',
9697
'applications' => 'Nexmo\Application\Client',

src/Insights/Advanced.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
namespace Nexmo\Insights;
3+
4+
class Advanced extends Standard
5+
{
6+
public function getValidNumber()
7+
{
8+
return $this['valid_number'];
9+
}
10+
11+
public function getReachable()
12+
{
13+
return $this['reachable'];
14+
}
15+
}

src/Insights/AdvancedCnam.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace Nexmo\Insights;
3+
4+
class AdvancedCnam extends Advanced
5+
{
6+
use CnamTrait;
7+
}

src/Insights/Basic.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
namespace Nexmo\Insights;
4+
5+
use Nexmo\Client\Exception\Exception;
6+
use Nexmo\Entity\JsonUnserializableInterface;
7+
8+
class Basic implements \JsonSerializable, JsonUnserializableInterface, \ArrayAccess {
9+
10+
protected $data = [];
11+
12+
public function __construct($number)
13+
{
14+
$this->data['national_format_number'] = $number;
15+
}
16+
17+
/**
18+
* @return string
19+
*/
20+
public function getRequestId()
21+
{
22+
return $this['request_id'];
23+
}
24+
25+
/**
26+
* @return string
27+
*/
28+
public function getNationalFormatNumber()
29+
{
30+
return $this['national_format_number'];
31+
}
32+
33+
/**
34+
* @return string
35+
*/
36+
public function getInternationalFormatNumber()
37+
{
38+
return $this['international_format_number'];
39+
}
40+
41+
/**
42+
* @return string
43+
*/
44+
public function getCountryCode()
45+
{
46+
return $this['country_code'];
47+
}
48+
49+
/**
50+
* @return string
51+
*/
52+
public function getCountryCodeISO3()
53+
{
54+
return $this['country_code_iso3'];
55+
}
56+
57+
/**
58+
* @return string
59+
*/
60+
public function getCountryName()
61+
{
62+
return $this['country_name'];
63+
}
64+
65+
/**
66+
* @return integer
67+
*/
68+
public function getCountryPrefix()
69+
{
70+
return $this['country_prefix'];
71+
}
72+
73+
public function jsonSerialize()
74+
{
75+
return $this->data;
76+
}
77+
78+
public function jsonUnserialize(array $json)
79+
{
80+
$this->data = $json;
81+
}
82+
83+
public function offsetExists($offset)
84+
{
85+
return isset($this->data[$offset]);
86+
}
87+
88+
public function offsetGet($offset)
89+
{
90+
return $this->data[$offset];
91+
}
92+
93+
public function offsetSet($offset, $value)
94+
{
95+
throw new Exception('Number insights results are read only');
96+
}
97+
98+
public function offsetUnset($offset)
99+
{
100+
throw new Exception('Number insights results are read only');
101+
}
102+
}

src/Insights/Client.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
/**
3+
* Nexmo Client Library for PHP
4+
*
5+
* @copyright Copyright (c) 2016 Nexmo, Inc. (http://nexmo.com)
6+
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
7+
*/
8+
9+
namespace Nexmo\Insights;
10+
11+
use Nexmo\Client\ClientAwareInterface;
12+
use Nexmo\Client\ClientAwareTrait;
13+
use Nexmo\Client\Exception;
14+
use Zend\Diactoros\Request;
15+
16+
/**
17+
* Class Client
18+
*/
19+
class Client implements ClientAwareInterface
20+
{
21+
use ClientAwareTrait;
22+
23+
public function basic($number)
24+
{
25+
$insightsResults = $this->makeRequest('/ni/basic/json', $number);
26+
27+
$basic = new Basic($insightsResults['national_format_number']);
28+
$basic->jsonUnserialize($insightsResults);
29+
return $basic;
30+
}
31+
32+
public function standardCNam($number)
33+
{
34+
$insightsResults = $this->makeRequest('/ni/standard/json', $number, ['cnam' => 'true']);
35+
$standard = new StandardCnam($insightsResults['national_format_number']);
36+
$standard->jsonUnserialize($insightsResults);
37+
return $standard;
38+
}
39+
40+
public function advancedCnam($number)
41+
{
42+
$insightsResults = $this->makeRequest('/ni/advanced/json', $number, ['cnam' => 'true']);
43+
$standard = new AdvancedCnam($insightsResults['national_format_number']);
44+
$standard->jsonUnserialize($insightsResults);
45+
return $standard;
46+
}
47+
48+
public function standard($number, $useCnam=false)
49+
{
50+
$insightsResults = $this->makeRequest('/ni/standard/json', $number);
51+
$standard = new Standard($insightsResults['national_format_number']);
52+
$standard->jsonUnserialize($insightsResults);
53+
return $standard;
54+
}
55+
56+
public function advanced($number)
57+
{
58+
$insightsResults = $this->makeRequest('/ni/advanced/json', $number);
59+
$advanced = new Advanced($insightsResults['national_format_number']);
60+
$advanced->jsonUnserialize($insightsResults);
61+
return $advanced;
62+
}
63+
64+
public function advancedAsync($number, $webhook)
65+
{
66+
// This method does not have a return value as it's async. If there is no exception thrown
67+
// We can assume that everything is fine
68+
$this->makeRequest('/ni/advanced/async/json', $number, ['callback' => $webhook]);
69+
}
70+
71+
public function makeRequest($path, $number, $additionalParams = [])
72+
{
73+
if ($number instanceof Number)
74+
{
75+
$number = $number->getMsisdn();
76+
}
77+
78+
$queryString = http_build_query([
79+
'number' => $number,
80+
] + $additionalParams);
81+
82+
$request = new Request(
83+
\Nexmo\Client::BASE_API . $path.'?'.$queryString,
84+
'GET',
85+
'php://temp',
86+
[
87+
'Accept' => 'application/json'
88+
]
89+
);
90+
91+
$response = $this->client->send($request);
92+
93+
$insightsResults = json_decode($response->getBody()->getContents(), true);
94+
95+
if('200' != $response->getStatusCode()){
96+
throw $this->getException($response);
97+
}
98+
99+
return $insightsResults;
100+
}
101+
102+
protected function getException(ResponseInterface $response)
103+
{
104+
$body = json_decode($response->getBody()->getContents(), true);
105+
$status = $response->getStatusCode();
106+
107+
if($status >= 400 AND $status < 500) {
108+
$e = new Exception\Request($body['error-code-label'], $status);
109+
} elseif($status >= 500 AND $status < 600) {
110+
$e = new Exception\Server($body['error-code-label'], $status);
111+
} else {
112+
$e = new Exception\Exception('Unexpected HTTP Status Code');
113+
throw $e;
114+
}
115+
116+
return $e;
117+
}
118+
}

src/Insights/CnamTrait.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
namespace Nexmo\Insights;
3+
4+
trait CnamTrait {
5+
6+
public function getCallerName()
7+
{
8+
return $this['caller_name'];
9+
}
10+
11+
public function getFirstName()
12+
{
13+
return $this['first_name'];
14+
}
15+
16+
public function getLastName()
17+
{
18+
return $this['last_name'];
19+
}
20+
21+
public function getCallerType()
22+
{
23+
return $this['caller_type'];
24+
}
25+
}

src/Insights/Standard.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
namespace Nexmo\Insights;
3+
4+
class Standard extends Basic
5+
{
6+
public function getCurrentCarrier()
7+
{
8+
return $this['current_carrier'];
9+
}
10+
11+
public function getOriginalCarrier()
12+
{
13+
return $this['original_carrier'];
14+
}
15+
16+
public function getPorted()
17+
{
18+
return $this['ported'];
19+
}
20+
21+
public function getRoaming()
22+
{
23+
return $this['roaming'];
24+
}
25+
}

src/Insights/StandardCnam.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace Nexmo\Insights;
3+
4+
class StandardCnam extends Standard
5+
{
6+
use CnamTrait;
7+
}

test/Insights/AdvancedTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* Nexmo Client Library for PHP
4+
*
5+
* @copyright Copyright (c) 2016 Nexmo, Inc. (http://nexmo.com)
6+
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
7+
*/
8+
9+
namespace NexmoTest\Insights;
10+
11+
use Nexmo\Insights\Advanced;
12+
13+
class AdvancedTest extends \PHPUnit_Framework_TestCase
14+
{
15+
16+
/**
17+
* @dataProvider advancedTestProvider
18+
*/
19+
public function testArrayAccess($advanced, $inputData)
20+
{
21+
$this->assertEquals($inputData['valid_number'], $advanced['valid_number']);
22+
$this->assertEquals($inputData['reachable'], $advanced['reachable']);
23+
}
24+
25+
/**
26+
* @dataProvider advancedTestProvider
27+
*/
28+
public function testObjectAccess($advanced, $inputData)
29+
{
30+
$this->assertEquals($inputData['valid_number'], $advanced->getValidNumber());
31+
$this->assertEquals($inputData['reachable'], $advanced->getReachable());
32+
}
33+
34+
public function advancedTestProvider()
35+
{
36+
$r = [];
37+
38+
$input1 = [
39+
'valid_number' => 'valid',
40+
'reachable' => 'unknown'
41+
];
42+
43+
$advanced1 = new Advanced('01234567890');
44+
$advanced1->jsonUnserialize($input1);
45+
$r['standard-1'] = [$advanced1, $input1];
46+
47+
return $r;
48+
}
49+
}

0 commit comments

Comments
 (0)