Skip to content

Commit 6f871bb

Browse files
author
Zack Teska
committed
added hash without zip
1 parent a52aa51 commit 6f871bb

2 files changed

Lines changed: 46 additions & 6 deletions

File tree

src/Address.php

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Address
2020
protected $suffix2;
2121
protected $street2;
2222

23+
protected $hash;
2324
protected $fullHash;
2425
protected $streetHash;
2526

@@ -138,7 +139,7 @@ public function getStreetHash(string $algo = 'sha1'): string
138139
}
139140

140141
/**
141-
* Gets the full hash (of all address components)
142+
* Gets the full hash (of all address components, including zip code)
142143
*
143144
* @param string $algo
144145
*
@@ -153,16 +154,43 @@ public function getFullHash(string $algo = 'sha1'): string
153154
return $this->fullHash;
154155
}
155156

157+
/**
158+
* Gets the address hash (of all components MINUS the zip code)
159+
*
160+
* NOTE: zip codes have the potential to change, and something like 4% of
161+
* ZIP codes change every month, so this hash is likely the better comparison
162+
*
163+
* @param string $algo
164+
*
165+
* @return string
166+
*/
167+
public function getHash(string $algo = 'sha1'): string
168+
{
169+
if ($this->hash) {
170+
return $this->hash;
171+
}
172+
$line1 = $this->getLineOne();
173+
$line2 = $this->getLineTwo(false);
174+
$this->hash = hash($algo, strtolower(
175+
($line1 && $line2) ? $line1 . ', ' . $line2 : $line1
176+
));
177+
return $this->hash;
178+
}
179+
156180
/**
157181
* Is this address is the same as another address?
158182
*
159183
* @param Address $address
184+
* @param bool $fullHash uses the full hash comparison (with zip) vs without zip
160185
*
161186
* @return bool
162187
*/
163-
public function is(Address $address): bool
188+
public function is(Address $address, bool $fullHash = false): bool
164189
{
165-
return $this->getFullHash() === $address->getFullHash();
190+
if ($fullHash) {
191+
return $this->getFullHash() === $address->getFullHash();
192+
}
193+
return $this->getHash() === $address->getHash();
166194
}
167195

168196
/**
@@ -201,14 +229,18 @@ private function getLineOne(): string
201229
/**
202230
* Gets the second line of a formatted address
203231
*
232+
* @param bool $withZip include/exclude zip code
233+
*
204234
* @return string
205235
*/
206-
private function getLineTwo(): string
236+
private function getLineTwo(bool $withZip = true): string
207237
{
208238
$line = (string) $this->city;
209239
$line .= $this->state ? ", " . $this->state : "";
210-
$line .= $this->postalCode ? " " . $this->postalCode : "";
211-
$line .= $this->postalCodeExt ? "-" . $this->postalCodeExt : "";
240+
if ($withZip) {
241+
$line .= $this->postalCode ? " " . $this->postalCode : "";
242+
$line .= $this->postalCodeExt ? "-" . $this->postalCodeExt : "";
243+
}
212244
return $line;
213245
}
214246

tests/AddressTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,24 @@ public function testHashesAddress()
1313
{
1414
$normalizer = new Normalizer();
1515
$address = $normalizer->parse("1234 Main St. NE, Minneapolis, MN 55401");
16+
$sameAddressDiffZip = $normalizer->parse("1234 Main St. NE, Minneapolis, MN 55402");
1617
$sameAddress = $normalizer->parse("1234 Main Street Northeast, Minneapolis, Minnesota 55401");
1718
$differentNumberAddress = $normalizer->parse("5678 Main Street Northeast, Minneapolis, Minnesota 55401");
1819

20+
$this->assertEquals('9bdbf17a475a0129c0546fc210ef46cb914338d0', $address->getHash());
1921
$this->assertEquals('c4ced80b9489911b4a66712470833242597aa032', $address->getFullHash());
2022
$this->assertEquals('f7f77f11493cccb50c5827dff7cb8b26d31f8442', $address->getStreetHash());
2123

24+
$this->assertEquals($address->getHash(), $sameAddressDiffZip->getHash());
25+
$this->assertNotEquals($address->getFullHash(), $sameAddressDiffZip->getFullHash());
26+
27+
$this->assertEquals($address->getHash(), $sameAddress->getHash());
2228
$this->assertEquals($address->getFullHash(), $sameAddress->getFullHash());
2329
$this->assertEquals($address->getStreetHash(), $sameAddress->getStreetHash());
2430
$this->assertTrue($address->is($sameAddress));
2531
$this->assertTrue($sameAddress->is($address));
32+
$this->assertTrue($address->is($sameAddress, true));
33+
$this->assertTrue($sameAddress->is($address, true));
2634

2735
$this->assertEquals($address->getStreetHash(), $differentNumberAddress->getStreetHash());
2836
$this->assertNotEquals($address->getFullHash(), $differentNumberAddress->getFullHash());

0 commit comments

Comments
 (0)