1
|
|
|
<?php namespace SimpleUPS\RegionValidate; |
2
|
|
|
|
3
|
|
|
use \SimpleUPS\Address; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @internal |
7
|
|
|
*/ |
8
|
|
|
class Response extends \SimpleUPS\Api\Response |
9
|
|
|
{ |
10
|
|
|
private |
11
|
|
|
$address, |
12
|
|
|
$suggestedRegions; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @param \SimpleUPS\Address $address |
16
|
|
|
*/ |
17
|
|
|
public function __construct(Address $address) |
18
|
|
|
{ |
19
|
|
|
$this->address = $address; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Determine if the given address region (city, state and postal code) are valid |
24
|
|
|
* @return bool |
25
|
|
|
*/ |
26
|
|
|
public function isRegionValid() |
27
|
|
|
{ |
28
|
|
|
$suggestedRegions = $this->getSuggestedRegions(); |
29
|
|
|
return count($suggestedRegions) == 1 && $suggestedRegions[0]->getQuality() == 1; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return Address |
34
|
|
|
*/ |
35
|
|
|
public function getAddress() |
36
|
|
|
{ |
37
|
|
|
return $this->address; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param RegionSuggestion $suggestedRegion |
42
|
|
|
* |
43
|
|
|
* @return Response |
44
|
|
|
*/ |
45
|
|
|
public function addSuggestedRegion(RegionSuggestion $suggestedRegion) |
46
|
|
|
{ |
47
|
|
|
if ($this->suggestedRegions == null) { |
48
|
|
|
$this->suggestedRegions = array(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->suggestedRegions[] = $suggestedRegion; |
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return RegionSuggestion |
57
|
|
|
*/ |
58
|
|
|
public function getSuggestedRegions() |
59
|
|
|
{ |
60
|
|
|
return $this->suggestedRegions; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param \SimpleXMLElement $xml |
65
|
|
|
* |
66
|
|
|
* @return Response |
67
|
|
|
*/ |
68
|
|
|
public function fromXml(\SimpleXMLElement $xml) |
69
|
|
|
{ |
70
|
|
|
$response = new Response($this->getAddress()); |
71
|
|
|
foreach ($xml->AddressValidationResult as $rating) { |
72
|
|
|
$address = new Address(); |
73
|
|
|
$address->setIsResponse(); |
74
|
|
|
$address |
75
|
|
|
->setCity((string)$rating->Address->City) |
76
|
|
|
->setStateProvinceCode((string)$rating->Address->StateProvinceCode); |
77
|
|
|
$regionSuggestion = new RegionSuggestion(); |
78
|
|
|
$regionSuggestion->setRank((int)$rating->Rank); |
79
|
|
|
$regionSuggestion->setQuality((float)$rating->Quality); |
80
|
|
|
$regionSuggestion->setAddress($address); |
81
|
|
|
$regionSuggestion->setPostalCodeLowEnd((string)$rating->PostalCodeLowEnd); |
82
|
|
|
$regionSuggestion->setPostalCodeHighEnd((string)$rating->PostalCodeHighEnd); |
83
|
|
|
|
84
|
|
|
$response->addSuggestedRegion($regionSuggestion); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $response; |
88
|
|
|
} |
89
|
|
|
} |