Services_Weather
[ class tree: Services_Weather ] [ index: Services_Weather ] [ all elements ]

Source for file Weather.php

Documentation is available at Weather.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at                              |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Alexander Wirtz <alex@pc4p.net>                             |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Weather.php,v 1.31 2004/05/04 13:47:41 eru Exp $
  20.  
  21. /**
  22. @package      Services_Weather
  23. @filesource
  24. */
  25.  
  26. /**
  27. */
  28. // {{{ constants
  29. // {{{ cache times
  30. define("SERVICES_WEATHER_EXPIRES_UNITS",      900);
  31. define("SERVICES_WEATHER_EXPIRES_LOCATION",   900);
  32. define("SERVICES_WEATHER_EXPIRES_WEATHER",   1800);
  33. define("SERVICES_WEATHER_EXPIRES_FORECAST",  7200);
  34. define("SERVICES_WEATHER_EXPIRES_LINKS",    43200);
  35. // }}}
  36.  
  37. // {{{ error codes
  38. define("SERVICES_WEATHER_ERROR_SERVICE_NOT_FOUND",   10);
  39. define("SERVICES_WEATHER_ERROR_UNKNOWN_LOCATION",    11);
  40. define("SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA",   12);
  41. define("SERVICES_WEATHER_ERROR_CACHE_INIT_FAILED",   13);
  42. define("SERVICES_WEATHER_ERROR_DB_NOT_CONNECTED",    14);
  43. // }}}
  44.  
  45. // {{{ error codes defined by weather.com
  46. define("SERVICES_WEATHER_ERROR_UNKNOWN_ERROR",            0);
  47. define("SERVICES_WEATHER_ERROR_NO_LOCATION",              1);
  48. define("SERVICES_WEATHER_ERROR_INVALID_LOCATION",         2);
  49. define("SERVICES_WEATHER_ERROR_INVALID_PARTNER_ID",     100);
  50. define("SERVICES_WEATHER_ERROR_INVALID_PRODUCT_CODE",   101);
  51. define("SERVICES_WEATHER_ERROR_INVALID_LICENSE_KEY",    102);
  52. // }}}
  53. // }}}
  54.  
  55. // {{{ class Services_Weather
  56. /**
  57. * PEAR::Services_Weather
  58. *
  59. * This class acts as an interface to various online weather-services.
  60. *
  61. * Services_Weather searches for given locations and retrieves current
  62. * weather data and, dependant on the used service, also forecasts. Up to
  63. * now, SOAP services from CapeScience and EJSE, XML from weather.com and
  64. * METAR/TAF from noaa.gov are supported, further services will get
  65. * included, if they become available and are properly documented.
  66. *
  67. @author       Alexander Wirtz <alex@pc4p.net>
  68. @package      Services_Weather
  69. @license      http://www.php.net/license/2_02.txt
  70. @version      1.3
  71. */
  72.  
  73.     // {{{ &service()
  74.     /**
  75.     * Factory for creating the services-objects
  76.     *
  77.     * Usable keys for the options array are:
  78.     * o debug               enables debugging output
  79.     * --- Common Options
  80.     * o cacheType           defines what type of cache to use
  81.     * o cacheOptions        passes cache options
  82.     * o unitsFormat         use (US)-standard, metric or custom units
  83.     * o customUnitsFormat   defines the customized units format
  84.     * o httpTimeout            sets timeout for HTTP requests
  85.     * o dateFormat          string to use for date output
  86.     * o timeFormat          string to use for time output
  87.     * --- EJSE Options
  88.     * o none
  89.     * --- GlobalWeather Options
  90.     * o none
  91.     * --- METAR/TAF Options
  92.     * o dsn                 String for defining the DB connection
  93.     * o dbOptions           passes DB options
  94.     * o sourceMetar         http, ftp or file - type of data-source for METAR
  95.     * o sourcePathMetar     where to look for the source, URI or filepath,
  96.     *                       of METAR information
  97.     * o sourceTaf           http, ftp or file - type of data-source for TAF
  98.     * o sourcePathTaf       where to look for the source, URI or filepath,
  99.     *                       of TAF information
  100.     * --- weather.com Options
  101.     * o partnerID           You'll receive these keys after registering
  102.     * o licenseKey          with the weather.com XML-service
  103.     *
  104.     * @param    string                      $service 
  105.     * @param    array                       $options 
  106.     * @return   PEAR_Error|object 
  107.     * @throws   PEAR_Error
  108.     * @throws   PEAR_Error::SERVICES_WEATHER_ERROR_SERVICE_NOT_FOUND
  109.     * @access   public
  110.     */
  111.     function &service($service$options = null)
  112.     {
  113.         $service ucfirst(strtolower($service));
  114.         $classname "Services_Weather_".$service;
  115.  
  116.         // Check for debugging-mode and set stuff accordingly
  117.         if (is_array($options&& isset($options["debug"]&& $options["debug">= 2{
  118.             if (!defined("SERVICES_WEATHER_DEBUG")) {
  119.                 define("SERVICES_WEATHER_DEBUG"true);
  120.             }
  121.             include_once("Services/Weather/".$service.".php");
  122.         else {
  123.             if (!defined("SERVICES_WEATHER_DEBUG")) {
  124.                 define("SERVICES_WEATHER_DEBUG"false);
  125.             }
  126.             @include_once("Services/Weather/".$service.".php");
  127.         }
  128.  
  129.         // No such service... bail out
  130.         if (!class_exists($classname)) {
  131.             return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_SERVICE_NOT_FOUND__FILE____LINE__);
  132.         }
  133.  
  134.         // Create service and return
  135.         $error = null;
  136.         @$obj &new $classname($options$error);
  137.  
  138.         if (Services_Weather::isError($error)) {
  139.             return $error;
  140.         else {
  141.             return $obj;
  142.         }
  143.     }
  144.     // }}}
  145.  
  146.     // {{{ apiVersion()
  147.     /**
  148.     * For your convenience, when I come up with changes in the API...
  149.     *
  150.     * @return   string 
  151.     * @access   public
  152.     */
  153.    function apiVersion()
  154.     {
  155.         return "1.3";
  156.     }
  157.     // }}}
  158.  
  159.     // {{{ _errorMessage()
  160.     /**
  161.     * Returns the message for a certain error code
  162.     *
  163.     * @param    PEAR_Error|int             $value 
  164.     * @return   string 
  165.     * @access   private
  166.     */
  167.     function _errorMessage($value)
  168.     {
  169.         static $errorMessages;
  170.         if (!isset($errorMessages)) {
  171.             $errorMessages = array(
  172.                 SERVICES_WEATHER_ERROR_SERVICE_NOT_FOUND         => "Requested service could not be found.",
  173.                 SERVICES_WEATHER_ERROR_UNKNOWN_LOCATION          => "Unknown location provided.",
  174.                 SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA         => "Server data wrong or not available.",
  175.                 SERVICES_WEATHER_ERROR_CACHE_INIT_FAILED         => "Cache init was not completed.",
  176.                 SERVICES_WEATHER_ERROR_DB_NOT_CONNECTED          => "MetarDB is not connected.",
  177.                 SERVICES_WEATHER_ERROR_UNKNOWN_ERROR             => "An unknown error has occured.",
  178.                 SERVICES_WEATHER_ERROR_NO_LOCATION               => "No location provided.",
  179.                 SERVICES_WEATHER_ERROR_INVALID_LOCATION          => "Invalid location provided.",
  180.                 SERVICES_WEATHER_ERROR_INVALID_PARTNER_ID        => "Invalid partner id.",
  181.                 SERVICES_WEATHER_ERROR_INVALID_PRODUCT_CODE      => "Invalid product code.",
  182.                 SERVICES_WEATHER_ERROR_INVALID_LICENSE_KEY       => "Invalid license key."
  183.             );
  184.         }
  185.  
  186.         if (Services_Weather::isError($value)) {
  187.             $value $value->getCode();
  188.         }
  189.  
  190.         return isset($errorMessages[$value]$errorMessages[$value$errorMessages[SERVICES_WEATHER_ERROR_UNKNOWN_ERROR];
  191.     }
  192.     // }}}
  193.  
  194.     // {{{ isError()
  195.     /**
  196.     * Checks for an error object, same as in PEAR
  197.     *
  198.     * @param    PEAR_Error|mixed           $value 
  199.     * @return   bool 
  200.     * @access   public
  201.     */
  202.     function isError($value)
  203.     {
  204.         return (is_object($value&& (strtolower(get_class($value)) == "pear_error" || is_subclass_of($value"pear_error")));
  205.     }
  206.     // }}}
  207.  
  208.     // {{{ &raiseError()
  209.     /**
  210.     * Creates error, same as in PEAR with a customized flavor
  211.     *
  212.     * @param    int                         $code 
  213.     * @param    string                      $file 
  214.     * @param    int                         $line 
  215.     * @return   PEAR_Error 
  216.     * @access   private
  217.     */
  218.     function &raiseError($code = SERVICES_WEATHER_ERROR_UNKNOWN_ERROR$file ""$line = 0)
  219.     {
  220.         // This should improve the performance of the script, as PEAR is only included, when
  221.         // really needed.
  222.         include_once "PEAR.php";
  223.  
  224.         $message "Services_Weather";
  225.         if ($file != "" && $line > 0{
  226.             $message .= " (".basename($file).":".$line.")";
  227.         }
  228.         $message .= ": ".Services_Weather::_errorMessage($code);
  229.  
  230.         return PEAR::raiseError($message$codePEAR_ERROR_RETURNE_USER_NOTICE"Services_Weather_Error"nullfalse);
  231.     }
  232.     // }}}
  233. }
  234. // }}}
  235. ?>

Documentation generated on Mon, 11 Mar 2019 13:56:24 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.