|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Ups; |
| 4 | + |
| 5 | +use DOMDocument; |
| 6 | +use Exception; |
| 7 | +use Psr\Log\LoggerInterface; |
| 8 | +use SimpleXMLElement; |
| 9 | +use stdClass; |
| 10 | +use Ups\Entity\Address; |
| 11 | +use Ups\Entity\AddressValidationResponse; |
| 12 | + |
| 13 | +/** |
| 14 | + * Address Validation API Wrapper to use the basic Address Validation endpoints. |
| 15 | + * |
| 16 | + * This functionality is more basic, but available in more countries than the 'extended' Address Validation methods. |
| 17 | + */ |
| 18 | +class SimpleAddressValidation extends Ups |
| 19 | +{ |
| 20 | + const ENDPOINT = '/AV'; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var RequestInterface |
| 24 | + */ |
| 25 | + private $request; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var ResponseInterface |
| 29 | + * |
| 30 | + * @todo make private |
| 31 | + */ |
| 32 | + public $response; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var Address |
| 36 | + */ |
| 37 | + private $address; |
| 38 | + |
| 39 | + /** |
| 40 | + * @param string|null $accessKey UPS License Access Key |
| 41 | + * @param string|null $userId UPS User ID |
| 42 | + * @param string|null $password UPS User Password |
| 43 | + * @param bool $useIntegration Determine if we should use production or CIE URLs. |
| 44 | + * @param RequestInterface|null $request |
| 45 | + * @param LoggerInterface|null $logger PSR3 compatible logger (optional) |
| 46 | + */ |
| 47 | + public function __construct( |
| 48 | + $accessKey = null, |
| 49 | + $userId = null, |
| 50 | + $password = null, |
| 51 | + $useIntegration = false, |
| 52 | + RequestInterface $request = null, |
| 53 | + LoggerInterface $logger = null |
| 54 | + ) { |
| 55 | + if (null !== $request) { |
| 56 | + $this->setRequest($request); |
| 57 | + } |
| 58 | + parent::__construct($accessKey, $userId, $password, $useIntegration, $logger); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Get address suggestions from UPS using the default Address Validation API (/AV) |
| 63 | + * |
| 64 | + * @param Address $address |
| 65 | + * |
| 66 | + * @throws Exception |
| 67 | + * |
| 68 | + * @return array |
| 69 | + */ |
| 70 | + public function validate(Address $address) |
| 71 | + { |
| 72 | + $this->address = $address; |
| 73 | + |
| 74 | + $access = $this->createAccess(); |
| 75 | + $request = $this->createRequest(); |
| 76 | + |
| 77 | + $this->response = $this->getRequest()->request($access, $request, $this->compileEndpointUrl(self::ENDPOINT)); |
| 78 | + $response = $this->response->getResponse(); |
| 79 | + |
| 80 | + if (null === $response) { |
| 81 | + throw new Exception('Failure (0): Unknown error', 0); |
| 82 | + } |
| 83 | + |
| 84 | + if ($response instanceof SimpleXMLElement && $response->Response->ResponseStatusCode == 0) { |
| 85 | + throw new Exception( |
| 86 | + "Failure ({$response->Response->Error->ErrorSeverity}): {$response->Response->Error->ErrorDescription}", |
| 87 | + (int)$response->Response->Error->ErrorCode |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + return $this->formatResponse($response); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Create the AV request. |
| 96 | + * |
| 97 | + * @return string |
| 98 | + */ |
| 99 | + private function createRequest() |
| 100 | + { |
| 101 | + $xml = new DOMDocument(); |
| 102 | + $xml->formatOutput = true; |
| 103 | + |
| 104 | + $avRequest = $xml->appendChild($xml->createElement('AddressValidationRequest')); |
| 105 | + $avRequest->setAttribute('xml:lang', 'en-US'); |
| 106 | + |
| 107 | + $request = $avRequest->appendChild($xml->createElement('Request')); |
| 108 | + |
| 109 | + $node = $xml->importNode($this->createTransactionNode(), true); |
| 110 | + $request->appendChild($node); |
| 111 | + |
| 112 | + $request->appendChild($xml->createElement('RequestAction', 'AV')); |
| 113 | + |
| 114 | + if (null !== $this->address) { |
| 115 | + $addressNode = $avRequest->appendChild($xml->createElement('Address')); |
| 116 | + |
| 117 | + if ($this->address->getStateProvinceCode()) { |
| 118 | + $addressNode->appendChild($xml->createElement('StateProvinceCode', $this->address->getStateProvinceCode())); |
| 119 | + } |
| 120 | + if ($this->address->getCity()) { |
| 121 | + $addressNode->appendChild($xml->createElement('City', $this->address->getCity())); |
| 122 | + } |
| 123 | + if ($this->address->getCountryCode()) { |
| 124 | + $addressNode->appendChild($xml->createElement('CountryCode', $this->address->getCountryCode())); |
| 125 | + } |
| 126 | + if ($this->address->getPostalCode()) { |
| 127 | + $addressNode->appendChild($xml->createElement('PostalCode', $this->address->getPostalCode())); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return $xml->saveXML(); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Format the response. |
| 136 | + * |
| 137 | + * @param SimpleXMLElement $response |
| 138 | + * |
| 139 | + * @return array |
| 140 | + */ |
| 141 | + private function formatResponse(SimpleXMLElement $response) |
| 142 | + { |
| 143 | + $result = $this->convertXmlObject($response); |
| 144 | + |
| 145 | + if (!is_array($result->AddressValidationResult)) { |
| 146 | + return [$result->AddressValidationResult]; |
| 147 | + } |
| 148 | + |
| 149 | + return $result->AddressValidationResult; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * @return RequestInterface |
| 154 | + */ |
| 155 | + public function getRequest() |
| 156 | + { |
| 157 | + if (null === $this->request) { |
| 158 | + $this->request = new Request($this->logger); |
| 159 | + } |
| 160 | + |
| 161 | + return $this->request; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * @param RequestInterface $request |
| 166 | + * |
| 167 | + * @return $this |
| 168 | + */ |
| 169 | + public function setRequest(RequestInterface $request) |
| 170 | + { |
| 171 | + $this->request = $request; |
| 172 | + |
| 173 | + return $this; |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * @return ResponseInterface |
| 178 | + */ |
| 179 | + public function getResponse() |
| 180 | + { |
| 181 | + return $this->response; |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * @param ResponseInterface $response |
| 186 | + * |
| 187 | + * @return $this |
| 188 | + */ |
| 189 | + public function setResponse(ResponseInterface $response) |
| 190 | + { |
| 191 | + $this->response = $response; |
| 192 | + |
| 193 | + return $this; |
| 194 | + } |
| 195 | +} |
0 commit comments