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.  * Holidays.php
  5.  *
  6.  * PHP Version 4
  7.  *
  8.  * Copyright (c) 1997-2008 The PHP Group
  9.  *
  10.  * This source file is subject to version 2.0 of the PHP license,
  11.  * that is bundled with this package in the file LICENSE, and is
  12.  * available at through the world-wide-web at
  13.  * http://www.php.net/license/2_02.txt.
  14.  * If you did not receive a copy of the PHP license and are unable to
  15.  * obtain it through the world-wide-web, please send a note to
  16.  * license@php.net so we can mail you a copy immediately.
  17.  *
  18.  * Authors:   Carsten Lucke <luckec@tool-garage.de>
  19.  *
  20.  * CVS file id: $Id: Holidays.php,v 1.18 2008/01/06 00:47:48 kguest Exp $
  21.  *
  22.  * @category Date
  23.  * @package  Date_Holidays
  24.  * @author   Carsten Lucke <luckec@tool-garage.de>
  25.  * @author   Stephan Schmidt <schst@php.net>
  26.  * @license  http://www.php.net/license/3_01.txt PHP License 3.0.1
  27.  * @version  CVS: $Id: Holidays.php,v 1.18 2008/01/06 00:47:48 kguest Exp $
  28.  * @link     http://pear.php.net/package/Date_Holidays
  29.  */
  30.  
  31. /**
  32.  * uses PEAR errors
  33.  */
  34. require_once 'PEAR.php';
  35.  
  36. /**
  37.  * uses PEAR::Date
  38.  */
  39. require_once 'Date.php';
  40.  
  41. /**
  42.  * Class that wraps a holiday's data
  43.  */
  44. require_once 'Date/Holidays/Holiday.php';
  45.  
  46. /**
  47.  * Driver baseclass
  48.  */
  49. require_once 'Date/Holidays/Driver.php';
  50.  
  51. /**
  52.  * could not find file of driver-class
  53.  *
  54.  * @access  public
  55.  */
  56. define('DATE_HOLIDAYS_ERROR_DRIVERFILE_NOT_FOUND'1);
  57.  
  58. /**
  59.  * invalid argument was passed to a method
  60.  *
  61.  * @access  public
  62.  */
  63. define('DATE_HOLIDAYS_ERROR_INVALID_ARGUMENT'2);
  64.  
  65. /**
  66.  * Driver directory does not exist
  67.  *
  68.  * @access  public
  69.  */
  70. define('DATE_HOLIDAYS_ERROR_MISSING_DRIVER_DIR'3);
  71.  
  72. /**
  73.  * Filter directory does not exist
  74.  *
  75.  * @access  public
  76.  */
  77. define('DATE_HOLIDAYS_ERROR_MISSING_FILTER_DIR'4);
  78.  
  79. /**
  80.  * class that helps you to locate holidays for a year
  81.  *
  82.  * @category Date
  83.  * @package  Date_Holidays
  84.  * @author   Carsten Lucke <luckec@tool-garage.de>
  85.  * @author   Stephan Schmidt <schst@php.net>
  86.  * @license  http://www.php.net/license/3_01.txt PHP License 3.0.1
  87.  * @version  CVS: $Id: Holidays.php,v 1.18 2008/01/06 00:47:48 kguest Exp $
  88.  * @link     http://pear.php.net/package/Date_Holidays
  89.  * @abstract
  90.  */
  91. {
  92.     /**
  93.      * Constructor
  94.      *
  95.      * Use the Date_Holidays::factory() method to construct an object of
  96.      * a certain driver
  97.      *
  98.      * @access   protected
  99.      */
  100.     function Date_Holidays()
  101.     {
  102.     }
  103.  
  104.     /**
  105.      * Factory method that creates a driver-object
  106.      *
  107.      * @param string  $driverId driver-name
  108.      * @param string  $year     year
  109.      * @param string  $locale   locale name
  110.      * @param boolean $external external driver
  111.      *
  112.      * @static
  113.      * @access   public
  114.      * @return   object  Date_Holidays driver-object on success,
  115.      *                    otherwise a PEAR_Error object
  116.      * @throws   object PEAR_Error
  117.      */
  118.     function factory($driverId$year = null$locale = null$external = false)
  119.     {
  120.         if (isset($GLOBALS['_DATE_HOLIDAYS']['DIE_ON_MISSING_LOCALE'])) {
  121.             Date_Holidays::staticSetProperty('DIE_ON_MISSING_LOCALE'true);
  122.         }
  123.  
  124.         $driverClass 'Date_Holidays_Driver_' $driverId;
  125.         if ($external{
  126.             $driverClass $driverId;
  127.         }
  128.  
  129.         if (class_exists($driverClass)) {
  130.             $driverFile 'Date' . DIRECTORY_SEPARATOR . 'Holidays' .
  131.                           DIRECTORY_SEPARATOR . 'Driver' .
  132.                           DIRECTORY_SEPARATOR . $driverId '.php';
  133.             if ($external{
  134.                 $driverFile str_replace('_',
  135.                                           DIRECTORY_SEPARATOR,
  136.                                           $driverClass'.php';
  137.             }
  138.  
  139.             @include_once $driverFile;
  140.             if (class_exists($driverClass)) {
  141.                 return Date_Holidays::raiseError(DATE_HOLIDAYS_ERROR_DRIVERFILE_NOT_FOUND,
  142.                     'Couldn\'t find file of the driver-class,  filename: ' $driverFile);
  143.             }
  144.         }
  145.         $driver = new $driverClass;
  146.  
  147.         if (is_null($year)) {
  148.             $year date('Y');
  149.         }
  150.         // sets internal var $_year and performs _buildHolidays()
  151.         $res $driver->setYear($year);
  152.         if (Date_Holidays::isError($res)) {
  153.             return $res;
  154.         }
  155.  
  156.         if (is_null($locale)) {
  157.             $locale setlocale(LC_ALL0);
  158.         }
  159.         $driver->setLocale($locale);
  160.         return $driver;
  161.     }
  162.  
  163.     /**
  164.      * Factory method that creates a driver-object
  165.      *
  166.      * @param string  $isoCode  ISO3166 code identifying the driver
  167.      * @param string  $year     year
  168.      * @param string  $locale   locale name
  169.      * @param boolean $external external driver
  170.      *
  171.      * @static
  172.      * @access   public
  173.      * @return   object  Date_Holidays driver-object on success, otherwise
  174.      *                    a PEAR_Error object
  175.      * @throws   object PEAR_Error
  176.      */
  177.     function factoryISO3166($isoCode,
  178.                             $year = null,
  179.                             $locale = null,
  180.                             $external = false)
  181.     {
  182.         $driverDir dirname(__FILE__. DIRECTORY_SEPARATOR .
  183.                      'Holidays' . DIRECTORY_SEPARATOR . 'Driver';
  184.         if (is_dir($driverDir)) {
  185.                     'Date_Holidays driver directory does not exist');
  186.         }
  187.  
  188.         $driverMappings = array();
  189.         $driverFiles    = array();
  190.         $dh             opendir($driverDir);
  191.         while (false !== ($filename readdir($dh))) {
  192.             array_push($driverFiles$filename);
  193.         }
  194.  
  195.         foreach ($driverFiles as $driverFileName{
  196.  
  197.             $file dirname(__FILE__. DIRECTORY_SEPARATOR . 'Holidays'
  198.                     . DIRECTORY_SEPARATOR . 'Driver'
  199.                     . DIRECTORY_SEPARATOR . $driverFileName;
  200.             if (is_file($file)) {
  201.                 continue;
  202.             }
  203.  
  204.             $driverId       str_replace('.php'''$driverFileName);
  205.             $driverClass    'Date_Holidays_Driver_' $driverId;
  206.             $driverFilePath $driverDir . DIRECTORY_SEPARATOR . $driverFileName;
  207.  
  208.             @include_once $driverFilePath;
  209.             if (class_exists($driverClass)) {
  210.                 return Date_Holidays::raiseError(DATE_HOLIDAYS_ERROR_DRIVERFILE_NOT_FOUND,
  211.                         'Couldn\'t find file of the driver-class ' $driverClass
  212.                                 . ',  filename: ' $driverFilePath);
  213.             }
  214.  
  215.             $isoCodes call_user_func(array(
  216.                             $driverClass,
  217.                             DATE_HOLIDAYS_DRIVER_IDENTIFY_ISO3166_METHOD));
  218.  
  219.             foreach ($isoCodes as $code{
  220.                 if (strtolower($code=== $isoCode{
  221.                     return Date_Holidays::factory($driverId,
  222.                                                   $year,
  223.                                                   $locale,
  224.                                                   $external);
  225.                 }
  226.             }
  227.         }
  228.  
  229.         /*
  230.          * If this line is reached the iso-code couldn't be mapped to
  231.          * a driver-class and an error will be thrown.
  232.          */
  233.             'Couldn\'t find a driver for the given ISO 3166 code: ' $isoCode);
  234.  
  235.     }
  236.  
  237.     /**
  238.      * Returns a list of the installed drivers
  239.      *
  240.      * @param string $directory where the drivers are installed
  241.      *
  242.      * @access public
  243.      * @static
  244.      * @return array 
  245.      */
  246.     function getInstalledDrivers($directory = null)
  247.     {
  248.         $drivers = array();
  249.         if ($directory === null{
  250.             $directory dirname(__FILE__'/Holidays/Driver';
  251.         }
  252.         if (!file_exists($directory|| !is_dir($directory)) {
  253.             $msg 'The driver directory "'.$directory.'" does not exist';
  254.             return PEAR::raiseError(DATE_HOLIDAYS_ERROR_MISSING_DRIVER_DIR$msg);
  255.         }
  256.         return Date_Holidays::_getModulesFromDir($directory);
  257.     }
  258.  
  259.     /**
  260.      * Returns a list of the installed filters
  261.      *
  262.      * @param string $directory where the filters are installed
  263.      *
  264.      * @access public
  265.      * @static
  266.      * @return array 
  267.      */
  268.     function getInstalledFilters($directory = null)
  269.     {
  270.         $filters = array();
  271.         if ($directory === null{
  272.             $directory dirname(__FILE__'/Holidays/Filter';
  273.         }
  274.         if (!file_exists($directory|| !is_dir($directory)) {
  275.             $msg 'The filter directory "'.$directory.'" does not exist';
  276.             return PEAR::raiseError(DATE_HOLIDAYS_ERROR_MISSING_FILTER_DIR$msg);
  277.         }
  278.         return Date_Holidays::_getModulesFromDir($directory);
  279.     }
  280.  
  281.     /**
  282.      * Fetch all modules from a directory and its subdirectories
  283.      *
  284.      * @param string $directory specified directory
  285.      * @param string $prefix    for the class names, will be used in recursive calls
  286.      *
  287.      * @static
  288.      * @access protected
  289.      * @return array modules
  290.      */
  291.     function _getModulesFromDir($directory$prefix '')
  292.     {
  293.         $modules = array();
  294.         $d       dir($directory);
  295.         while (false !== $moduleFile $d->read()) {
  296.             if ($moduleFile === '.' ||
  297.                 $moduleFile === '..' ||
  298.                 $moduleFile === 'CVS'{
  299.                 continue;
  300.             }
  301.             if (is_dir($directory.'/'.$moduleFile)) {
  302.                 $modules array_merge($modules,
  303.                         Date_Holidays::_getModulesFromDir($directory.'/'.$moduleFile,
  304.                                                           $prefix.$moduleFile.'_'));
  305.                 continue;
  306.             }
  307.             $matches = array();
  308.             if (preg_match('/(.*)\.php$/'$moduleFile$matches)) {
  309.                 array_push($modulesarray('id'    => $prefix.$matches[1],
  310.                                            'title' => $prefix.$matches[1]));
  311.             }
  312.         }
  313.         return $modules;
  314.     }
  315.  
  316.     /**
  317.      * Returns the error-stack
  318.      *
  319.      * @static
  320.      * @access   public
  321.      * @return   object PEAR_ErrorStack  error-stack
  322.      */
  323.     function &getErrorStack()
  324.     {
  325.         return PEAR_ErrorStack::singleton('Date_Holidays'falsefalsetrue);
  326.     }
  327.  
  328.     /**
  329.      * Pushes a new error on the error-stack and returns a PEAR_Error object
  330.      *
  331.      * @param int    $code error-code
  332.      * @param string $msg  error-message
  333.      *
  334.      * @static
  335.      * @access   public
  336.      * @return   object PEAR_Error 
  337.      */
  338.     function raiseError($code$msg = null)
  339.     {
  340.         $errorStack &Date_Holidays::getErrorStack();
  341.         return $errorStack->push($code,
  342.                                  'error',
  343.                                  array(),
  344.                                  $msg,
  345.                                  false,
  346.                                  debug_backtrace());
  347.     }
  348.  
  349.     /**
  350.      * Checks a variable to determine whether it represents an error object or not
  351.      *
  352.      * @param mixed $data variable to test
  353.      * @param int   $code if $data is an PEAR_Error object, return true
  354.      *                     only if $code is a string and
  355.      *                     $obj->getMessage() == $code or
  356.      *                     $code is an integer and $obj->getCode() == $code
  357.      *
  358.      * @static
  359.      * @access   public
  360.      * @return   boolean true if $subject is an error object
  361.      */
  362.     function isError($data$code = null)
  363.     {
  364.         $errorClass get_class($data);
  365.         switch (strtolower($errorClass)) {
  366.         case 'pear_error':
  367.             return PEAR::isError($data$code);
  368.         case 'pear_errorstack':
  369.             return $data->hasErrors();
  370.         }
  371.         return false;
  372.     }
  373.  
  374.     /**
  375.      * Checks whether errors occured
  376.      *
  377.      * @static
  378.      * @access   public
  379.      * @return   boolean true if errors occurred
  380.      */
  381.     function errorsOccurred()
  382.     {
  383.         $errorStack &Date_Holidays::getErrorStack();
  384.         return $errorStack->hasErrors();
  385.     }
  386.  
  387.     /**
  388.      * Returns the errors the error-stack contains
  389.      *
  390.      * @param boolean $purge true if the stall shall be purged
  391.      *
  392.      * @static
  393.      * @access   public
  394.      * @return   array   errors
  395.      */
  396.     function getErrors($purge = false)
  397.     {
  398.         $errorStack &Date_Holidays::getErrorStack();
  399.         return $errorStack->getErrors($purge);
  400.     }
  401.  
  402.     /**
  403.      * Set a property for the Date_Holidays drivers
  404.      *
  405.      * Available properties:
  406.      * <pre>
  407.      * DIE_ON_MISSING_LOCALE = boolean
  408.      *   false: if no localized holiday-title is found an error will be returned
  409.      *   true: if no localized holiday-title is found then the default
  410.      *         translation (English) will be used
  411.      * </pre>
  412.      *
  413.      * @param string $prop  property
  414.      * @param string $value property-value
  415.      *
  416.      * @access   public
  417.      * @static
  418.      * @return   void 
  419.      */
  420.     function staticSetProperty($prop$value)
  421.     {
  422.         if (isset($GLOBALS['_DATE_HOLIDAYS'])) {
  423.             $GLOBALS['_DATE_HOLIDAYS'= array();
  424.         }
  425.  
  426.         switch ($prop{
  427.         case 'DIE_ON_MISSING_LOCALE':
  428.             if (is_bool($value)) {
  429.                 $GLOBALS['_DATE_HOLIDAYS'][$prop$value;
  430.             }
  431.             break;
  432.         }
  433.     }
  434.  
  435.     /**
  436.      * Returns an internal property value
  437.      *
  438.      * @param string $prop property-name
  439.      *
  440.      * @static
  441.      * @access   public
  442.      * @return   mixed   property value on success, otherwise null
  443.      */
  444.     function staticGetProperty($prop)
  445.     {
  446.         if (isset($GLOBALS['_DATE_HOLIDAYS'])) {
  447.             return null;
  448.         }
  449.  
  450.         switch ($prop{
  451.         case 'DIE_ON_MISSING_LOCALE':
  452.             if (isset($GLOBALS['_DATE_HOLIDAYS'][$prop])) {
  453.                 return $GLOBALS['_DATE_HOLIDAYS'][$prop];
  454.             }
  455.         }
  456.  
  457.         return null;
  458.     }
  459. }
  460. ?>

Documentation generated on Thu, 10 Apr 2008 20:00:20 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.