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