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

Source for file Holidays.php

Documentation is available at Holidays.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2002 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 at 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:   Carsten Lucke <luckec@tool-garage.de>                     |
  17. // +----------------------------------------------------------------------+
  18. //
  19. //    $Id: Holidays.php,v 1.3 2005/02/13 21:51:46 luckec Exp $
  20.  
  21. /**
  22.  * uses PEAR errors
  23.  */
  24. require_once 'PEAR.php';
  25.  
  26. /**
  27.  * uses PEAR::Date
  28.  */
  29. require_once 'Date.php';
  30.  
  31. /**
  32.  * Class that wraps a holiday's data
  33.  */
  34. require_once 'Date/Holidays/Holiday.php';
  35.  
  36. /**
  37.  * Driver baseclass
  38.  */
  39. require_once 'Date/Holidays/Driver.php';
  40.     
  41. /**
  42.  * could not find file of driver-class
  43.  *
  44.  * @access  public
  45.  */
  46. define('DATE_HOLIDAYS_ERROR_DRIVERFILE_NOT_FOUND'1);
  47.  
  48. /**
  49.  * class that helps you to locate holidays for a year
  50.  *
  51.  * @abstract
  52.  * @category Date
  53.  * @package  Date_Holidays
  54.  * @version  $Id: Holidays.php,v 1.3 2005/02/13 21:51:46 luckec Exp $
  55.  * @author   Carsten Lucke <luckec@tool-garage.de>
  56.  * @author   Stephan Schmidt <schst@php.net>
  57.  */
  58. class Date_Holidays 
  59. {
  60.   /**
  61.     * Constructor
  62.     *
  63.     * Use the Date_Holidays::factory() method to construct an object of a certain driver
  64.     *
  65.     * @access   protected
  66.     */
  67.     function Date_Holidays()
  68.     {
  69.     }
  70.     
  71.    /**
  72.     * Factory method that creates a driver-object
  73.     *
  74.     * @static
  75.     * @access   public
  76.     * @param    string  $driverId   driver-name
  77.     * @return   object  Date_Holidays driver-object on success, otherwise a PEAR_Error object
  78.     * @throws   object PEAR_Error
  79.     */
  80.     function factory($driverId$year = null$locale = null)
  81.     {
  82.         if (isset($GLOBALS['_DATE_HOLIDAYS']['DIE_ON_MISSING_LOCALE'])) {
  83.             Date_Holidays::staticSetProperty('DIE_ON_MISSING_LOCALE'true);
  84.         }
  85.         
  86.         $driverClass        =   'Date_Holidays_Driver_' $driverId;
  87.         if (class_exists($driverClass)) {
  88.             $driverFile     =   'Date' . DIRECTORY_SEPARATOR . 'Holidays' . DIRECTORY_SEPARATOR . 
  89.                 'Driver' . DIRECTORY_SEPARATOR . $driverId '.php';
  90.  
  91.             @include_once $driverFile;
  92.             if (class_exists($driverClass)) {
  93.                 return Date_Holidays::raiseError(DATE_HOLIDAYS_ERROR_DRIVERFILE_NOT_FOUND
  94.                     'Couldn\'t find file of the driver-class,  filename: ' $driverFile);
  95.             }
  96.         }
  97.         $driver             =&  new $driverClass;
  98.         
  99.         if (is_null($year)) {
  100.             $year           =   date('Y');
  101.         }
  102.         // sets internal var $_year and performs _buildHolidays()
  103.         $res                =   $driver->setYear($year);
  104.         if (Date_Holidays::isError($res)) {
  105.             return $res;
  106.         }
  107.  
  108.         if (is_null($locale)) {
  109.             $locale         =   setlocale(LC_ALLnull);
  110.         }
  111.         $driver->setLocale($locale);
  112.         return $driver;
  113.     }
  114.     
  115.    /**
  116.     * Returns the error-stack
  117.     *
  118.     * @static
  119.     * @access   public
  120.     * @return   object PEAR_ErrorStack  error-stack
  121.     */
  122.     function &getErrorStack()
  123.     {
  124.         return PEAR_ErrorStack::singleton('Date_Holidays'falsefalsetrue);
  125.     }
  126.     
  127.    /**
  128.     * Pushes a new error on the error-stack and returns a PEAR_Error object
  129.     *
  130.     * @static
  131.     * @access   public
  132.     * @param    int     $code   error-code
  133.     * @param    string  $msg    error-message
  134.     * @return   object PEAR_Error 
  135.     */
  136.     function raiseError($code$msg = null)
  137.     {
  138.         $errorStack &Date_Holidays::getErrorStack();
  139.         return $errorStack->push($code'error'array()$msgfalsedebug_backtrace());
  140.     }
  141.     
  142.    /**
  143.     * Checks a variable to determine whether it represnts an error object or not
  144.     *
  145.     * @static
  146.     * @access   public
  147.     * @param    mixed   $data   variable to test
  148.     * @param    int     $code   if $data is an PEAR_Error object, return true
  149.     *                            only if $code is a string and
  150.     *                            $obj->getMessage() == $code or
  151.     *                            $code is an integer and $obj->getCode() == $code
  152.     * @return   boolean true if $subject is an error object
  153.     */
  154.     function isError($data$code = null)
  155.     {
  156.         $errorClass get_class($data);
  157.         switch (strtolower($errorClass)) {
  158.             case 'pear_error':
  159.                 return PEAR::isError($data$code);
  160.                 break;
  161.             case 'pear_errorstack':
  162.                 return $data->hasErrors();
  163.                 break;
  164.         }
  165.         return false;
  166.     }
  167.     
  168.    /**
  169.     * Checks whether errors occured
  170.     *
  171.     * @static
  172.     * @access   public
  173.     * @return   boolean true if errors occurred
  174.     */
  175.     function errorsOccurred()
  176.     {
  177.         $errorStack &Date_Holidays::getErrorStack();
  178.         return $errorStack->hasErrors();
  179.     }
  180.     
  181.    /**
  182.     * Returns the errors the error-stack contains
  183.     * 
  184.     * @static
  185.     * @access   public
  186.     * @param    boolean $purge  true if the stall shall be purged
  187.     * @return   array   errors
  188.     */
  189.     function getErrors($purge = false)
  190.     {
  191.         $errorStack &Date_Holidays::getErrorStack();
  192.         return $errorStack->getErrors($purge);
  193.     }
  194.     
  195.    /**
  196.     * Set a property for the Date_Holidays drivers
  197.     *
  198.     * Available properties:
  199.     * <pre>
  200.     * DIE_ON_MISSING_LOCALE = boolean
  201.     *   false: if no localized holiday-title is found an error will be returned
  202.     *   true: if no localized holiday-title is found the default translation (English) will be used
  203.     * </pre>
  204.     * 
  205.     * @static
  206.     * @access   public
  207.     * @param    string  $prop   property
  208.     * @param    string  $value  property-value
  209.     */
  210.     function staticSetProperty($prop$value)
  211.     {
  212.         if (isset($GLOBALS['_DATE_HOLIDAYS'])) {
  213.             $GLOBALS['_DATE_HOLIDAYS'= array();
  214.         }
  215.         
  216.         switch ($prop{
  217.             case 'DIE_ON_MISSING_LOCALE':
  218.                 if (is_bool($value)) {
  219.                     $GLOBALS['_DATE_HOLIDAYS'][$prop$value;
  220.                 }
  221.                 break;
  222.                 
  223.             default:
  224.                 break;
  225.         }
  226.     }
  227.     
  228.    /**
  229.     * Returns an internal property value
  230.     *
  231.     * @static
  232.     * @access   public
  233.     * @param    string  $prop   property-name
  234.     * @return   mixed   property value on success, otherwise null
  235.     */
  236.     function staticGetProperty($prop)
  237.     {
  238.         if (isset($GLOBALS['_DATE_HOLIDAYS'])) {
  239.             return null;
  240.         }
  241.         
  242.         switch ($prop{
  243.             case 'DIE_ON_MISSING_LOCALE':
  244.                 if (isset($GLOBALS['_DATE_HOLIDAYS'][$prop])) {
  245.                     return $GLOBALS['_DATE_HOLIDAYS'][$prop];
  246.                 }
  247.                 return null;
  248.                 break;
  249.                 
  250.             default:
  251.                 return null;
  252.                 break;
  253.         }
  254.     }
  255. }
  256. ?>

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