LocationHelper
in package
Table of Contents
Constants
- LOOKUP_ERROR : string = 'error'
- Network error, timeout, quota, rejected key or unreadable response; never cached.
- LOOKUP_NOT_FOUND : string = 'not_found'
- Google answered but does not know the address (`ZERO_RESULTS`); safe to cache.
- LOOKUP_OK : string = 'ok'
- Google found a point.
- CONNECT_TIMEOUT_SECONDS : int = 3
- IMPORTANT: geocoding runs synchronously in `AdvertManager::beforeSave()`, so these fixed limits bound the request. Without them a slow or unavailable Google would hold the whole advert save. On failure the lookup returns `null` and the caller keeps the original coordinates.
- MAX_RESPONSE_BYTES : int = 256 * 1024
- TOTAL_TIMEOUT_SECONDS : int = 5
Methods
- lookupAddress() : array{status: string, latitude: string, longitude: string, formattedAddress: string}
- Geocodes a free-text query (address, city, postcode) through the Google Maps Geocoding API.
- fetch() : string|null
- Performs a bounded GET: connect and total timeout, no redirects, capped body size.
- getXml() : SimpleXMLElement|null
Constants
LOOKUP_ERROR
Network error, timeout, quota, rejected key or unreadable response; never cached.
public
string
LOOKUP_ERROR
= 'error'
LOOKUP_NOT_FOUND
Google answered but does not know the address (`ZERO_RESULTS`); safe to cache.
public
string
LOOKUP_NOT_FOUND
= 'not_found'
LOOKUP_OK
Google found a point.
public
string
LOOKUP_OK
= 'ok'
CONNECT_TIMEOUT_SECONDS
IMPORTANT: geocoding runs synchronously in `AdvertManager::beforeSave()`, so these fixed limits bound the request. Without them a slow or unavailable Google would hold the whole advert save. On failure the lookup returns `null` and the caller keeps the original coordinates.
private
int
CONNECT_TIMEOUT_SECONDS
= 3
MAX_RESPONSE_BYTES
private
int
MAX_RESPONSE_BYTES
= 256 * 1024
TOTAL_TIMEOUT_SECONDS
private
int
TOTAL_TIMEOUT_SECONDS
= 5
Methods
lookupAddress()
Geocodes a free-text query (address, city, postcode) through the Google Maps Geocoding API.
public
static lookupAddress(string $address, string $key) : array{status: string, latitude: string, longitude: string, formattedAddress: string}
Distinguishes an unknown address (not_found, cacheable) from an unavailable Google
(error, not cacheable); also returns formatted_address.
Parameters
- $address : string
- $key : string
Return values
array{status: string, latitude: string, longitude: string, formattedAddress: string}fetch()
Performs a bounded GET: connect and total timeout, no redirects, capped body size.
private
static fetch(string $url, int $connectTimeout, int $totalTimeout, int $maxBytes[, bool $httpsOnly = true ]) : string|null
Returns null on failure (network, timeout, HTTP other than 200, oversized response).
A failure is never logged with the URL because it carries the API key.
Parameters
- $url : string
- $connectTimeout : int
- $totalTimeout : int
- $maxBytes : int
- $httpsOnly : bool = true
-
always
truein production;falseonly for tests against a local server
Return values
string|nullgetXml()
private
static getXml(string $url) : SimpleXMLElement|null
Parameters
- $url : string