| 1 | <?php |
||
| 5 | class Geocoder |
||
| 6 | { |
||
| 7 | const API_URL = 'http://maps.google.com/maps/api/geocode'; |
||
| 8 | const VALID_OUTPUT_FORMAT = ['json', 'xml']; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * @param $address |
||
| 12 | * @param string $region |
||
| 13 | * @param string $outputFormat |
||
| 14 | * |
||
| 15 | * @throws \Exception |
||
| 16 | * |
||
| 17 | * @return string |
||
| 18 | */ |
||
| 19 | 8 | public function geocode($address, $region = '', $outputFormat = 'json') |
|
| 20 | { |
||
| 21 | 8 | if ($this->validateOutputFormat($outputFormat) !== true) { |
|
| 22 | 1 | throw new \Exception("'{$outputFormat}' is not a valid format"); |
|
| 23 | } |
||
| 24 | |||
| 25 | 7 | return file_get_contents($this->generateRequestUrl($address, $region, $outputFormat)); |
|
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @param $address |
||
| 30 | * |
||
| 31 | * @return array |
||
| 32 | */ |
||
| 33 | 3 | public function getLatLng($address) |
|
| 52 | |||
| 53 | /** |
||
| 54 | * @param $format |
||
| 55 | * |
||
| 56 | * @return bool |
||
| 57 | */ |
||
| 58 | 8 | private function validateOutputFormat($format) |
|
| 66 | |||
| 67 | /** |
||
| 68 | * @param $address |
||
| 69 | * @param string $region |
||
| 70 | * @param string $outputFormat |
||
| 71 | * @param bool $sensor |
||
| 72 | * |
||
| 73 | * @return string |
||
| 74 | */ |
||
| 75 | 7 | private function generateRequestUrl($address, $region = '', $outputFormat = 'json', $sensor = false) |
|
| 85 | } |
||
| 86 |