@@ -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
0 commit comments