error_reporting(E_ALL);
ini_set('display_errors', 1);
// This is to help new users to Composer
require_once '../vendor/autoload.php';
use AntiMattr\AddressVerifier\Event\EventFactory;
use AntiMattr\AddressVerifier\Exception\Location\CorrectionException;
use AntiMattr\AddressVerifier\Client\BriteVerifyClient as Verifier;
use AntiMattr\Common\Address\GenericAddress as Address;
use Buzz\Client\Curl as RestClient;
use Buzz\Message\Factory\Factory as MessageFactory;
$secretKey = 'YOUR-API-KEY';
$buzz = new RestClient();
$eventFactory = new EventFactory();
$messageFactory = new MessageFactory();
$client = new Verifier($secretKey, $buzz, $eventFactory, $messageFactory);
$address = new Address();
$error = '';
$success = '';
$correction = null;
if ($_POST) {
    try {
        if (!isset($_POST['address'])) {
            throw new \Exception("The address not submitted");
        }
        $data = $_POST['address'];
        $address->setStreetAddressLine1($data['streetAddressLine1']);
        $address->setStreetAddressLine2($data['streetAddressLine2']);
        $address->setLocality($data['locality']);
        $address->setRegion($data['region']);
        $address->setPostalCode($data['postalCode']);
        $client->verifyLocation($address);
 /**
  * @param AntiMattr\Common\Address\AddressInterface $address
  *
  * @return AntiMattr\AddressVerifier\Event\VerificationEvent $event
  *
  * @throws AntiMattr\AddressVerifier\Exception\Connection\ConnectionException
  * @throws AntiMattr\AddressVerifier\Exception\Connection\TimeoutException
  * @throws AntiMattr\AddressVerifier\Exception\IntegrationException
  * @throws AntiMattr\AddressVerifier\Exception\Location\CorrectionException
  * @throws AntiMattr\AddressVerifier\Exception\Location\StreetAddressException     
  */
 public function verifyLocation(AddressInterface $address)
 {
     if (!($streetAddressLine1 = $address->getStreetAddressLine1())) {
         throw new IntegrationException('Verify address missing street address line 1');
     }
     if (!($locality = $address->getLocality())) {
         throw new IntegrationException('Verify address missing locality');
     }
     if (!($region = $address->getRegion())) {
         throw new IntegrationException('Verify address missing region');
     }
     if (!($postalCode = $address->getPostalCode())) {
         throw new IntegrationException('Verify address missing postal code');
     }
     $streetAddressLine2 = $address->getStreetAddressLine2() ?: '';
     $params = array('address[street]' => $streetAddressLine1, 'address[unit]' => $streetAddressLine2, 'address[city]' => $locality, 'address[state]' => $region, 'address[zip]' => $postalCode, 'apikey' => $this->apiKey);
     $query = http_build_query($params);
     $resource = sprintf("/addresses.json?%s", $query);
     $request = $this->messageFactory->createRequest('GET', $resource, 'https://bpi.briteverify.com');
     $response = $this->messageFactory->createResponse();
     $event = $this->eventFactory->createEvent($address);
     $this->buzz->setTimeout($this->timeout);
     $this->log($request);
     try {
         $this->buzz->send($request, $response);
         $this->log($response);
         $statusCode = $response->getStatusCode();
         if ($statusCode !== 200) {
             $message = $response->getReasonPhrase();
             throw new IntegrationException($message);
         }
         $content = json_decode($response->getContent());
         if ($content->status === 'invalid') {
             $message = $content->error;
             throw new StreetAddressException($message);
         }
         if ($content->corrected === true) {
             $newAddress = new GenericAddress();
             if (isset($content->street)) {
                 $newAddress->setStreetAddressLine1($content->street);
             }
             if (isset($content->unit)) {
                 $newAddress->setStreetAddressLine2($content->unit);
             }
             if (isset($content->city)) {
                 $newAddress->setLocality($content->city);
             }
             if (isset($content->state_code)) {
                 $newAddress->setRegion($content->state_code);
             }
             if (isset($content->zip)) {
                 $postalCode = isset($content->plus4) ? $content->zip . '-' . $content->plus4 : $content->zip;
                 $newAddress->setPostalCode($postalCode);
             }
             if (isset($content->country_code)) {
                 $newAddress->setCountry($content->country_code);
             }
             throw new CorrectionException($newAddress);
         }
     } catch (ClientException $e) {
         $subject = $e->getMessage();
         if (0 !== preg_match('/timed out/', $subject)) {
             throw new TimeoutException($subject);
         }
         throw new ConnectionException($subject);
     }
     return $event;
 }
 /**
  * @param SimpleXMLElement $addrEl
  *
  * @return AntiMattr\Common\Address\AddressInterface
  */
 public static function addressFromXml(SimpleXMLElement $addrEl)
 {
     $addr = new GenericAddress();
     $addr->setName((string) $addrEl->{'Name'});
     $addr->setStreetAddressLine1((string) $addrEl->{'AddressFieldOne'});
     $addr->setStreetAddressLine2((string) $addrEl->{'AddressFieldTwo'});
     $addr->setLocality((string) $addrEl->{'City'});
     $addr->setRegion((string) $addrEl->{'StateOrRegion'});
     $addr->setPostalCode((string) $addrEl->{'PostalCode'});
     $addr->setCountry((string) $addrEl->{'CountryCode'});
     $addr->setPhone((string) $addrEl->{'PhoneNumber'});
     return $addr;
 }