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

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