mirror of
https://github.com/beestat/app.git
synced 2025-07-09 03:04:07 -04:00
Fixed #282 - Pre-screen for bad addresses
This commit is contained in:
parent
ddb43c773c
commit
f909470b5d
@ -19,7 +19,7 @@ class address extends cora\crud {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Search for an address based on an address string. This will make an API
|
* Search for an address based on an address string. This will make an API
|
||||||
* call to Smarty Streets using that address string (after first checking
|
* call to SmartyStreets using that address string (after first checking
|
||||||
* the cache to see if we've done it before), then it will either create the
|
* the cache to see if we've done it before), then it will either create the
|
||||||
* address row for this user or return the existing one if it already
|
* address row for this user or return the existing one if it already
|
||||||
* exists.
|
* exists.
|
||||||
@ -30,12 +30,39 @@ class address extends cora\crud {
|
|||||||
* 2. 123 Sesame Street (query smarty, return existing row)
|
* 2. 123 Sesame Street (query smarty, return existing row)
|
||||||
* 3. 123 Sesame Street (query smarty (cached), return existing row)
|
* 3. 123 Sesame Street (query smarty (cached), return existing row)
|
||||||
*
|
*
|
||||||
* @param string $address_string Freeform address string
|
* @param string $address Address components (line_1, locality,
|
||||||
|
* administrative_area, postal_code)
|
||||||
* @param string $country ISO 3 country code
|
* @param string $country ISO 3 country code
|
||||||
*
|
*
|
||||||
* @return array The address row.
|
* @return array The address row.
|
||||||
*/
|
*/
|
||||||
public function search($address_string, $country) {
|
public function search($address, $country) {
|
||||||
|
/**
|
||||||
|
* If any of these fields are missing, set normalized to an empty array
|
||||||
|
* and skip the SmartyStreets lookup. Also, line_1 must have a number and
|
||||||
|
* text.
|
||||||
|
*/
|
||||||
|
foreach(['line_1', 'locality', 'administrative_area', 'postal_code'] as $key) {
|
||||||
|
if(
|
||||||
|
isset($address[$key]) === false ||
|
||||||
|
trim($address[$key]) === '' ||
|
||||||
|
$address[$key] === null
|
||||||
|
) {
|
||||||
|
$normalized = [];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(
|
||||||
|
isset($address['line_1']) === true &&
|
||||||
|
preg_match('/\d+ [0-9]*[a-z]+/i', trim(preg_replace('/[^a-z0-9 ]/i', ' ', $address['line_1']))) !== 1
|
||||||
|
) {
|
||||||
|
$normalized = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
// If normalized wasn't overrridden, check with SmartyStreets.
|
||||||
|
if(isset($normalized) === false) {
|
||||||
|
$address_string = $address['line_1'] . ', ' . $address['locality'] . ', ' . $address['administrative_area'] . ', ' . $address['postal_code'];
|
||||||
$normalized = $this->api(
|
$normalized = $this->api(
|
||||||
'smarty_streets',
|
'smarty_streets',
|
||||||
'smarty_streets_api',
|
'smarty_streets_api',
|
||||||
@ -44,6 +71,7 @@ class address extends cora\crud {
|
|||||||
'country' => $country
|
'country' => $country
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
$key = $this->generate_key($normalized);
|
$key = $this->generate_key($normalized);
|
||||||
$existing_address = $this->get([
|
$existing_address = $this->get([
|
||||||
|
@ -299,19 +299,19 @@ class ecobee_thermostat extends cora\crud {
|
|||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
private function get_address($thermostat, $ecobee_thermostat) {
|
private function get_address($thermostat, $ecobee_thermostat) {
|
||||||
$address_parts = [];
|
$address = [];
|
||||||
|
|
||||||
if(isset($ecobee_thermostat['location']['streetAddress']) === true) {
|
if(isset($ecobee_thermostat['location']['streetAddress']) === true) {
|
||||||
$address_parts[] = $ecobee_thermostat['location']['streetAddress'];
|
$address['line_1'] = $ecobee_thermostat['location']['streetAddress'];
|
||||||
}
|
}
|
||||||
if(isset($ecobee_thermostat['location']['city']) === true) {
|
if(isset($ecobee_thermostat['location']['city']) === true) {
|
||||||
$address_parts[] = $ecobee_thermostat['location']['city'];
|
$address['locality'] = $ecobee_thermostat['location']['city'];
|
||||||
}
|
}
|
||||||
if(isset($ecobee_thermostat['location']['provinceState']) === true) {
|
if(isset($ecobee_thermostat['location']['provinceState']) === true) {
|
||||||
$address_parts[] = $ecobee_thermostat['location']['provinceState'];
|
$address['administrative_area'] = $ecobee_thermostat['location']['provinceState'];
|
||||||
}
|
}
|
||||||
if(isset($ecobee_thermostat['location']['postalCode']) === true) {
|
if(isset($ecobee_thermostat['location']['postalCode']) === true) {
|
||||||
$address_parts[] = $ecobee_thermostat['location']['postalCode'];
|
$address['postal_code'] = $ecobee_thermostat['location']['postalCode'];
|
||||||
}
|
}
|
||||||
|
|
||||||
if(
|
if(
|
||||||
@ -334,7 +334,7 @@ class ecobee_thermostat extends cora\crud {
|
|||||||
'address',
|
'address',
|
||||||
'search',
|
'search',
|
||||||
[
|
[
|
||||||
'address_string' => implode(', ', $address_parts),
|
'address' => $address,
|
||||||
'country' => $country
|
'country' => $country
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user