1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nickcheek\USPSLookup\Service; |
4
|
|
|
|
5
|
|
|
use Nickcheek\USPSLookup\USPSLookup; |
6
|
|
|
|
7
|
|
|
class Address extends USPSLookup |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Verify Address is current and/or inhabited |
11
|
|
|
* @param $address |
12
|
|
|
* @param $address2 |
13
|
|
|
* @param $city |
14
|
|
|
* @param $state |
15
|
|
|
* @param $zip |
16
|
|
|
* @return object |
17
|
|
|
*/ |
18
|
|
|
public static function verify(string $address, string $address2, string $city, string $state, int $zip): object |
19
|
|
|
{ |
20
|
|
|
$Address = new \SimpleXMLElement("<AddressValidateRequest></AddressValidateRequest>"); |
21
|
|
|
$Address->addAttribute('USERID', self::$USPSuser); |
22
|
|
|
$Address->addChild('Revision','1'); |
23
|
|
|
$add = $Address->addChild('Address'); |
24
|
|
|
$add->addAttribute('ID', '0'); |
25
|
|
|
$add->addChild('Address1',$address); |
26
|
|
|
$add->addChild('Address2',$address2); |
27
|
|
|
$add->addChild('City',$city); |
28
|
|
|
$add->addChild('State',$state); |
29
|
|
|
$add->addChild('Zip5',$zip); |
30
|
|
|
$add->addChild('Zip4'); |
31
|
|
|
$url = self::$service . 'Verify&XML=' . $Address->asXML(); |
32
|
|
|
return simplexml_load_file($url); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* City and State lookup from zipcode. |
37
|
|
|
* @param $zip |
38
|
|
|
* @return \SimpleXMLElement |
39
|
|
|
*/ |
40
|
|
View Code Duplication |
public static function cityState(int $zip): object |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$CityState = new \SimpleXMLElement("<CityStateLookupRequest></CityStateLookupRequest>"); |
43
|
|
|
$CityState->addAttribute('USERID', self::$USPSuser); |
44
|
|
|
$ZipCode = $CityState->addChild('ZipCode'); |
45
|
|
|
$ZipCode->addAttribute('ID', '0'); |
46
|
|
|
$ZipCode->addChild('Zip5',$zip); |
47
|
|
|
$url = self::$service . 'CityStateLookup&XML=' . $CityState->asXML(); |
48
|
|
|
return simplexml_load_file($url); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Mulitple city and state lookup |
53
|
|
|
* @param $zip (array) |
54
|
|
|
* @return \SimpleXMLElement |
55
|
|
|
*/ |
56
|
|
|
public static function cityStateMultiple(array $ziparray): object |
57
|
|
|
{ |
58
|
|
|
$CityState = new \SimpleXMLElement("<CityStateLookupRequest></CityStateLookupRequest>"); |
59
|
|
|
$CityState->addAttribute('USERID', self::$USPSuser); |
60
|
|
|
foreach($ziparray as $k=>$v){ |
61
|
|
|
$ZipCode = $CityState->addChild('ZipCode'); |
62
|
|
|
$ZipCode->addAttribute('ID', $k); |
63
|
|
|
$ZipCode->addChild('Zip5',$v); |
64
|
|
|
} |
65
|
|
|
$url = self::$service . 'CityStateLookup&XML=' . $CityState->asXML(); |
66
|
|
|
return simplexml_load_file($url); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Zipcode lookup by Address, City, and State |
71
|
|
|
* @param $address |
72
|
|
|
* @param $address2 |
73
|
|
|
* @param $city |
74
|
|
|
* @param $state |
75
|
|
|
* @return \SimpleXMLElement |
76
|
|
|
*/ |
77
|
|
|
public static function zipCode(string $address,string $address2,string $city,string $state): object |
78
|
|
|
{ |
79
|
|
|
$Address = new \SimpleXMLElement("<ZipCodeLookupRequest></ZipCodeLookupRequest>"); |
80
|
|
|
$Address->addAttribute('USERID', self::$USPSuser); |
81
|
|
|
$add = $Address->addChild('Address'); |
82
|
|
|
$add->addAttribute('ID', '0'); |
83
|
|
|
$add->addChild('Address1',$address); |
84
|
|
|
$add->addChild('Address2',$address2); |
85
|
|
|
$add->addChild('City',$city); |
86
|
|
|
$add->addChild('State',$state); |
87
|
|
|
$url = self::$service . 'ZipCodeLookup&XML=' . $Address->asXML(); |
88
|
|
|
return simplexml_load_file($url); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.