Source for file WeatherForecastProvider.php

Documentation is available at WeatherForecastProvider.php

  1. <?php
  2. /* SVN FILE: $Id: WeatherForecastProvider.php 14 2009-10-13 13:43:06Z Chris $ */
  3. /**
  4.  * Weather Forecast Interface and Weather Forecast Objects
  5.  *
  6.  * @filesource
  7.  * @copyright    Copyright 2009 PBM Web Development - All Rights Reserved
  8.  * @package      weatherForecast
  9.  * @subpackage   weatherForecast.providers
  10.  * @version      $Revision: 14 $
  11.  * @lastmodified $Date: 2009-10-13 14:43:06 +0100 (Tue, 13 Oct 2009) $
  12.  * @license      http://www.opensource.org/licenses/bsd-license.php The BSD License
  13.  */
  14.  
  15. /**
  16.  * Weather Forecast Provider interface
  17.  *
  18.  * @package    weatherForecast
  19.  * @subpackage weatherForecast.providers
  20.  */
  21. interface IWeatherForecastProvider {
  22.   public function getForecast($params$symbols);
  23.  
  24.  
  25. /**
  26.  * Base class for Weather Forecast providers
  27.  *
  28.  * @package    weatherForecast
  29.  * @subpackage weatherForecast.providers
  30.  */
  31. abstract class WFBaseProvider implements IWeatherForecastProvider {
  32.   /**
  33.    * @var string Date format
  34.    * @link http://www.yiiframework.com/doc/api/CDateFormatter
  35.   */
  36.   protected $dateFormat;
  37.   /**
  38.    * @var string Forecast location
  39.    */
  40.   protected $location;
  41.   /**
  42.    * @var string Path to symbol set to use
  43.    */
  44.   protected $symbolsDir;
  45.   /**
  46.    * @var array Units for display. C=metric, F=imperial
  47.    */
  48.   protected $units;
  49.  
  50.   protected function init($params$symbols{
  51.     $params array_merge($this->defaults$params);
  52.  
  53.     foreach ($params as $param => $value{
  54.       $this->$param $value;
  55.     // foreach
  56.  
  57.     $this->symbolsDir $symbols[0];
  58.     if (!empty($symbols[1])) $this->symbolMap $symbols[1];
  59.   }
  60.  
  61.   /**
  62.    * Converts degrees Celcius degrees Farenheight to
  63.    *
  64.    * @param float $value The value to convert
  65.    * @param mixed $precision Precision of the result
  66.    * @return float The converted value
  67.    */
  68.   protected function c2f($value$precision=0{
  69.     debugbreak();
  70.     return round(((9/5)*$value)+32$precision);
  71.   }
  72.  
  73.   /**
  74.    * Converts degrees Farenheight to degrees Celcius
  75.    *
  76.    * @param float $value The value to convert
  77.    * @param mixed $precision Precision of the result
  78.    * @return float The converted value
  79.    */
  80.   protected function f2c($value$precision=0{
  81.     return round((5/9)*($value-32)$precision);
  82.   }
  83.  
  84.   /**
  85.    * Converts mph to kph
  86.    *
  87.    * @param float $value The value to convert
  88.    * @param mixed $precision Precision of the result
  89.    * @return float The converted value
  90.    */
  91.   protected function mph2kph($value$precision=0{
  92.     return round($value 1.609344$precision);
  93.   }
  94. }
  95.  
  96. /**
  97.  * Base class for Weather Forecast providers using XML feeds
  98.  *
  99.  * Reads XML data and provides an DOMXPath object for querying it
  100.  *
  101.  * @package    weatherForecast
  102.  * @subpackage weatherForecast.providers
  103.  */
  104. abstract class WFXMLProvider extends WFBaseProvider {
  105.   /**
  106.    * @var string DOMXPath Object
  107.    */
  108.   protected $xpath null;
  109.   /**
  110.    * @var string XML response
  111.    */
  112.   private $xml;
  113.  
  114.   public function __get($name{
  115.     $getter='get'.$name;
  116.     if (method_exists($this,$getter)) {
  117.       return $this->$getter();
  118.     }
  119.     else {
  120.       throw new CException(Yii::t('WeatherForecast','Property "{class}.{property}" is not defined.',
  121.         array('{class}'=>get_class($this)'{property}'=>$name)));
  122.     }
  123.   }
  124.  
  125.   /**
  126.    * Reads the URL and puts the content into $this->xml
  127.    *
  128.    * @param string $url
  129.    * @throws CException if unable to read the URL
  130.    */
  131.   protected function read($url{
  132.     unset($this->xpath)// ensures the feed is recognised
  133.  
  134.     $ch curl_init();
  135.     curl_setopt($chCURLOPT_RETURNTRANSFER1);
  136.     curl_setopt($chCURLOPT_URL$url);
  137.     curl_setopt($chCURLOPT_USERAGENT$_SERVER['HTTP_USER_AGENT']);
  138.     curl_setopt($chCURLOPT_TIMEOUT60);
  139.     curl_setopt($chCURLOPT_FOLLOWLOCATIONtrue);
  140.     $this->xml curl_exec($ch);
  141.     curl_close($ch);
  142.     //$this->xml = file_get_contents($url); // Use if PHP not compiled with cURL
  143.  
  144.     if (empty($this->xml)) {
  145.       throw new CException(Yii::t('WeatherForecast','Unable to read url {url}.',
  146.         array('{url}'=>$url)));
  147.     }
  148.   }
  149.  
  150.   /**
  151.    * Creates a new XPath object from the XML
  152.    */
  153.   private function getXPath({
  154.     $doc new DOMDocument;
  155.     $doc->preserveWhiteSpace false;
  156.     $doc->loadXML(utf8_encode($this->xml));
  157.     $this->xpath new DOMXPath($doc);
  158.     return $this->xpath;
  159.   }
  160. }
  161.  
  162. /**
  163.  * Weather Forecast class
  164.  *
  165.  * @package    weatherForecast
  166.  * @subpackage weatherForecast.providers
  167.  */
  168. class WFWeatherForecast extends WFBase {
  169.   /**
  170.    * @var string Name of the data provider
  171.    */
  172.   protected $provider;
  173.   /**
  174.    * @var string Date/time the forecast was issued
  175.    */
  176.   protected $issued;
  177.   /**
  178.    * @var WFWGS84Location Forecast location
  179.    */
  180.   protected $location;
  181.   /**
  182.    * @var CMap Days of the forecast
  183.    */
  184.   protected $days;
  185.  
  186.   /**
  187.    * Weather forecast contructor
  188.    */
  189.   public function __construct({
  190.     $this->location new WFWGS84Location();
  191.     $this->days new CMap();
  192.   }
  193. }
  194.  
  195. /**
  196.  * Weather Forecast Day class
  197.  *
  198.  * Records the weather forecast for a given day
  199.  *
  200.  * @package    weatherForecast
  201.  * @subpackage weatherForecast.providers
  202.  */
  203. class WFDay extends WFBase {
  204.   /**
  205.    * @var string Date for this day
  206.    */
  207.   protected $date;
  208.   /**
  209.    * @var string Forecast description
  210.    */
  211.   protected $description;
  212.   /**
  213.    * @var WFValue Humidity value
  214.    */
  215.   protected $humidity;
  216.   /**
  217.    * @var WFValue Maximum temperature value
  218.    */
  219.   protected $maxTemperature;
  220.   /**
  221.    * @var WFValue Minimum temperature value
  222.    */
  223.   protected $minTemperature;
  224.   /**
  225.    * @var WFValue Atmospheric pressure value
  226.    */
  227.   protected $pressure;
  228.   /**
  229.    * @var string Pressure trend
  230.    */
  231.   protected $pressureTrend;
  232.   /**
  233.    * @var string Time of sunrise
  234.    */
  235.   protected $sunrise;
  236.   /**
  237.    * @var string Time of sunset
  238.    */
  239.   protected $sunset;
  240.   /**
  241.    * @var string Path to the symbol representing the forecast
  242.    */
  243.   protected $symbol;
  244.   /**
  245.    * @var WFValue Current temperature value
  246.    */
  247.   protected $temperature;
  248.   /**
  249.    * @var WFValue Visibility value
  250.    */
  251.   protected $visibility;
  252.   /**
  253.    * @var WFValue Wind direction
  254.    */
  255.   protected $windDirection;
  256.   /**
  257.    * @var WFValue Wind speed value
  258.    */
  259.   protected $windSpeed;
  260.  
  261.   /**
  262.    * Weather Forecast Day contructor
  263.    */
  264.   public function __construct({
  265.     $this->humidity       new WFValue();
  266.     $this->pressure       new WFValue();
  267.     $this->temperature    new WFValue();
  268.     $this->maxTemperature new WFValue();
  269.     $this->minTemperature new WFValue();
  270.     $this->visibility     new WFValue();
  271.     $this->windDirection  new WFValue();
  272.     $this->windSpeed      new WFValue();
  273.   }
  274. }
  275.  
  276. /**
  277.  * Weather Forecast Value class
  278.  *
  279.  * Holds a weather forecast value
  280.  *
  281.  * @package    weatherForecast
  282.  * @subpackage weatherForecast.providers
  283.  */
  284. class WFValue extends WFBase {
  285.   /**
  286.    * @var mixed Value
  287.    */
  288.   protected $value;
  289.   /**
  290.    * @var string Units of the value
  291.    */
  292.   protected $units;
  293. }
  294.  
  295. /**
  296.  * Weather Forecast Location class
  297.  *
  298.  * Holds a location of a weather forecast
  299.  *
  300.  * @package    weatherForecast
  301.  * @subpackage weatherForecast.providers
  302.  */
  303. class WFWGS84Location extends WFBase {
  304.   /**
  305.    * @var string Name of the location
  306.    */
  307.   protected $name;
  308.   /**
  309.    * @var float Latitude of the location
  310.    */
  311.   protected $lat;
  312.   /**
  313.    * @var float Longitude of the location
  314.    */
  315.   protected $long;
  316.   /**
  317.    * @var WFValue Altitude of the location
  318.    */
  319.   protected $alt;
  320.  
  321.   /**
  322.    * Weather Forecast Location contructor
  323.    */
  324.   public function __construct({
  325.     $this->alt new WFValue();
  326.   }
  327. }
  328.  
  329. /**
  330.  * Weather Forecast base class
  331.  *
  332.  * Provides the magic functions
  333.  *
  334.  * @package    weatherForecast
  335.  * @subpackage weatherForecast.providers
  336.  */
  337. abstract class WFBase {
  338.   public function __get($name{
  339.     if(property_exists($this,$name)) return $this->$name;
  340.   }
  341.  
  342.   public function __set($name$value{
  343.     if(property_exists($this,$name)) {
  344.       $this->$name=$value;
  345.       return true;
  346.     }
  347.     else {
  348.       return false;
  349.     }
  350.   }
  351.  
  352.   public function __isset($name{
  353.     if(property_exists($this,$name&& isset($this->$name)) return true;
  354.     else return false;
  355.   }
  356.  
  357.   public function __unset($name{
  358.     if(property_exists($this,$name)) unset($this->$name);
  359.   }
  360. }
  361. ?>

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