@@ -25,73 +25,80 @@ public function run() {
2525
2626 $ client = (new ClientBuilder ($ credentials ))
2727 ->buildUsStreetApiClient ();
28- $ batch = new Batch ();
29-
30- $ addressWithStrictStrategy = new Lookup ();
31- $ addressWithStrictStrategy ->setStreet ("691 W 1150 S " );
32- $ addressWithStrictStrategy ->setCity ("provo " );
33- $ addressWithStrictStrategy ->setState ("utah " );
34- $ addressWithStrictStrategy ->setMatchStrategy (Lookup::STRICT );
35-
36- // Uncomment the below line to add a custom parameter to the API call
37- // $addressWithStrictStrategy->addCustomParameter("parameter", "value");
3828
39- $ addressWithInvalidStrategy = new Lookup ();
40- $ addressWithInvalidStrategy ->setStreet ("693 W 1150 S " );
41- $ addressWithInvalidStrategy ->setCity ("provo " );
42- $ addressWithInvalidStrategy ->setState ("utah " );
43- $ addressWithInvalidStrategy ->setMatchStrategy (Lookup::INVALID );
29+ // Each address is run through all three match strategies so you can compare how
30+ // 'strict', 'enhanced', and 'invalid' each handle a valid, an invalid, and an
31+ // ambiguous address.
32+ // - strict: only returns candidates that are valid, mailable addresses.
33+ // - enhanced: returns a more comprehensive dataset (requires a US Core or Rooftop license).
34+ // - invalid: most permissive; always returns at least one candidate (a best-guess standardization).
35+ // Documentation for input fields: https://smartystreets.com/docs/cloud/us-street-api
36+ $ addresses = [
37+ ["valid (real, deliverable) " , "1600 Amphitheatre Pkwy " , "Mountain View " , "CA " , "94043 " ],
38+ ["invalid (no such address) " , "9999 W 1150 S " , "Provo " , "UT " , "84601 " ],
39+ ["ambiguous (missing ZIP/unit) " , "1 Rosedale St " , "Baltimore " , "MD " , "" ],
40+ ];
41+ $ strategies = [Lookup::STRICT , Lookup::ENHANCED , Lookup::INVALID ];
4442
45- $ addressWithEnhancedStrategy = new Lookup ();
46- $ addressWithEnhancedStrategy ->setStreet ("9999 W 1150 S " );
47- $ addressWithEnhancedStrategy ->setCity ("provo " );
48- $ addressWithEnhancedStrategy ->setState ("utah " );
49- $ addressWithEnhancedStrategy ->setMatchStrategy (Lookup::ENHANCED );
43+ $ batch = new Batch ();
44+ $ cases = []; // parallel metadata for each lookup, in the order they are added to the batch
45+
46+ foreach ($ addresses as [$ label , $ street , $ city , $ state , $ zipcode ]) {
47+ foreach ($ strategies as $ strategy ) {
48+ $ lookup = new Lookup ();
49+ $ lookup ->setStreet ($ street );
50+ $ lookup ->setCity ($ city );
51+ $ lookup ->setState ($ state );
52+ $ lookup ->setZipcode ($ zipcode );
53+ $ lookup ->setMatchStrategy ($ strategy );
54+ $ lookup ->setMaxCandidates (10 ); // allow ambiguous addresses to return more than one match
55+ $ batch ->add ($ lookup );
56+ $ cases [] = [$ label , "$ street, $ city, $ state " , $ strategy ];
57+ }
58+ }
5059
5160 try {
52- $ batch ->add ($ addressWithStrictStrategy );
53- $ batch ->add ($ addressWithInvalidStrategy );
54- $ batch ->add ($ addressWithEnhancedStrategy );
55-
5661 $ client ->sendBatch ($ batch );
57- $ this ->displayResults ($ batch );
5862 }
5963 catch (BatchFullException $ ex ) {
6064 echo ("Oops! Batch was already full. " );
65+ return ;
6166 }
6267 catch (\Exception $ ex ) {
6368 echo ($ ex ->getMessage ());
69+ return ;
6470 }
71+
72+ $ this ->displayResults ($ batch , $ cases );
6573 }
6674
67- public function displayResults (Batch $ batch ) {
75+ public function displayResults (Batch $ batch, array $ cases ) {
6876 $ lookups = $ batch ->getAllLookups ();
77+ $ lastAddress = null ;
78+
79+ for ($ i = 0 ; $ i < $ batch ->size (); $ i ++) {
80+ [$ label , $ addressDisplay , $ strategy ] = $ cases [$ i ];
81+
82+ if ($ addressDisplay !== $ lastAddress ) {
83+ echo ("\n" . str_repeat ("= " , 70 ) . "\n" );
84+ echo (" Address: $ addressDisplay [ $ label] \n" );
85+ echo (str_repeat ("= " , 70 ) . "\n" );
86+ $ lastAddress = $ addressDisplay ;
87+ }
6988
70- for ($ i = 0 ; $ i < $ batch ->size (); $ i ++) {
7189 $ candidates = $ lookups [$ i ]->getResult ();
90+ echo ("\n--- ' $ strategy' strategy --- \n" );
7291
7392 if (empty ($ candidates )) {
74- echo ("\n Address " . $ i . " is invalid . \n" );
93+ echo (" 0 candidates - no match returned under this strategy . \n" );
7594 continue ;
7695 }
7796
78- echo ("\nAddress " . $ i . " has at least one candidate. If the match parameter is set to STRICT, the address is valid. Otherwise, check the Analysis output fields to see if the address is valid. \n" );
79-
97+ echo (" " . count ($ candidates ) . " candidate(s): \n" );
8098 foreach ($ candidates as $ candidate ) {
81- $ components = $ candidate ->getComponents ();
82- $ metadata = $ candidate ->getMetadata ();
83-
84- echo ("\n\nCandidate " . $ candidate ->getCandidateIndex () . " " );
85- echo ("with " . $ lookups [$ i ]->getMatchStrategy () . " strategy " );
86- echo ("\nDelivery line 1: " . $ candidate ->getDeliveryLine1 ());
87- echo ("\nLast line: " . $ candidate ->getLastLine ());
88- echo ("\nZIP Code: " . $ components ->getZIPCode () . "- " . $ components ->getPlus4Code ());
89- echo ("\nCounty: " . $ metadata ->getCountyName ());
90- echo ("\nLatitude: " . $ metadata ->getLatitude ());
91- echo ("\nLongitude: " . $ metadata ->getLongitude ());
99+ echo (" [ " . $ candidate ->getCandidateIndex () . "] " . $ candidate ->getDeliveryLine1 () . " " . $ candidate ->getLastLine () . "\n" );
92100 }
93- echo ("\n*********************************** \n" );
94101 }
95102 echo ("\n" );
96103 }
97- }
104+ }
0 commit comments