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

Documentation generated on Mon, 11 Mar 2019 10:14:19 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.