Created
April 20, 2012 21:20
-
-
Save kemo/2431972 to your computer and use it in GitHub Desktop.
Google Weather helper class for Kohana (fixed days and a bug with caching)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php defined('SYSPATH') or die('No direct script access.'); | |
/** | |
* Google (unofficial) Weather API Helper | |
* | |
* @author Kemal Delalic <[email protected]> | |
*/ | |
class Weather { | |
protected static $_days = array( | |
'Mon' => 'Monday', | |
'Tue' => 'Tuesday', | |
'Wed' => 'Wednesday', | |
'Thu' => 'Thursday', | |
'Fri' => 'Friday', | |
'Sat' => 'Saturday', | |
'Sun' => 'Sunday', | |
); | |
/** | |
* @var string Base Google URL | |
*/ | |
protected static $_base_url = 'http://www.google.com'; | |
/** | |
* Gets the cached weather info, if any | |
* | |
* @param string $city name | |
* @param int $duration (optional) | |
* @return array | |
*/ | |
public static function cached($city, $duration = NULL) | |
{ | |
$cache = Cache::instance(); | |
$cache_key = Weather::cache_key($city); | |
$duration = ($duration === NULL) ? Date::HOUR : $duration; | |
if ( ! $weather = $cache->get($cache_key)) | |
{ | |
if ($weather = Weather::get($city)) | |
{ | |
$cache->set($cache_key, $weather, $duration); | |
} | |
} | |
return $weather; | |
} | |
/** | |
* Retrieves the per-city cache key | |
* | |
* @param string $city | |
* @return string | |
*/ | |
public static function cache_key($city) | |
{ | |
return "weather::{$city}"; | |
} | |
/** | |
* Gets the weather info for the passed city in a consistent format | |
* | |
* @param string $city name | |
* @return array data | |
*/ | |
public static function get($city) | |
{ | |
$query = array( | |
'hl' => 'en', | |
'version' => 1, | |
'weather' => $city, | |
); | |
$url = Weather::_url('/ig/api?'.http_build_query($query, '', '&')); | |
try | |
{ | |
$response = Request::factory($url) | |
->execute(); | |
$xml = simplexml_load_string($response->body()); | |
} | |
catch (Exception $e) | |
{ | |
return FALSE; | |
} | |
$array = Weather::_to_array($xml->weather); | |
return Weather::_format_response($array); | |
} | |
public static function day($short) | |
{ | |
return Arr::get(Weather::$_days, $short, $short); | |
} | |
public static function to_celsius($fahrenheit) | |
{ | |
return round((5 / 9) * ($fahrenheit - 32)); | |
} | |
public static function to_fahrenheit($celsius) | |
{ | |
return round((9 / 5) * $celsius + 32); | |
} | |
protected static function _format_response(array $response) | |
{ | |
if ( ! isset($response['current_conditions'], $response['forecast_conditions'])) | |
throw new Kohana_Exception('Invalid response array.'); | |
$response['current_conditions']['icon'] = Weather::_url($response['current_conditions']['icon']); | |
$forecasts = & $response['forecast_conditions']; | |
foreach ($forecasts as $key => $condition) | |
{ | |
$forecasts[$key]['day_of_week'] = Weather::day($condition['day_of_week']); | |
$forecasts[$key]['low'] = Weather::to_celsius($condition['low']); | |
$forecasts[$key]['high'] = Weather::to_celsius($condition['high']); | |
$forecasts[$key]['icon'] = Weather::_url($condition['icon']); | |
} | |
return $response; | |
} | |
/** | |
* Converts the XML response into a human-readable array | |
* | |
* @param mixed $xml | |
* @return array | |
*/ | |
protected static function _to_array($xml) | |
{ | |
$array = (array) $xml; | |
$result = array(); | |
foreach ($array as $key => $value) | |
{ | |
if ($key === '@attributes') | |
{ | |
$result = Arr::get($value, 'data', Arr::flatten($value)); | |
} | |
else | |
{ | |
$result[$key] = Arr::is_array($value) ? Weather::_to_array($value) : $value; | |
} | |
} | |
return $result; | |
} | |
protected static function _url($append) | |
{ | |
return Weather::$_base_url.$append; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment