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.10 2006/04/02 22:19:07 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.10 2006/04/02 22:19:07 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.     * Factory method that creates a driver-object
  147.     *
  148.     * @static
  149.     * @access   public
  150.     * @param    string  $isoCode    ISO3166 code identifying the driver
  151.     * @param    string  $year       year
  152.     * @param    string  $locale     locale name
  153.     * @return   object  Date_Holidays driver-object on success, otherwise a PEAR_Error object
  154.     * @throws   object PEAR_Error
  155.     */
  156.     function factoryISO3166($isoCode$year = null$locale = null$external = false)
  157.     {
  158.         $driverDir dirname(__FILE__. DIRECTORY_SEPARATOR . 'Holidays' . DIRECTORY_SEPARATOR . 'Driver';
  159.         if (is_dir($driverDir)) {
  160.             return Date_Holidays::raiseError(
  161.                     DATE_HOLIDAYS_ERROR_DRIVERFILE_NOT_FOUND
  162.                     'Date_Holidays driver directory does not exist');
  163.         }
  164.         
  165.         $driverMappings = array();
  166.         $driverFiles    = array();
  167.         $dh             opendir($driverDir);
  168.         while (false !== ($filename readdir($dh))) {
  169.             array_push($driverFiles$filename);
  170.         }
  171.         
  172.         foreach ($driverFiles as $driverFileName{
  173.             
  174.             $file dirname(__FILE__. DIRECTORY_SEPARATOR . 'Holidays' 
  175.                     . DIRECTORY_SEPARATOR . 'Driver' 
  176.                     . DIRECTORY_SEPARATOR . $driverFileName;
  177.             if (is_file($file)) {
  178.                 continue;
  179.             }
  180.             
  181.             $driverId       str_replace('.php'''$driverFileName);
  182.             $driverClass    'Date_Holidays_Driver_' $driverId;
  183.             $driverFilePath $driverDir . DIRECTORY_SEPARATOR . $driverFileName;
  184.             
  185.             @include_once $driverFilePath;
  186.             if (class_exists($driverClass)) {
  187.                 return Date_Holidays::raiseError(
  188.                         DATE_HOLIDAYS_ERROR_DRIVERFILE_NOT_FOUND
  189.                         'Couldn\'t find file of the driver-class ' $driverClass 
  190.                                 . ',  filename: ' $driverFilePath);
  191.             }
  192.             
  193.             $isoCodes call_user_func(
  194.                     array(
  195.                             $driverClass
  196.                             DATE_HOLIDAYS_DRIVER_IDENTIFY_ISO3166_METHOD));
  197.                            
  198.             foreach ($isoCodes as $code{
  199.                 if (strtolower($code=== $isoCode{
  200.                     return Date_Holidays::factory($driverId$year$locale$external);
  201.                 }
  202.             }
  203.         }
  204.         
  205.         return null;
  206.         
  207.     }
  208.  
  209.    /**
  210.     * Returns a list of the installed drivers
  211.     *
  212.     * @access public
  213.     * @static
  214.     * @param  string      directory, where the drivers are installed
  215.     * @return array 
  216.     */
  217.     function getInstalledDrivers($directory = null)
  218.     {
  219.         $drivers = array();
  220.         if ($directory === null{
  221.             $directory dirname(__FILE__'/Holidays/Driver';
  222.         }
  223.         if (!file_exists($directory|| !is_dir($directory)) {
  224.             return PEAR::raiseError(DATE_HOLIDAYS_ERROR_MISSING_DRIVER_DIR'The driver directory "'.$directory.'" does not exist');
  225.         }
  226.         return Date_Holidays::_getModulesFromDir($directory);
  227.     }
  228.  
  229.    /**
  230.     * Returns a list of the installed filters
  231.     *
  232.     * @access public
  233.     * @static
  234.     * @param  string      directory, where the filters are installed
  235.     * @return array 
  236.     */
  237.     function getInstalledFilters($directory = null)
  238.     {
  239.         $filters = array();
  240.         if ($directory === null{
  241.             $directory dirname(__FILE__'/Holidays/Filter';
  242.         }
  243.         if (!file_exists($directory|| !is_dir($directory)) {
  244.             return PEAR::raiseError(DATE_HOLIDAYS_ERROR_MISSING_FILTER_DIR'The filter directory "'.$directory.'" does not exist');
  245.         }
  246.         return Date_Holidays::_getModulesFromDir($directory);
  247.     }
  248.  
  249.    /**
  250.     * Fetch all modules from a directory and its subdirectories
  251.     *
  252.     * @static
  253.     * @access protected
  254.     * @param  string        directory
  255.     * @param  string        prefix for the class names, will be used in recursive calls
  256.     */
  257.     function _getModulesFromDir($dir$prefix '')
  258.     {
  259.         $modules = array();
  260.         $d dir($dir);
  261.         while (false !== $moduleFile $d->read()) {
  262.             if ($moduleFile === '.' || $moduleFile === '..' || $moduleFile === 'CVS'{
  263.                 continue;
  264.             }
  265.             if (is_dir($dir.'/'.$moduleFile)) {
  266.                 $modules array_merge($modulesDate_Holidays::_getModulesFromDir($dir.'/'.$moduleFile$prefix.$moduleFile.'_'));
  267.                 continue;
  268.             }
  269.             $matches = array();
  270.             if (preg_match('/(.*)\.php$/'$moduleFile$matches)) {
  271.                 array_push($modulesarray('id' => $prefix.$matches[1]'title' => $prefix.$matches[1]));
  272.             }
  273.         }
  274.         return $modules;
  275.     }
  276.     
  277.    /**
  278.     * Returns the error-stack
  279.     *
  280.     * @static
  281.     * @access   public
  282.     * @return   object PEAR_ErrorStack  error-stack
  283.     */
  284.     function &getErrorStack()
  285.     {
  286.         return PEAR_ErrorStack::singleton('Date_Holidays'falsefalsetrue);
  287.     }
  288.     
  289.    /**
  290.     * Pushes a new error on the error-stack and returns a PEAR_Error object
  291.     *
  292.     * @static
  293.     * @access   public
  294.     * @param    int     $code   error-code
  295.     * @param    string  $msg    error-message
  296.     * @return   object PEAR_Error 
  297.     */
  298.     function raiseError($code$msg = null)
  299.     {
  300.         $errorStack &Date_Holidays::getErrorStack();
  301.         return $errorStack->push($code'error'array()$msgfalsedebug_backtrace());
  302.     }
  303.     
  304.    /**
  305.     * Checks a variable to determine whether it represnts an error object or not
  306.     *
  307.     * @static
  308.     * @access   public
  309.     * @param    mixed   $data   variable to test
  310.     * @param    int     $code   if $data is an PEAR_Error object, return true
  311.     *                            only if $code is a string and
  312.     *                            $obj->getMessage() == $code or
  313.     *                            $code is an integer and $obj->getCode() == $code
  314.     * @return   boolean true if $subject is an error object
  315.     */
  316.     function isError($data$code = null)
  317.     {
  318.         $errorClass get_class($data);
  319.         switch (strtolower($errorClass)) {
  320.             case 'pear_error':
  321.                 return PEAR::isError($data$code);
  322.             case 'pear_errorstack':
  323.                 return $data->hasErrors();
  324.         }
  325.         return false;
  326.     }
  327.     
  328.    /**
  329.     * Checks whether errors occured
  330.     *
  331.     * @static
  332.     * @access   public
  333.     * @return   boolean true if errors occurred
  334.     */
  335.     function errorsOccurred()
  336.     {
  337.         $errorStack &Date_Holidays::getErrorStack();
  338.         return $errorStack->hasErrors();
  339.     }
  340.     
  341.    /**
  342.     * Returns the errors the error-stack contains
  343.     * 
  344.     * @static
  345.     * @access   public
  346.     * @param    boolean $purge  true if the stall shall be purged
  347.     * @return   array   errors
  348.     */
  349.     function getErrors($purge = false)
  350.     {
  351.         $errorStack &Date_Holidays::getErrorStack();
  352.         return $errorStack->getErrors($purge);
  353.     }
  354.     
  355.    /**
  356.     * Set a property for the Date_Holidays drivers
  357.     *
  358.     * Available properties:
  359.     * <pre>
  360.     * DIE_ON_MISSING_LOCALE = boolean
  361.     *   false: if no localized holiday-title is found an error will be returned
  362.     *   true: if no localized holiday-title is found the default translation (English) will be used
  363.     * </pre>
  364.     * 
  365.     * @static
  366.     * @access   public
  367.     * @param    string  $prop   property
  368.     * @param    string  $value  property-value
  369.     */
  370.     function staticSetProperty($prop$value)
  371.     {
  372.         if (isset($GLOBALS['_DATE_HOLIDAYS'])) {
  373.             $GLOBALS['_DATE_HOLIDAYS'= array();
  374.         }
  375.         
  376.         switch ($prop{
  377.             case 'DIE_ON_MISSING_LOCALE':
  378.                 if (is_bool($value)) {
  379.                     $GLOBALS['_DATE_HOLIDAYS'][$prop$value;
  380.                 }
  381.                 break;
  382.         }
  383.     }
  384.     
  385.    /**
  386.     * Returns an internal property value
  387.     *
  388.     * @static
  389.     * @access   public
  390.     * @param    string  $prop   property-name
  391.     * @return   mixed   property value on success, otherwise null
  392.     */
  393.     function staticGetProperty($prop)
  394.     {
  395.         if (isset($GLOBALS['_DATE_HOLIDAYS'])) {
  396.             return null;
  397.         }
  398.         
  399.         switch ($prop{
  400.             case 'DIE_ON_MISSING_LOCALE':
  401.                 if (isset($GLOBALS['_DATE_HOLIDAYS'][$prop])) {
  402.                     return $GLOBALS['_DATE_HOLIDAYS'][$prop];
  403.                 }
  404.         }
  405.         
  406.         return null;
  407.     }
  408. }
  409. ?>

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