Source for file YahooWeather.php

Documentation is available at YahooWeather.php

  1. <?php
  2. /* SVN FILE: $Id: YahooWeather.php 8 2009-10-12 19:22:01Z Chris $ */
  3. /**
  4.  * Yahoo! Weather data provider
  5.  *
  6.  * @link http://developer.yahoo.com/weather/ Yahoo Weather API documentation
  7.  * @filesource
  8.  * @copyright    Copyright 2009 PBM Web Development - All Rights Reserved
  9.  * @package      weatherForecast
  10.  * @subpackage   weatherForecast.providers
  11.  * @version      $Revision: 8 $
  12.  * @modifiedby   $LastChangedBy: Chris $
  13.  * @lastmodified $Date: 2009-10-12 20:22:01 +0100 (Mon, 12 Oct 2009) $
  14.  * @license      http://www.opensource.org/licenses/bsd-license.php The BSD License
  15.  */
  16.  
  17. include dirname(__FILE__'/../WeatherForecastProvider.php';
  18.  
  19. /**
  20.  * Yahoo! Weather data provider
  21.  *
  22.  * <p>Values in params:<br>
  23.  * dateFormat (string) => Date format for days;
  24.  * Used by {@link http://www.yiiframework.com/doc/api/CDateFormatter#format-detail CDateFormatter::format()},
  25.  * see {@link http://www.unicode.org/reports/tr35/#Date_Format_Patterns Date Format Patterns}
  26.  * for a description of formats. Default: "EEE d'&nbsp;'MMM yyyy"<br>
  27.  * location (string) => Location.<br>
  28.  * To find the location id follow the instructions on the Yahoo Developer Weather RSS page.<br>
  29.  * units (string) => Units to provide the forecast in.
  30.  * "C"|"F" C=metric (default), F=imperial</p>
  31.  *
  32.  * @package    weatherForecast
  33.  * @subpackage weatherForecast.providers
  34.  */
  35. class YahooWeather extends WFXMLProvider {
  36.   /**
  37.    * @var string Driver description
  38.    */
  39.   private $provider "Yahoo! Weather";
  40.   /**
  41.    * @var string Yahoo! Weather feed
  42.    */
  43.   private $feed 'http://weather.yahooapis.com/forecastrss?p=%s&u=%s';
  44.   /**
  45.    * @var string Yahoo! Weather url
  46.    */
  47.   private $url 'http://weather.yahoo.com/';
  48.   /**
  49.    * @var string Default values
  50.    */
  51.   protected $defaults array(
  52.     'location' => 'UKXX0085'//London
  53.     'units' => 'C',
  54.     'dateFormat' => 'EEE d MMM yyyy'
  55.   );
  56.   /**
  57.    * @var array Maps weather codes to symbols
  58.    */
  59.   protected $symbolMap array(
  60.     => 'tornado.png',
  61.     => 'storm.png',
  62.     => 'hurricane.png',
  63.     => 'thunderstorm_severe.png',
  64.     => 'thunderstorm.png',
  65.     => 'sleet.png',
  66.     => 'sleet.png',
  67.     => 'sleet.png',
  68.     => 'drizzle.png',
  69.     => 'drizzle.png',
  70.     10 => 'rain.png',
  71.     11 => 'showers.png',
  72.     12 => 'showers.png',
  73.     13 => 'snow.png',
  74.     14 => 'snow_showers.png',
  75.     15 => 'snow.png',
  76.     16 => 'snow.png',
  77.     17 => 'hail.png',
  78.     18 => 'sleet.png',
  79.     19 => 'dust.png',
  80.     20 => 'fog.png',
  81.     21 => 'haze.png',
  82.     22 => 'smoke.png',
  83.     23 => 'windy.png',
  84.     24 => 'windy.png',
  85.     25 => 'cold.png',
  86.     26 => 'cloudy.png',
  87.     27 => 'partly_cloudy_night.png',
  88.     28 => 'partly_cloudy_day.png',
  89.     29 => 'partly_cloudy_night.png',
  90.     30 => 'partly_cloudy_day.png',
  91.     31 => 'clear_night.png',
  92.     32 => 'sunny.png',
  93.     33 => 'clear_night.png',
  94.     34 => 'partly_cloudy_day.png',
  95.     35 => 'hail.png',
  96.     36 => 'hot.png',
  97.     37 => 'thunderstorm.png',
  98.     38 => 'thunderstorm.png',
  99.     39 => 'thunderstorm.png',
  100.     40 => 'showers.png',
  101.     41 => 'snow_heavy.png',
  102.     42 => 'snow_showers.png',
  103.     43 => 'snow_heavy.png',
  104.     44 => 'partly_cloudy_day.png',
  105.     45 => 'thunder_showers.png',
  106.     46 => 'snow_showers.png',
  107.     47 => 'thunder_showers.png',
  108.   );
  109.  
  110.   /**
  111.    * Fetches the forecast from the provider's datasource
  112.    *
  113.    * Required by IWeatherForecastProvider interface
  114.    *
  115.    * @return WFWeatherForecast Forecast object
  116.    */
  117.   public function getForecast($params$symbols{
  118.     $this->init($params$symbols);
  119.  
  120.     $forecast new WFWeatherForecast();
  121.     $dateFormatter Yii::app()->dateFormatter;
  122.     $this->read(vsprintf($this->feed,  array($this->locationstrtolower($this->units))));
  123.  
  124.     $contextNode $this->xpath->evaluate('/rss/channel')->item(0);
  125.  
  126.     $forecast->provider  CHtml::link($this->provider$this->urlarray('rel'=>'external'));
  127.     $forecast->issued    $this->xpath->evaluate('string(lastBuildDate)'$contextNode);
  128.     $distanceUnits       $this->xpath->evaluate('string(yweather:units/@distance)'$contextNode);
  129.     $humidityUnits       '%';
  130.     $pressureUnits       $this->xpath->evaluate('string(yweather:units/@pressure)'$contextNode);
  131.     $speedUnits          $this->xpath->evaluate('string(yweather:units/@speed)'$contextNode);
  132.     $temperatureUnits    $this->xpath->evaluate('string(yweather:units/@temperature)'$contextNode);
  133.  
  134.     $forecast->location->name $this->xpath->evaluate('string(yweather:location/@city)'$contextNode);
  135.     $forecast->location->lat  $this->xpath->evaluate('string(item/geo:lat)'$contextNode);
  136.     $forecast->location->long $this->xpath->evaluate('string(item/geo:long)'$contextNode);
  137.  
  138.     $day new WFDay();
  139.     $day->date               'Current Conditions';
  140.     $day->sunrise            $this->xpath->evaluate('string(yweather:astronomy/@sunrise)'$contextNode);
  141.     $day->sunset             $this->xpath->evaluate('string(yweather:astronomy/@sunset)'$contextNode);
  142.     $day->description        $this->xpath->evaluate('string(item/yweather:condition/@text)'$contextNode);
  143.     $day->symbol             $this->symbolsDir $this->symbolMap[$this->xpath->evaluate('string(item/yweather:condition/@code)'$contextNode)];
  144.     $day->temperature->value $this->xpath->evaluate('string(item/yweather:condition/@temp)'$contextNode);
  145.     $day->temperature->units $temperatureUnits;
  146.     $day->windSpeed->value   $this->xpath->evaluate('string(yweather:wind/@speed)'$contextNode);
  147.     $day->windSpeed->units    $speedUnits;
  148.     $day->windDirection->value $this->xpath->evaluate('string(yweather:wind/@direction)'$contextNode);
  149.     $day->windDirection->units '&#176;';
  150.     $day->humidity->value    $this->xpath->evaluate('string(yweather:atmosphere/@humidity)'$contextNode);
  151.     $day->humidity->units    $humidityUnits;
  152.     $day->visibility->value  $this->xpath->evaluate('string(yweather:atmosphere/@visibility)'$contextNode)/100;
  153.     $day->visibility->units  $distanceUnits;
  154.     $day->pressure->value    $this->xpath->evaluate('string(yweather:atmosphere/@pressure)'$contextNode);
  155.     $day->pressure->units    $pressureUnits;
  156.     $day->pressureTrend      $this->parsePressureTrend($contextNode);
  157.     $forecast->days->add(0$day);
  158.  
  159.     $items $this->xpath->evaluate('item/yweather:forecast'$contextNode);
  160.     for ($i 0$l $items->length$i $l;{
  161.       $contextNode $items->item($i++);
  162.       $day new WFDay();
  163.       $day->date $dateFormatter->format($this->dateFormat$this->xpath->evaluate('string(@date)'$contextNode));
  164.       $day->description $this->xpath->evaluate('string(@text)'$contextNode);
  165.       $day->symbol  $this->symbolsDir $this->symbolMap[$this->xpath->evaluate('string(@code)'$contextNode)];
  166.       $day->maxTemperature->value $this->xpath->evaluate('string(@high)'$contextNode);
  167.       $day->maxTemperature->units $temperatureUnits;
  168.       $day->minTemperature->value $this->xpath->evaluate('string(@low)'$contextNode);
  169.       $day->minTemperature->units $temperatureUnits;
  170.       $forecast->days->add($i$day);
  171.     }
  172.  
  173.     return $forecast;
  174.   }
  175.  
  176.   /**
  177.    * Parses the pressure trend into  words
  178.    *
  179.    * @param DOMNode $contextNode The node from with to start XPath evaluation
  180.    * @return string The pressure trend
  181.    */
  182.   private function parsePressureTrend($contextNode{
  183.     switch ($this->xpath->evaluate('string(yweather:atmosphere/@rising)'$contextNode)) {
  184.       case 0:
  185.         return 'Steady';
  186.         break;
  187.       case 1:
  188.         return 'Rising';
  189.         break;
  190.       case 2:
  191.         return 'Falling';
  192.         break;
  193.       default:
  194.         return '';
  195.         break;
  196.     // switch
  197.   }
  198. }
  199. ?>

Documentation generated on Tue, 13 Oct 2009 14:09:09 +0100 by phpDocumentor 1.4.2