1
|
|
|
<?php namespace Ups\Entity; |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
use Ups\AddressValidation; |
5
|
|
|
use Ups\Entity\AddressValidation\AVAddress; |
6
|
|
|
use Ups\Entity\AddressValidation\AddressClassification; |
7
|
|
|
|
8
|
|
|
class AddressValidationResponse |
9
|
|
|
{ |
10
|
|
|
protected $response; |
11
|
|
|
protected $requestAction; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* AddressValidationResponse constructor. |
15
|
|
|
* @param \SimpleXMLElement $xmlDocument |
16
|
|
|
* @param $requestAction |
17
|
|
|
*/ |
18
|
14 |
|
public function __construct(\SimpleXMLElement $xmlDocument,$requestAction) |
|
|
|
|
19
|
|
|
{ |
20
|
14 |
|
$this->response = $xmlDocument; |
21
|
14 |
|
$this->requestAction = $requestAction; |
22
|
14 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Tells whether or not the NoCandidatesIndicator is present on the XML document. |
26
|
|
|
* This indicator is returned if the address is so badly formed that UPS is |
27
|
|
|
* unable to even offer any suggested alternatives |
28
|
|
|
* |
29
|
|
|
* @return bool |
30
|
|
|
*/ |
31
|
5 |
View Code Duplication |
public function noCandidates() |
|
|
|
|
32
|
2 |
|
{ |
33
|
5 |
|
if(AddressValidation::REQUEST_OPTION_ADDRESS_CLASSIFICATION == $this->requestAction) { |
|
|
|
|
34
|
3 |
|
throw new \BadMethodCallException(__METHOD__.' should not be called on Address Classification only requests.'); |
35
|
2 |
|
} |
36
|
2 |
|
return isset($this->response->NoCandidatesIndicator); |
37
|
2 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Tells whether or not the ValidAddressIndicator is present on the XML document. |
41
|
|
|
* This indicator is present if provided address is valid and represents a |
42
|
|
|
* single, unique address in the UPS Address Validation system. |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
3 |
|
public function isValid() |
46
|
|
|
{ |
47
|
3 |
|
if(AddressValidation::REQUEST_OPTION_ADDRESS_CLASSIFICATION == $this->requestAction) { |
|
|
|
|
48
|
1 |
|
return $this->response->AddressClassification->Code > 0; |
49
|
|
|
} |
50
|
2 |
|
return isset($this->response->ValidAddressIndicator); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Tells whether or not the AmbiguousAddressIndicator is present on the XML document. |
55
|
|
|
* This indicator is present when the address provided is not specific enough to |
56
|
|
|
* be unique to one physical location, but provides enough |
57
|
|
|
* |
58
|
|
|
* @throws \BadMethodCallException |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
3 |
View Code Duplication |
public function isAmbiguous() |
|
|
|
|
62
|
|
|
{ |
63
|
3 |
|
if(AddressValidation::REQUEST_OPTION_ADDRESS_CLASSIFICATION == $this->requestAction) { |
|
|
|
|
64
|
1 |
|
throw new \BadMethodCallException(__METHOD__.' should not be called on Address Classification only requests.'); |
65
|
|
|
} |
66
|
2 |
|
return isset($this->response->AmbiguousAddressIndicator); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @throws \BadMethodCallException |
71
|
|
|
* @return AddressClassification |
72
|
|
|
*/ |
73
|
2 |
|
public function getAddressClassification() |
74
|
|
|
{ |
75
|
2 |
|
if($this->requestAction < AddressValidation::REQUEST_OPTION_ADDRESS_CLASSIFICATION) { |
|
|
|
|
76
|
1 |
|
throw new \BadMethodCallException('Address Classification was not requested.'); |
77
|
|
|
} |
78
|
1 |
|
return new AddressClassification($this->response->AddressClassification); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return \Ups\Entity\AddressValidation\Address[] |
83
|
|
|
*/ |
84
|
1 |
|
public function getCandidateAddressList() |
85
|
|
|
{ |
86
|
1 |
|
if(!isset($this->response->AddressKeyFormat)) { |
|
|
|
|
87
|
|
|
return []; |
88
|
|
|
} |
89
|
1 |
|
$candidates = []; |
90
|
1 |
|
foreach($this->response->AddressKeyFormat as $address) { |
|
|
|
|
91
|
1 |
|
$candidates[] = new AVAddress($address); |
92
|
1 |
|
} |
93
|
1 |
|
return $candidates; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
public function getValidatedAddress() |
97
|
|
|
{ |
98
|
1 |
|
if($this->requestAction == AddressValidation::REQUEST_OPTION_ADDRESS_CLASSIFICATION) { |
|
|
|
|
99
|
|
|
throw new \BadMethodCallException('Only Address Classification was requested. There is no address.'); |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
return new AVAddress($this->response->AddressKeyFormat); |
103
|
|
|
} |
104
|
|
|
} |