|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SmartyStreets; |
|
4
|
|
|
|
|
5
|
|
|
use SmartyStreets\Location\Location; |
|
6
|
|
|
use SmartyStreets\Location\Lookup; |
|
7
|
|
|
use GuzzleHttp\Client; |
|
8
|
|
|
|
|
9
|
|
|
class Service |
|
10
|
|
|
{ |
|
11
|
|
|
protected $authId; |
|
12
|
|
|
|
|
13
|
|
|
protected $authToken; |
|
14
|
|
|
|
|
15
|
|
|
private $simulation = false; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct($authId, $authToken, $simulation = false) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->authId = $authId; |
|
20
|
|
|
$this->authToken = $authToken; |
|
21
|
|
|
$this->setSimulation($simulation); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function setSimulation($value) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->simulation = $value; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function getSimulation() |
|
30
|
|
|
{ |
|
31
|
|
|
return $this->simulation; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function getInspectedLocation($location) |
|
35
|
|
|
{ |
|
36
|
|
|
try { |
|
37
|
|
|
if ($this->getSimulation()) { |
|
38
|
|
|
return new Location($this->getSampleLocation()); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return new Location($this->getRequest(new Lookup($location))); |
|
42
|
|
|
} catch(\Exception $e) { |
|
43
|
|
|
return 'Error: ' .$e->getMessage(); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function getRequest(Lookup $location) |
|
48
|
|
|
{ |
|
49
|
|
|
$client = new Client([ |
|
50
|
|
|
'base_uri' => 'https://international-street.api.smartystreets.com/', |
|
51
|
|
|
]); |
|
52
|
|
|
$response = $client->request('GET', 'verify', [ |
|
53
|
|
|
'query' => [ |
|
54
|
|
|
'auth-id' => $this->authId, |
|
55
|
|
|
'auth-token' => $this->authToken, |
|
56
|
|
|
'address1' => $location->getStreet().' '.$location->getStreetNumber(), |
|
57
|
|
|
'locality' => $location->getPlace(), |
|
58
|
|
|
'postal_code' => $location->getPostcode(), |
|
59
|
|
|
'country' => $location->getCountry(), |
|
60
|
|
|
'geocode' => $location->getGeocode(), |
|
61
|
|
|
], |
|
62
|
|
|
]); |
|
63
|
|
|
|
|
64
|
|
|
$body = json_decode($response->getBody(), true); |
|
65
|
|
|
|
|
66
|
|
|
return reset($body); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
protected function getSampleLocation() |
|
70
|
|
|
{ |
|
71
|
|
|
return [ |
|
72
|
|
|
'address1' => 'Vulkanstrasse 106', |
|
73
|
|
|
'address2' => '8010 Zurich', |
|
74
|
|
|
'components' => [ |
|
75
|
|
|
|
|
76
|
|
|
'administrative_area' => 'ZH', |
|
77
|
|
|
'sub_administrative_area' => 'Zurich', |
|
78
|
|
|
'country_iso_3' => 'CHE', |
|
79
|
|
|
'locality' => 'Vulkanstrasse', |
|
80
|
|
|
'postal_code' => '8304', |
|
81
|
|
|
'postal_code_short' => '8304', |
|
82
|
|
|
'premise' => '10', |
|
83
|
|
|
'premise_number' => '10', |
|
84
|
|
|
'thoroughfare' => 'Vulkanstrasse', |
|
85
|
|
|
|
|
86
|
|
|
], |
|
87
|
|
|
'metadata' => [ |
|
88
|
|
|
|
|
89
|
|
|
'latitude' => 47.41603, |
|
90
|
|
|
'longitude' => 8.57788, |
|
91
|
|
|
'geocode_precision' => 'Premise', |
|
92
|
|
|
'max_geocode_precision' => 'DeliveryPoint', |
|
93
|
|
|
'address_format' => 'thoroughfare premise|postal_code locality', |
|
94
|
|
|
], |
|
95
|
|
|
'analysis' => [ |
|
96
|
|
|
'verification_status' => 'Verified', |
|
97
|
|
|
'address_precision' => 'Premise', |
|
98
|
|
|
'max_address_precision' => 'Premise', |
|
99
|
|
|
], |
|
100
|
|
|
]; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|