Skip to content

Commit e8940f0

Browse files
authored
Merge pull request #88 from smartystreets/mae/match-strategy-examples
added/updated us_street match strategy examples
2 parents 25d40d1 + d0c9631 commit e8940f0

2 files changed

Lines changed: 54 additions & 44 deletions

File tree

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ us_reverse_geo_api:
4242
us_street_iana_timezone_api:
4343
php examples/UsStreetIanaTimeZoneExample.php
4444

45+
us_street_match_strategy_api:
46+
php examples/USStreetLookupsWithMatchStrategyExamples.php
47+
4548
us_street_api:
4649
php examples/UsStreetSingleAddressExample.php && php examples/UsStreetMultipleAddressesExample.php && php examples/UsStreetComponentAnalysisExample.php && php examples/UsStreetIanaTimeZoneExample.php && php examples/USStreetLookupsWithMatchStrategyExamples.php
4750

@@ -50,4 +53,4 @@ us_zipcode_api:
5053

5154
examples: international_autocomplete_api international_postal_code_api international_street_api us_autocomplete_pro_api us_enrichment_api us_extract_api us_reverse_geo_api us_street_api us_zipcode_api
5255

53-
.PHONY: test package release examples international_autocomplete_api international_postal_code_api international_street_api us_autocomplete_pro_api us_enrichment_api us_enrichment_business_api us_enrichment_etag us_extract_api us_reverse_geo_api us_street_api us_street_iana_timezone_api us_zipcode_api
56+
.PHONY: test package release examples international_autocomplete_api international_postal_code_api international_street_api us_autocomplete_pro_api us_enrichment_api us_enrichment_business_api us_enrichment_etag us_extract_api us_reverse_geo_api us_street_api us_street_iana_timezone_api us_street_match_strategy_api us_zipcode_api

examples/USStreetLookupsWithMatchStrategyExamples.php

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -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("\nAddress " . $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

Comments
 (0)