Skip to content

Commit

Permalink
[FEATURE] Added geocoder utility.
Browse files Browse the repository at this point in the history
  • Loading branch information
adgrafik committed Oct 2, 2013
1 parent d471071 commit 9e6ccd1
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
7 changes: 4 additions & 3 deletions Classes/PlugIns/AbstractBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,9 @@ protected function parseOptionSplit($options, $counterPropertyName) {
$configuration = \AdGrafik\GoogleMapsPHP\Utility\OptionSplit::split(array('objectNumber' => $options[$propertyName]), $count);

// Match each option split value with the content of the property value.
foreach ($configuration as $key => &$objectNumber) {
$objectNumber = $objectNumber['objectNumber'];
$i = 0;
foreach ($finalOptions as $key => &$finalOption) {
$objectNumber = $configuration[$i++]['objectNumber'];
if (isset($options[$referenceProperty][$objectNumber])) {
$finalOptions[$key][$referenceProperty] = $options[$referenceProperty][$objectNumber];
}
Expand All @@ -191,7 +192,7 @@ protected function parseOptionSplit($options, $counterPropertyName) {

} else if (array_key_exists($propertyName . 'OptionSplit', $options) === FALSE) {

foreach ($options[$counterPropertyName] as $key => $propertyValue) {
foreach ($options[$counterPropertyName] as $key => &$propertyValue) {
$finalOptions[$key][$propertyName] = $propertyValue;
}

Expand Down
49 changes: 49 additions & 0 deletions Classes/Utility/MapUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
*/
class MapUtility {

const GEOGODE_TYPE_ARRAY = 'array';
const GEOGODE_TYPE_JSON = 'json';
const GEOGODE_TYPE_XML = 'xml';

/**
* isValidDiv
*
Expand All @@ -34,6 +38,51 @@ class MapUtility {
static public function isValidDiv($div) {
return (is_string($div) && !preg_match('/[^a-z0-9_]/i', $div));
}

/**
* geocodeRequest
*
* @param array|string $request
* @param string $type
* @return array
* @throw array
*/
static public function geocodeRequest($request, $type = self::GEOGODE_TYPE_ARRAY) {

$settings = \AdGrafik\GoogleMapsPHP\Utility\ClassUtility::makeInstance('\\AdGrafik\\GoogleMapsPHP\\Configuration\\Settings');

$request = is_array($request)
? $request
: array('address' => $request);

$source = $settings->get('utility.geocoder.source');
$parameters = $settings->get('utility.geocoder.parameters');
$parameters = array_replace_recursive($parameters, $request);

// Convert boolen values to string and remove empty values.
foreach ($parameters as $key => &$value) {
if (is_bool($value)) {
$value = $value ? 'true' : 'false';
} else if ($value === '') {
unset($parameters[$key]);
}
}

$requestType = ($type === self::GEOGODE_TYPE_ARRAY)
? self::GEOGODE_TYPE_JSON
: $type;

$requestUrl = $source . $requestType . '?' . http_build_query($parameters);
$response = file_get_contents($requestUrl);

if ($response === FALSE) {
throw new \AdGrafik\GoogleMapsPHP\Exception(sprintf('URL "%s" could not be fetched. Possible reasons: network problems, allow_url_fopen is off.', $requestUrl), 1334426097);
}

return ($type === self::GEOGODE_TYPE_ARRAY)
? json_decode($response)
: $response;
}
}

?>
11 changes: 11 additions & 0 deletions Configuration/Settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ view:
xmlEncoding: utf-8


#
# Geocoder
#
utility:

geocoder:
source: http://maps.googleapis.com/maps/api/geocode/
parameters:
sensor: false


#
# mapBuilder
#
Expand Down

0 comments on commit 9e6ccd1

Please sign in to comment.