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.9 2006/04/02 17:18:03 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.  * Driver directory does not exist
  57.  *
  58.  * @access  public
  59.  */
  60. define('DATE_HOLIDAYS_ERROR_MISSING_DRIVER_DIR'3);
  61.  
  62. /**
  63.  * Filter directory does not exist
  64.  *
  65.  * @access  public
  66.  */
  67. define('DATE_HOLIDAYS_ERROR_MISSING_FILTER_DIR'4);
  68.  
  69. /**
  70.  * class that helps you to locate holidays for a year
  71.  *
  72.  * @abstract
  73.  * @category Date
  74.  * @package  Date_Holidays
  75.  * @version  $Id: Holidays.php,v 1.9 2006/04/02 17:18:03 luckec Exp $
  76.  * @author   Carsten Lucke <luckec@tool-garage.de>
  77.  * @author   Stephan Schmidt <schst@php.net>
  78.  */
  79. class Date_Holidays 
  80. {
  81.   /**
  82.     * Constructor
  83.     *
  84.     * Use the Date_Holidays::factory() method to construct an object of a certain driver
  85.     *
  86.     * @access   protected
  87.     */
  88.     function Date_Holidays()
  89.     {
  90.     }
  91.     
  92.    /**
  93.     * Factory method that creates a driver-object
  94.     *
  95.     * @static
  96.     * @access   public
  97.     * @param    string  $driverId   driver-name
  98.     * @param    string  $year       year
  99.     * @param    string  $locale     locale name
  100.     * @return   object  Date_Holidays driver-object on success, otherwise a PEAR_Error object
  101.     * @throws   object PEAR_Error
  102.     */
  103.     function factory($driverId$year = null$locale = null$external = false)
  104.     {
  105.         if (isset($GLOBALS['_DATE_HOLIDAYS']['DIE_ON_MISSING_LOCALE'])) {
  106.             Date_Holidays::staticSetProperty('DIE_ON_MISSING_LOCALE'true);
  107.         }
  108.         
  109.         $driverClass        'Date_Holidays_Driver_' $driverId;
  110.         if ($external{
  111.             $driverClass    $driverId;
  112.         }
  113.         
  114.         if (class_exists($driverClass)) {
  115.             $driverFile     'Date' . DIRECTORY_SEPARATOR . 'Holidays' . DIRECTORY_SEPARATOR . 
  116.                     'Driver' . DIRECTORY_SEPARATOR . $driverId '.php';
  117.             if ($external{
  118.                 $driverFile str_replace('_'DIRECTORY_SEPARATOR$driverClass'.php';
  119.             }
  120.  
  121.             @include_once $driverFile;
  122.             if (class_exists($driverClass)) {
  123.                 return Date_Holidays::raiseError(DATE_HOLIDAYS_ERROR_DRIVERFILE_NOT_FOUND
  124.                     'Couldn\'t find file of the driver-class,  filename: ' $driverFile);
  125.             }
  126.         }
  127.         $driver             = new $driverClass;
  128.         
  129.         if (is_null($year)) {
  130.             $year           =   date('Y');
  131.         }
  132.         // sets internal var $_year and performs _buildHolidays()
  133.         $res                =   $driver->setYear($year);
  134.         if (Date_Holidays::isError($res)) {
  135.             return $res;
  136.         }
  137.  
  138.         if (is_null($locale)) {
  139.             $locale         =   setlocale(LC_ALLnull);
  140.         }
  141.         $driver->setLocale($locale);
  142.         return $driver;
  143.     }
  144.  
  145.    /**
  146.     * Returns a list of the installed drivers
  147.     *
  148.     * @access public
  149.     * @static
  150.     * @param  string      directory, where the drivers are installed
  151.     * @return array 
  152.     */
  153.     function getInstalledDrivers($directory = null)
  154.     {
  155.         $drivers = array();
  156.         if ($directory === null{
  157.             $directory dirname(__FILE__'/Holidays/Driver';
  158.         }
  159.         if (!file_exists($directory|| !is_dir($directory)) {
  160.             return PEAR::raiseError(DATE_HOLIDAYS_ERROR_MISSING_DRIVER_DIR'The driver directory "'.$directory.'" does not exist');
  161.         }
  162.         return Date_Holidays::_getModulesFromDir($directory);
  163.     }
  164.  
  165.    /**
  166.     * Returns a list of the installed filters
  167.     *
  168.     * @access public
  169.     * @static
  170.     * @param  string      directory, where the filters are installed
  171.     * @return array 
  172.     */
  173.     function getInstalledFilters($directory = null)
  174.     {
  175.         $filters = array();
  176.         if ($directory === null{
  177.             $directory dirname(__FILE__'/Holidays/Filter';
  178.         }
  179.         if (!file_exists($directory|| !is_dir($directory)) {
  180.             return PEAR::raiseError(DATE_HOLIDAYS_ERROR_MISSING_FILTER_DIR'The filter directory "'.$directory.'" does not exist');
  181.         }
  182.         return Date_Holidays::_getModulesFromDir($directory);
  183.     }
  184.  
  185.    /**
  186.     * Fetch all modules from a directory and its subdirectories
  187.     *
  188.     * @static
  189.     * @access protected
  190.     * @param  string        directory
  191.     * @param  string        prefix for the class names, will be used in recursive calls
  192.     */
  193.     function _getModulesFromDir($dir$prefix '')
  194.     {
  195.         $modules = array();
  196.         $d dir($dir);
  197.         while (false !== $moduleFile $d->read()) {
  198.             if ($moduleFile === '.' || $moduleFile === '..' || $moduleFile === 'CVS'{
  199.                 continue;
  200.             }
  201.             if (is_dir($dir.'/'.$moduleFile)) {
  202.                 $modules array_merge($modulesDate_Holidays::_getModulesFromDir($dir.'/'.$moduleFile$prefix.$moduleFile.'_'));
  203.                 continue;
  204.             }
  205.             $matches = array();
  206.             if (preg_match('/(.*)\.php$/'$moduleFile$matches)) {
  207.                 array_push($modulesarray('id' => $prefix.$matches[1]'title' => $prefix.$matches[1]));
  208.             }
  209.         }
  210.         return $modules;
  211.     }
  212.     
  213.    /**
  214.     * Returns the error-stack
  215.     *
  216.     * @static
  217.     * @access   public
  218.     * @return   object PEAR_ErrorStack  error-stack
  219.     */
  220.     function &getErrorStack()
  221.     {
  222.         return PEAR_ErrorStack::singleton('Date_Holidays'falsefalsetrue);
  223.     }
  224.     
  225.    /**
  226.     * Pushes a new error on the error-stack and returns a PEAR_Error object
  227.     *
  228.     * @static
  229.     * @access   public
  230.     * @param    int     $code   error-code
  231.     * @param    string  $msg    error-message
  232.     * @return   object PEAR_Error 
  233.     */
  234.     function raiseError($code$msg = null)
  235.     {
  236.         $errorStack &Date_Holidays::getErrorStack();
  237.         return $errorStack->push($code'error'array()$msgfalsedebug_backtrace());
  238.     }
  239.     
  240.    /**
  241.     * Checks a variable to determine whether it represnts an error object or not
  242.     *
  243.     * @static
  244.     * @access   public
  245.     * @param    mixed   $data   variable to test
  246.     * @param    int     $code   if $data is an PEAR_Error object, return true
  247.     *                            only if $code is a string and
  248.     *                            $obj->getMessage() == $code or
  249.     *                            $code is an integer and $obj->getCode() == $code
  250.     * @return   boolean true if $subject is an error object
  251.     */
  252.     function isError($data$code = null)
  253.     {
  254.         $errorClass get_class($data);
  255.         switch (strtolower($errorClass)) {
  256.             case 'pear_error':
  257.                 return PEAR::isError($data$code);
  258.             case 'pear_errorstack':
  259.                 return $data->hasErrors();
  260.         }
  261.         return false;
  262.     }
  263.     
  264.    /**
  265.     * Checks whether errors occured
  266.     *
  267.     * @static
  268.     * @access   public
  269.     * @return   boolean true if errors occurred
  270.     */
  271.     function errorsOccurred()
  272.     {
  273.         $errorStack &Date_Holidays::getErrorStack();
  274.         return $errorStack->hasErrors();
  275.     }
  276.     
  277.    /**
  278.     * Returns the errors the error-stack contains
  279.     * 
  280.     * @static
  281.     * @access   public
  282.     * @param    boolean $purge  true if the stall shall be purged
  283.     * @return   array   errors
  284.     */
  285.     function getErrors($purge = false)
  286.     {
  287.         $errorStack &Date_Holidays::getErrorStack();
  288.         return $errorStack->getErrors($purge);
  289.     }
  290.     
  291.    /**
  292.     * Set a property for the Date_Holidays drivers
  293.     *
  294.     * Available properties:
  295.     * <pre>
  296.     * DIE_ON_MISSING_LOCALE = boolean
  297.     *   false: if no localized holiday-title is found an error will be returned
  298.     *   true: if no localized holiday-title is found the default translation (English) will be used
  299.     * </pre>
  300.     * 
  301.     * @static
  302.     * @access   public
  303.     * @param    string  $prop   property
  304.     * @param    string  $value  property-value
  305.     */
  306.     function staticSetProperty($prop$value)
  307.     {
  308.         if (isset($GLOBALS['_DATE_HOLIDAYS'])) {
  309.             $GLOBALS['_DATE_HOLIDAYS'= array();
  310.         }
  311.         
  312.         switch ($prop{
  313.             case 'DIE_ON_MISSING_LOCALE':
  314.                 if (is_bool($value)) {
  315.                     $GLOBALS['_DATE_HOLIDAYS'][$prop$value;
  316.                 }
  317.                 break;
  318.         }
  319.     }
  320.     
  321.    /**
  322.     * Returns an internal property value
  323.     *
  324.     * @static
  325.     * @access   public
  326.     * @param    string  $prop   property-name
  327.     * @return   mixed   property value on success, otherwise null
  328.     */
  329.     function staticGetProperty($prop)
  330.     {
  331.         if (isset($GLOBALS['_DATE_HOLIDAYS'])) {
  332.             return null;
  333.         }
  334.         
  335.         switch ($prop{
  336.             case 'DIE_ON_MISSING_LOCALE':
  337.                 if (isset($GLOBALS['_DATE_HOLIDAYS'][$prop])) {
  338.                     return $GLOBALS['_DATE_HOLIDAYS'][$prop];
  339.                 }
  340.         }
  341.         
  342.         return null;
  343.     }
  344. }
  345. ?>

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