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.13 2007/05/28 14:52:56 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.13 2007/05/28 14:52:56 luckec Exp $
  76.  * @author   Carsten Lucke <luckec@tool-garage.de>
  77.  * @author   Stephan Schmidt <schst@php.net>
  78.  */
  79. {
  80.   /**
  81.     * Constructor
  82.     *
  83.     * Use the Date_Holidays::factory() method to construct an object of a certain driver
  84.     *
  85.     * @access   protected
  86.     */
  87.     function Date_Holidays()
  88.     {
  89.     }
  90.  
  91.    /**
  92.     * Factory method that creates a driver-object
  93.     *
  94.     * @static
  95.     * @access   public
  96.     * @param    string  $driverId   driver-name
  97.     * @param    string  $year       year
  98.     * @param    string  $locale     locale name
  99.     * @return   object  Date_Holidays driver-object on success, otherwise a PEAR_Error object
  100.     * @throws   object PEAR_Error
  101.     */
  102.     function factory($driverId$year = null$locale = null$external = false)
  103.     {
  104.         if (isset($GLOBALS['_DATE_HOLIDAYS']['DIE_ON_MISSING_LOCALE'])) {
  105.             Date_Holidays::staticSetProperty('DIE_ON_MISSING_LOCALE'true);
  106.         }
  107.  
  108.         $driverClass        'Date_Holidays_Driver_' $driverId;
  109.         if ($external{
  110.             $driverClass    $driverId;
  111.         }
  112.  
  113.         if (class_exists($driverClass)) {
  114.             $driverFile     'Date' . DIRECTORY_SEPARATOR . 'Holidays' . DIRECTORY_SEPARATOR .
  115.                     'Driver' . DIRECTORY_SEPARATOR . $driverId '.php';
  116.             if ($external{
  117.                 $driverFile str_replace('_'DIRECTORY_SEPARATOR$driverClass'.php';
  118.             }
  119.  
  120.             @include_once $driverFile;
  121.             if (class_exists($driverClass)) {
  122.                 return Date_Holidays::raiseError(DATE_HOLIDAYS_ERROR_DRIVERFILE_NOT_FOUND,
  123.                     'Couldn\'t find file of the driver-class,  filename: ' $driverFile);
  124.             }
  125.         }
  126.         $driver             = new $driverClass;
  127.  
  128.         if (is_null($year)) {
  129.             $year           =   date('Y');
  130.         }
  131.         // sets internal var $_year and performs _buildHolidays()
  132.         $res                =   $driver->setYear($year);
  133.         if (Date_Holidays::isError($res)) {
  134.             return $res;
  135.         }
  136.  
  137.         if (is_null($locale)) {
  138.             $locale         =   setlocale(LC_ALL0);
  139.         }
  140.         $driver->setLocale($locale);
  141.         return $driver;
  142.     }
  143.  
  144.    /**
  145.     * Factory method that creates a driver-object
  146.     *
  147.     * @static
  148.     * @access   public
  149.     * @param    string  $isoCode    ISO3166 code identifying the driver
  150.     * @param    string  $year       year
  151.     * @param    string  $locale     locale name
  152.     * @return   object  Date_Holidays driver-object on success, otherwise a PEAR_Error object
  153.     * @throws   object PEAR_Error
  154.     */
  155.     function factoryISO3166($isoCode$year = null$locale = null$external = false)
  156.     {
  157.         $driverDir dirname(__FILE__. DIRECTORY_SEPARATOR . 'Holidays' . DIRECTORY_SEPARATOR . 'Driver';
  158.         if (is_dir($driverDir)) {
  159.             return Date_Holidays::raiseError(
  160.                     DATE_HOLIDAYS_ERROR_DRIVERFILE_NOT_FOUND,
  161.                     'Date_Holidays driver directory does not exist');
  162.         }
  163.  
  164.         $driverMappings = array();
  165.         $driverFiles    = array();
  166.         $dh             opendir($driverDir);
  167.         while (false !== ($filename readdir($dh))) {
  168.             array_push($driverFiles$filename);
  169.         }
  170.  
  171.         foreach ($driverFiles as $driverFileName{
  172.  
  173.             $file dirname(__FILE__. DIRECTORY_SEPARATOR . 'Holidays'
  174.                     . DIRECTORY_SEPARATOR . 'Driver'
  175.                     . DIRECTORY_SEPARATOR . $driverFileName;
  176.             if (is_file($file)) {
  177.                 continue;
  178.             }
  179.  
  180.             $driverId       str_replace('.php'''$driverFileName);
  181.             $driverClass    'Date_Holidays_Driver_' $driverId;
  182.             $driverFilePath $driverDir . DIRECTORY_SEPARATOR . $driverFileName;
  183.  
  184.             @include_once $driverFilePath;
  185.             if (class_exists($driverClass)) {
  186.                 return Date_Holidays::raiseError(
  187.                         DATE_HOLIDAYS_ERROR_DRIVERFILE_NOT_FOUND,
  188.                         'Couldn\'t find file of the driver-class ' $driverClass
  189.                                 . ',  filename: ' $driverFilePath);
  190.             }
  191.  
  192.             $isoCodes call_user_func(
  193.                     array(
  194.                             $driverClass,
  195.                             DATE_HOLIDAYS_DRIVER_IDENTIFY_ISO3166_METHOD));
  196.  
  197.             foreach ($isoCodes as $code{
  198.                 if (strtolower($code=== $isoCode{
  199.                     return Date_Holidays::factory($driverId$year$locale$external);
  200.                 }
  201.             }
  202.         }
  203.  
  204.         /*
  205.          * If this line is reached the iso-code couldn't be mapped to
  206.          * a driver-class and an error will be thrown.
  207.          */
  208.             'Couldn\'t find a driver for the given ISO 3166 code: ' $isoCode);
  209.  
  210.     }
  211.  
  212.    /**
  213.     * Returns a list of the installed drivers
  214.     *
  215.     * @access public
  216.     * @static
  217.     * @param  string      directory, where the drivers are installed
  218.     * @return array 
  219.     */
  220.     function getInstalledDrivers($directory = null)
  221.     {
  222.         $drivers = array();
  223.         if ($directory === null{
  224.             $directory dirname(__FILE__'/Holidays/Driver';
  225.         }
  226.         if (!file_exists($directory|| !is_dir($directory)) {
  227.             return PEAR::raiseError(DATE_HOLIDAYS_ERROR_MISSING_DRIVER_DIR'The driver directory "'.$directory.'" does not exist');
  228.         }
  229.         return Date_Holidays::_getModulesFromDir($directory);
  230.     }
  231.  
  232.    /**
  233.     * Returns a list of the installed filters
  234.     *
  235.     * @access public
  236.     * @static
  237.     * @param  string      directory, where the filters are installed
  238.     * @return array 
  239.     */
  240.     function getInstalledFilters($directory = null)
  241.     {
  242.         $filters = array();
  243.         if ($directory === null{
  244.             $directory dirname(__FILE__'/Holidays/Filter';
  245.         }
  246.         if (!file_exists($directory|| !is_dir($directory)) {
  247.             return PEAR::raiseError(DATE_HOLIDAYS_ERROR_MISSING_FILTER_DIR'The filter directory "'.$directory.'" does not exist');
  248.         }
  249.         return Date_Holidays::_getModulesFromDir($directory);
  250.     }
  251.  
  252.    /**
  253.     * Fetch all modules from a directory and its subdirectories
  254.     *
  255.     * @static
  256.     * @access protected
  257.     * @param  string        directory
  258.     * @param  string        prefix for the class names, will be used in recursive calls
  259.     */
  260.     function _getModulesFromDir($dir$prefix '')
  261.     {
  262.         $modules = array();
  263.         $d dir($dir);
  264.         while (false !== $moduleFile $d->read()) {
  265.             if ($moduleFile === '.' || $moduleFile === '..' || $moduleFile === 'CVS'{
  266.                 continue;
  267.             }
  268.             if (is_dir($dir.'/'.$moduleFile)) {
  269.                 $modules array_merge($modulesDate_Holidays::_getModulesFromDir($dir.'/'.$moduleFile$prefix.$moduleFile.'_'));
  270.                 continue;
  271.             }
  272.             $matches = array();
  273.             if (preg_match('/(.*)\.php$/'$moduleFile$matches)) {
  274.                 array_push($modulesarray('id' => $prefix.$matches[1]'title' => $prefix.$matches[1]));
  275.             }
  276.         }
  277.         return $modules;
  278.     }
  279.  
  280.    /**
  281.     * Returns the error-stack
  282.     *
  283.     * @static
  284.     * @access   public
  285.     * @return   object PEAR_ErrorStack  error-stack
  286.     */
  287.     function &getErrorStack()
  288.     {
  289.         return PEAR_ErrorStack::singleton('Date_Holidays'falsefalsetrue);
  290.     }
  291.  
  292.    /**
  293.     * Pushes a new error on the error-stack and returns a PEAR_Error object
  294.     *
  295.     * @static
  296.     * @access   public
  297.     * @param    int     $code   error-code
  298.     * @param    string  $msg    error-message
  299.     * @return   object PEAR_Error 
  300.     */
  301.     function raiseError($code$msg = null)
  302.     {
  303.         $errorStack &Date_Holidays::getErrorStack();
  304.         return $errorStack->push($code'error'array()$msgfalsedebug_backtrace());
  305.     }
  306.  
  307.    /**
  308.     * Checks a variable to determine whether it represnts an error object or not
  309.     *
  310.     * @static
  311.     * @access   public
  312.     * @param    mixed   $data   variable to test
  313.     * @param    int     $code   if $data is an PEAR_Error object, return true
  314.     *                            only if $code is a string and
  315.     *                            $obj->getMessage() == $code or
  316.     *                            $code is an integer and $obj->getCode() == $code
  317.     * @return   boolean true if $subject is an error object
  318.     */
  319.     function isError($data$code = null)
  320.     {
  321.         $errorClass get_class($data);
  322.         switch (strtolower($errorClass)) {
  323.             case 'pear_error':
  324.                 return PEAR::isError($data$code);
  325.             case 'pear_errorstack':
  326.                 return $data->hasErrors();
  327.         }
  328.         return false;
  329.     }
  330.  
  331.    /**
  332.     * Checks whether errors occured
  333.     *
  334.     * @static
  335.     * @access   public
  336.     * @return   boolean true if errors occurred
  337.     */
  338.     function errorsOccurred()
  339.     {
  340.         $errorStack &Date_Holidays::getErrorStack();
  341.         return $errorStack->hasErrors();
  342.     }
  343.  
  344.    /**
  345.     * Returns the errors the error-stack contains
  346.     *
  347.     * @static
  348.     * @access   public
  349.     * @param    boolean $purge  true if the stall shall be purged
  350.     * @return   array   errors
  351.     */
  352.     function getErrors($purge = false)
  353.     {
  354.         $errorStack &Date_Holidays::getErrorStack();
  355.         return $errorStack->getErrors($purge);
  356.     }
  357.  
  358.    /**
  359.     * Set a property for the Date_Holidays drivers
  360.     *
  361.     * Available properties:
  362.     * <pre>
  363.     * DIE_ON_MISSING_LOCALE = boolean
  364.     *   false: if no localized holiday-title is found an error will be returned
  365.     *   true: if no localized holiday-title is found the default translation (English) will be used
  366.     * </pre>
  367.     *
  368.     * @static
  369.     * @access   public
  370.     * @param    string  $prop   property
  371.     * @param    string  $value  property-value
  372.     */
  373.     function staticSetProperty($prop$value)
  374.     {
  375.         if (isset($GLOBALS['_DATE_HOLIDAYS'])) {
  376.             $GLOBALS['_DATE_HOLIDAYS'= array();
  377.         }
  378.  
  379.         switch ($prop{
  380.             case 'DIE_ON_MISSING_LOCALE':
  381.                 if (is_bool($value)) {
  382.                     $GLOBALS['_DATE_HOLIDAYS'][$prop$value;
  383.                 }
  384.                 break;
  385.         }
  386.     }
  387.  
  388.    /**
  389.     * Returns an internal property value
  390.     *
  391.     * @static
  392.     * @access   public
  393.     * @param    string  $prop   property-name
  394.     * @return   mixed   property value on success, otherwise null
  395.     */
  396.     function staticGetProperty($prop)
  397.     {
  398.         if (isset($GLOBALS['_DATE_HOLIDAYS'])) {
  399.             return null;
  400.         }
  401.  
  402.         switch ($prop{
  403.             case 'DIE_ON_MISSING_LOCALE':
  404.                 if (isset($GLOBALS['_DATE_HOLIDAYS'][$prop])) {
  405.                     return $GLOBALS['_DATE_HOLIDAYS'][$prop];
  406.                 }
  407.         }
  408.  
  409.         return null;
  410.     }
  411. }
  412. ?>

Documentation generated on Mon, 11 Mar 2019 15:03:07 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.