Skip to content

Commit 042815d

Browse files
authored
Merge pull request #58 from smartystreets/eric/enrichment-address-search
Added Address Search Feature for the US Address Enrichment API
2 parents d936041 + 52e2721 commit 042815d

File tree

7 files changed

+334
-39
lines changed

7 files changed

+334
-39
lines changed

examples/USEnrichmentExample.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
require_once(dirname(dirname(__FILE__)) . '/src/StaticCredentials.php');
44
require_once(dirname(dirname(__FILE__)) . '/src/ClientBuilder.php');
55
require_once(dirname(dirname(__FILE__)) . '/src/US_Enrichment/Client.php');
6+
require_once(dirname(dirname(__FILE__)) . '/src/US_Enrichment/Lookup.php');
67

78
use SmartyStreets\PhpSdk\StaticCredentials;
89
use SmartyStreets\PhpSdk\ClientBuilder;
910
use SmartyStreets\PhpSdk\US_Enrichment\Result;
11+
use SmartyStreets\PhpSdk\US_Enrichment\Lookup;
1012

1113
$lookupExample = new USEnrichmentExample();
1214
$lookupExample->run();
@@ -31,10 +33,25 @@ public function run()
3133
$client = (new ClientBuilder($staticCredentials)) ->withLicenses(["us-property-data-principal-cloud"])
3234
->buildUsEnrichmentApiClient();
3335

34-
$smartyKey = "1682393594";
36+
$smartyKey = "325023201";
37+
38+
$lookup = new Lookup();
39+
40+
$lookup->setStreet("56 Union Ave");
41+
$lookup->setCity("Somerville");
42+
$lookup->setState("NJ");
43+
$lookup->setZipcode("08876");
44+
45+
// You can also send an address in freeform by uncommenting the line below
46+
// $lookup->setFreeform("56 Union Ave Somerville NJ 08876");
3547

