Skip to content

Commit 28bb553

Browse files
author
Zack Teska
committed
added factory method helpers for simple address
1 parent c04775f commit 28bb553

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ $address = new SimpleAddress('1234 Main St NE', null, 'Minneapolis', 'MN', '5540
118118
$address->getHash(); // full hash minus zip
119119
$address->getFullHash(); // full hash including zip
120120

121+
// or do it all with the factory method:
122+
SimpleAddress::hashFromParts('1234 Main St NE', null, 'Minneapolis', 'MN', '55401');
123+
121124
// CANNOT hash street, since the component parts don't exist
122125
$address->getStreetHash(); // throws exception
123126
```

src/SimpleAddress.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,38 @@ public function getStreetHash(string $algo = 'sha1'): string
4545
{
4646
throw new AddressNotNormalizedException('Address is not normalized and cannot hash the street.');
4747
}
48+
49+
/**
50+
* Helper to get an address hash from 5-part address
51+
*
52+
* @param string $address1
53+
* @param string|null $address2
54+
* @param string $city
55+
* @param string $state
56+
* @param string|null $postalCode
57+
*
58+
* @return string
59+
*/
60+
public static function hashFromParts(string $address1, ?string $address2, string $city, string $state, ?string $postalCode = null): string
61+
{
62+
$address = new self($address1, $address2, $city, $state, $postalCode);
63+
return $address->getHash();
64+
}
65+
66+
/**
67+
* Helper to get a full address hash from 5-part address
68+
*
69+
* @param string $address1
70+
* @param string|null $address2
71+
* @param string $city
72+
* @param string $state
73+
* @param string|null $postalCode
74+
*
75+
* @return string
76+
*/
77+
public static function fullHashFromParts(string $address1, ?string $address2, string $city, string $state, ?string $postalCode = null): string
78+
{
79+
$address = new self($address1, $address2, $city, $state, $postalCode);
80+
return $address->getFullHash();
81+
}
4882
}

tests/SimpleAddressTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ public function testHashesAddress()
1616
$address = new SimpleAddress('1234 Main St NE', null, 'Minneapolis', 'MN', '55401');
1717

1818
$this->assertEquals('9bdbf17a475a0129c0546fc210ef46cb914338d0', $address->getHash());
19+
$this->assertEquals($address->getHash(), SimpleAddress::hashFromParts('1234 Main St NE', null, 'Minneapolis', 'MN', '55401'));
20+
1921
$this->assertEquals('c4ced80b9489911b4a66712470833242597aa032', $address->getFullHash());
22+
$this->assertEquals($address->getFullHash(), SimpleAddress::fullHashFromParts('1234 Main St NE', null, 'Minneapolis', 'MN', '55401'));
2023

2124
$this->expectException(AddressNotNormalizedException::class);
2225
$address->getStreetHash();

0 commit comments

Comments
 (0)