3648
try {
49+
// Call the API with only a smarty key using the line below
3750
$result = $client->sendPropertyPrincipalLookup($smartyKey);
51+
52+
// Or call the API with an address using the lookup object with the commented line below
53+
// $result = $client->sendPropertyPrincipalLookup($lookup);
54+
3855
if ($result != null) {
3956
$this->displayResult($result[0]);
4057
}

src/US_Enrichment/Client.php

Lines changed: 108 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,40 +21,106 @@ public function __construct(Sender $sender, Serializer $serializer = null) {
2121
$this->serializer = $serializer;
2222
}
2323

24-
public function sendPropertyFinancialLookup($smartyKey){
25-
$lookup = new Lookup($smartyKey, "property", "financial");
26-
$this->sendLookup($lookup);
27-
return $lookup->getResponse();
24+
public function sendPropertyFinancialLookup($financialLookup){
25+
if (is_string($financialLookup)) {
26+
$lookup = new Lookup($financialLookup, "property", "financial");
27+
$this->sendLookup($lookup);
28+
return $lookup->getResponse();
29+
}
30+
else if (is_object($financialLookup)) {
31+
$financialLookup->setDataSetName("property");
32+
$financialLookup->setDataSubSetName("financial");
33+
$this->sendLookup($financialLookup);
34+
return $financialLookup->getResponse();
35+
}
36+
else {
37+
return null;
38+
}
2839
}
2940

30-
public function sendPropertyPrincipalLookup($smartyKey){
31-
$lookup = new Lookup($smartyKey, "property", "principal");
32-
$this->sendLookup($lookup);
33-
return $lookup->getResponse();
41+
public function sendPropertyPrincipalLookup($principalLookup){
42+
if (is_string($principalLookup)) {
43+
$lookup = new Lookup($principalLookup, "property", "principal");
44+
$this->sendLookup($lookup);
45+
return $lookup->getResponse();
46+
}
47+
else if (is_object($principalLookup)) {
48+
$principalLookup->setDataSetName("property");
49+
$principalLookup->setDataSubSetName("principal");
50+
$this->sendLookup($principalLookup);
51+
return $principalLookup->getResponse();
52+
}
53+
else {
54+
return null;
55+
}
3456
}
3557

36-
public function sendGeoReferenceLookup($smartyKey){
37-
$lookup = new Lookup($smartyKey, "geo-reference", null);
38-
$this->sendLookup($lookup);
39-
return $lookup->getResponse();
58+
public function sendGeoReferenceLookup($geoReferenceLookup){
59+
if (is_string($geoReferenceLookup)) {
60+
$lookup = new Lookup($geoReferenceLookup, "geo-reference");
61+
$this->sendLookup($lookup);
62+
return $lookup->getResponse();
63+
}
64+
else if (is_object($geoReferenceLookup)) {
65+
$geoReferenceLookup->setDataSetName("geo-reference");
66+
$geoReferenceLookup->setDataSubSetName(null);
67+
$this->sendLookup($geoReferenceLookup);
68+
return $geoReferenceLookup->getResponse();
69+
}
70+
else {
71+
return null;
72+
}
4073
}
4174

42-
public function sendSecondaryLookup($smartyKey){
43-
$lookup = new Lookup($smartyKey, "secondary", null);
44-
$this->sendLookup($lookup);
45-
return $lookup->getResponse();
75+
public function sendSecondaryLookup($secondaryLookup){
76+
if (is_string($secondaryLookup)) {
77+
$lookup = new Lookup($secondaryLookup, "secondary");
78+
$this->sendLookup($lookup);
79+
return $lookup->getResponse();
80+
}
81+
else if (is_object($secondaryLookup)) {
82+
$secondaryLookup->setDataSetName("secondary");
83+
$secondaryLookup->setDataSubSetName(null);
84+
$this->sendLookup($secondaryLookup);
85+
return $secondaryLookup->getResponse();
86+
}
87+
else {
88+
return null;
89+
}
4690
}
4791

48-
public function sendSecondaryCountLookup($smartyKey){
49-
$lookup = new Lookup($smartyKey, "secondary", "count");
50-
$this->sendLookup($lookup);
51-
return $lookup->getResponse();
92+
public function sendSecondaryCountLookup($secondaryCountLookup){
93+
if (is_string($secondaryCountLookup)) {
94+
$lookup = new Lookup($secondaryCountLookup, "secondary", "count");
95+
$this->sendLookup($lookup);
96+
return $lookup->getResponse();
97+
}
98+
else if (is_object($secondaryCountLookup)) {
99+
$secondaryCountLookup->setDataSetName("secondary");
100+
$secondaryCountLookup->setDataSubSetName("count");
101+
$this->sendLookup($secondaryCountLookup);
102+
return $secondaryCountLookup->getResponse();
103+
}
104+
else {
105+
return null;
106+
}
52107
}
53108

54-
public function sendGenericLookup($smartyKey, $dataSetName, $dataSubsetName){
55-
$lookup = new Lookup($smartyKey, $dataSetName, $dataSubsetName);
56-
$this->sendLookup($lookup);
57-
return $lookup->getResponse();
109+
public function sendGenericLookup($genericLookup, $dataSetName, $dataSubsetName){
110+
if (is_string($genericLookup)) {
111+
$lookup = new Lookup($genericLookup, $dataSetName, $dataSubsetName);
112+
$this->sendLookup($lookup);
113+
return $lookup->getResponse();
114+
}
115+
else if (is_object($genericLookup)) {
116+
$genericLookup->setDataSetName($dataSetName);
117+
$genericLookup->setDataSubSetName($dataSubsetName);
118+
$this->sendLookup($genericLookup);
119+
return $genericLookup->getResponse();
120+
}
121+
else {
122+
return null;
123+
}
58124
}
59125

60126
private function sendLookup(Lookup $lookup) {
@@ -83,13 +149,28 @@ private function buildRequest(Lookup $lookup) {
83149

84150
$request->setUrlComponents($this->getUrlPrefix($lookup));
85151

152+
if ($lookup->getSmartyKey() == null) {
153+
$request->setParameter("freeform", $lookup->getFreeform());
154+
$request->setParameter("street", $lookup->getStreet());
155+
$request->setParameter("city", $lookup->getCity());
156+
$request->setParameter("state", $lookup->getState());
157+
$request->setParameter("zipcode", $lookup->getZipcode());
158+
}
86159
return $request;
87160
}
88161

89162
private function getUrlPrefix($lookup){
90-
if ($lookup->getDataSubsetName() == null) {
91-
return $lookup->getSmartyKey() . "/" . $lookup->getDataSetName();
163+
if ($lookup->getSmartyKey() == null) {
164+
if ($lookup->getDataSubsetName() == null) {
165+
return "search/" . $lookup->getDataSetName();
166+
}
167+
return "search/" . $lookup->getDataSetName() . "/" . $lookup->getDataSubsetName();
168+
}
169+
else {
170+
if ($lookup->getDataSubsetName() == null) {
171+
return $lookup->getSmartyKey() . "/" . $lookup->getDataSetName();
172+
}
173+
return $lookup->getSmartyKey() . "/" . $lookup->getDataSetName() . "/" . $lookup->getDataSubsetName();
92174
}
93-
return $lookup->getSmartyKey() . "/" . $lookup->getDataSetName() . "/" . $lookup->getDataSubsetName();
94175
}
95176
}

src/US_Enrichment/Lookup.php

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,54 @@ class Lookup {
77
//region [ Fields ]
88

99
private $smartyKey,
10+
$freeform,
11+
$street,
12+
$city,
13+
$state,
14+
$zipcode,
1015
$dataSetName,
1116
$dataSubsetName,
1217
$response;
1318

1419
//endregion
1520

16-
public function __construct($smartyKey, $dataSetName, $dataSubsetName) {
21+
public function __construct($smartyKey = null, $dataSetName = null, $dataSubsetName = null, $freeform = null, $street = null, $city = null, $state = null,
22+
$zipcode = null) {
1723
$this->smartyKey = $smartyKey;
1824
$this->dataSetName = $dataSetName;
1925
$this->dataSubsetName = $dataSubsetName;
26+
$this->freeform = $freeform;
27+
$this->street = $street;
28+
$this->city = $city;
29+
$this->state = $state;
30+
$this->zipcode = $zipcode;
2031
$this->response = null;
21-
2232
}
2333

2434
public function getSmartyKey(){
2535
return $this->smartyKey;
2636
}
2737

38+
public function getFreeform(){
39+
return $this->freeform;
40+
}
41+
42+
public function getStreet(){
43+
return $this->street;
44+
}
45+
46+
public function getCity(){
47+
return $this->city;
48+
}
49+
50+
public function getState(){
51+
return $this->state;
52+
}
53+
54+
public function getZipcode(){
55+
return $this->zipcode;
56+
}
57+
2858
public function getDataSetName(){
2959
return $this->dataSetName;
3060
}
@@ -37,6 +67,38 @@ public function getResponse() {
3767
return $this->response;
3868
}
3969

70+
public function setSmartyKey($smartyKey) {
71+
$this->smartyKey = $smartyKey;
72+
}
73+
74+
public function setDataSetName($dataSetName) {
75+
$this->dataSetName = $dataSetName;
76+
}
77+
78+
public function setDataSubsetName($dataSubsetName) {
79+
$this->dataSubsetName = $dataSubsetName;
80+
}
81+
82+
public function setFreeform($freeform) {
83+
$this->freeform = $freeform;
84+
}
85+
86+
public function setStreet($street){
87+
$this->street = $street;
88+
}
89+
90+
public function setCity($city){
91+
$this->city = $city;
92+
}
93+
94+
public function setState($state){
95+
$this->state = $state;
96+
}
97+
98+
public function setZipcode($zipcode){
99+
$this->zipcode = $zipcode;
100+
}
101+
40102
public function setResponse($response){
41103
$this->response = $response;
42104
}

src/US_Enrichment/Result.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,15 @@ private function createAttributes($dataSetName, $dataSubsetName, $attributesObj)
6464
}
6565

6666
private function createSecondaryData($responseObj, $dataSubsetName) {
67-
if ($dataSubsetName = 'count'){
67+
if ($dataSubsetName == 'count'){
6868
$attributes = new SecondaryCountAttributes($responseObj);
6969
$this->count = $attributes->count;
7070
}
71-
$attributes = new SecondaryAttributes($responseObj);
72-
$this->rootAddress = $attributes->rootAddress;
73-
$this->aliases[] = $attributes->aliases;
74-
$this->secondaries[] = $attributes->secondaries;
71+
else {
72+
$attributes = new SecondaryAttributes($responseObj);
73+
$this->rootAddress = $attributes->rootAddress;
74+
$this->aliases[] = $attributes->aliases;
75+
$this->secondaries[] = $attributes->secondaries;
76+
}
7577
}
7678
}

src/US_Enrichment/SecondaryAttributes.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,16 @@ private function createRootAddress($rootAddress){
3434
}
3535

3636
private function createAliases($aliasesArray){
37-
foreach($aliasesArray as $value){
38-
$this->aliases = new AliasesEntry($value);
37+
if ($aliasesArray != null) {
38+
foreach($aliasesArray as $value){
39+
$this->aliases[] = new AliasesEntry($value);
40+
}
3941
}
4042
}
4143

4244
private function createSecondaries($secondariesArray){
4345
foreach($secondariesArray as $value){
44-
$this->secondaries = new SecondariesEntry($value);
46+
$this->secondaries[] = new SecondariesEntry($value);
4547
}
4648
}
4749
}

tests/US_Enrichment/ClientTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
require_once(dirname(dirname(__FILE__)) . '/Mocks/MockSender.php');
1010
require_once(dirname(dirname(__FILE__)) . '/Mocks/MockCrashingSender.php');
1111
require_once(dirname(dirname(dirname(__FILE__))) . '/src/US_Enrichment/Client.php');
12+
require_once(dirname(dirname(dirname(__FILE__))) . '/src/US_Enrichment/Lookup.php');
1213
require_once(dirname(dirname(dirname(__FILE__))) . '/src/URLPrefixSender.php');
1314
use SmartyStreets\PhpSdk\Tests\Mocks\MockSerializer;
1415
use SmartyStreets\PhpSdk\Tests\Mocks\RequestCapturingSender;
1516
use SmartyStreets\PhpSdk\URLPrefixSender;
1617
use SmartyStreets\PhpSdk\US_Enrichment\Client;
18+
use SmartyStreets\PhpSdk\US_Enrichment\Lookup;
1719
use PHPUnit\Framework\TestCase;
1820

1921
class ClientTest extends TestCase {
@@ -28,4 +30,32 @@ public function testSendingLookup() {
2830

2931
$this->assertEquals("http://localhost/123/property/principal?", $capturingSender->getRequest()->getUrl());
3032
}
33+
public function testSendingAddressComponentLookup() {
34+
$capturingSender = new RequestCapturingSender();
35+
$sender = new URLPrefixSender("http://localhost/", $capturingSender);
36+
$serializer = new MockSerializer(null);
37+
$client = new Client($sender, $serializer);
38+
$lookup = new Lookup();
39+
$lookup->setStreet("123 Test Street");
40+
$lookup->setCity("Test City");
41+
$lookup->setState("Test State");
42+
$lookup->setZipcode("Test Zipcode");
43+
44+
$client->sendPropertyPrincipalLookup($lookup);
45+
46+
$this->assertEquals("http://localhost/search/property/principal?street=123+Test+Street&city=Test+City&state=Test+State&zipcode=Test+Zipcode", $capturingSender->getRequest()->getUrl());
47+
}
48+
49+
public function testSendingFreeformLookup() {
50+
$capturingSender = new RequestCapturingSender();
51+
$sender = new URLPrefixSender("http://localhost/", $capturingSender);
52+
$serializer = new MockSerializer(null);
53+
$client = new Client($sender, $serializer);
54+
$lookup = new Lookup();
55+
$lookup->setFreeform("123 Test Street City State Zipcode");
56+
57+
$client->sendPropertyPrincipalLookup($lookup);
58+
59+
$this->assertEquals("http://localhost/search/property/principal?freeform=123+Test+Street+City+State+Zipcode", $capturingSender->getRequest()->getUrl());
60+
}
3161
}

0 commit comments

Comments
 (0)