Date_Holidays
[ class tree: Date_Holidays ] [ index: Date_Holidays ] [ all elements ]

Source for file Driver.php

Documentation is available at Driver.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: Driver.php,v 1.18 2005/12/22 21:10:14 luckec Exp $s
  20.  
  21. /**
  22.  * uses PEAR_Errorstack
  23.  */
  24. require_once 'PEAR/ErrorStack.php';
  25. require_once 'Date/Holidays/Filter.php';
  26. require_once 'Date/Holidays/Filter/Whitelist.php';
  27. require_once 'Date/Holidays/Filter/Blacklist.php';
  28.  
  29. /**
  30.  * invalid internal name
  31.  *
  32.  * @access  public
  33.  */
  34. define('DATE_HOLIDAYS_INVALID_INTERNAL_NAME'51);
  35.  
  36. /**
  37.  * title for a holiday is not available
  38.  *
  39.  * @access  public
  40.  */
  41. define('DATE_HOLIDAYS_TITLE_UNAVAILABLE'52);
  42.  
  43. /**
  44.  * date could not be converted into a PEAR::Date object
  45.  *
  46.  * date was neither a timestamp nor a string
  47.  *
  48.  * @access  public
  49.  * @deprecated   will certainly be removed
  50.  */
  51. define('DATE_HOLIDAYS_INVALID_DATE'53);
  52.  
  53. /**
  54.  * string that represents a date has wrong format
  55.  *
  56.  * format must be YYYY-MM-DD
  57.  *
  58.  * @access  public
  59.  * @deprecated   will certainly be removed
  60.  */
  61. define('DATE_HOLIDAYS_INVALID_DATE_FORMAT'54);
  62.  
  63. /**
  64.  * date for a holiday is not available
  65.  *
  66.  * @access  public
  67.  */
  68. define('DATE_HOLIDAYS_DATE_UNAVAILABLE'55);
  69.  
  70. /**
  71.  * language-file doesn't exist
  72.  *
  73.  * @access  public
  74.  */
  75. define('DATE_HOLIDAYS_LANGUAGEFILE_NOT_FOUND'56);
  76.  
  77. /**
  78.  * unable to read language-file
  79.  *
  80.  * @access  public
  81.  */
  82. define('DATE_HOLIDAYS_UNABLE_TO_READ_TRANSLATIONDATA'57);
  83.  
  84. /**
  85.  * class that helps you to locate holidays for a year
  86.  *
  87.  * @abstract
  88.  * @category    Date
  89.  * @package     Date_Holidays
  90.  * @subpackage  Driver
  91.  * @version     $Id: Driver.php,v 1.18 2005/12/22 21:10:14 luckec Exp $
  92.  * @author      Carsten Lucke <luckec@tool-garage.de>
  93.  */
  94. {
  95.    /**
  96.     * locale setting for output
  97.     *
  98.     * @access   protected
  99.     * @var      string 
  100.     */
  101.     var $_locale;
  102.     
  103.    /**
  104.     * locales for which translations of holiday titles are available
  105.     *
  106.     * @access   private
  107.     * @var      array 
  108.     */
  109.     var $_availableLocales = array('C');
  110.     
  111.    /**
  112.     * object's current year
  113.     *
  114.     * @access   protected
  115.     * @var      int 
  116.     */
  117.     var $_year;
  118.     
  119.    /**
  120.     * internal names for the available holidays
  121.     *
  122.     * @access   protected
  123.     * @var      array 
  124.     */
  125.     var $_internalNames = array();
  126.     
  127.    /**
  128.     * dates of the available holidays
  129.     *
  130.     * @access   protected
  131.     * @var      array 
  132.     */
  133.     var $_dates = array();
  134.     
  135.    /**
  136.     * array of the available holidays indexed by date
  137.     *
  138.     * @access   protected
  139.     * @var      array 
  140.     */
  141.     var $_holidays = array();
  142.  
  143.    /**
  144.     * localized names of the available holidays
  145.     *
  146.     * @access   protected
  147.     * @var      array 
  148.     */
  149.     var $_titles = array();
  150.     
  151.    /**
  152.     * Array of holiday-properties indexed by internal-names and furthermore by locales.
  153.     * 
  154.     * <code>
  155.     * $_holidayProperties = array(
  156.     *       'internalName1' =>  array(
  157.     *                               'de_DE' => array(),
  158.     *                               'en_US' => array(),
  159.     *                               'fr_FR' => array()
  160.     *                           )
  161.     *       'internalName2' =>  array(
  162.     *                               'de_DE' => array(),
  163.     *                               'en_US' => array(),
  164.     *                               'fr_FR' => array()
  165.     *                           )
  166.     * );
  167.     * </code>
  168.     */
  169.     var $_holidayProperties = array();
  170.     
  171.    /**
  172.     * Constructor
  173.     *
  174.     * Use the Date_Holidays::factory() method to construct an object of a certain driver
  175.     *
  176.     * @access   protected
  177.     */
  178.     function Date_Holidays_Driver()
  179.     {
  180.     }
  181.     
  182.    /**
  183.     * Sets the driver's current year
  184.     *
  185.     * Calling this method forces the object to rebuild the holidays
  186.     *
  187.     * @access   public
  188.     * @param    int     $year   year
  189.     * @return   boolean true on success, otherwise a PEAR_ErrorStack object
  190.     * @throws   object PEAR_ErrorStack
  191.     * @uses     _buildHolidays()
  192.     */
  193.     function setYear($year)
  194.     {
  195.         $this->_year    =   $year;
  196.         return $this->_buildHolidays();
  197.     }
  198.     
  199.    /**
  200.     * Returns the driver's current year
  201.     *
  202.     * @access   public
  203.     * @return   int     current year
  204.     */
  205.     function getYear()
  206.     {
  207.         return $this->_year;
  208.     }
  209.     
  210.    /**
  211.     * Build the internal arrays that contain data about the calculated holidays
  212.     *
  213.     * @abstract
  214.     * @access   protected
  215.     * @return   boolean true on success, otherwise a PEAR_ErrorStack object
  216.     * @throws   object PEAR_ErrorStack
  217.     */
  218.     function _buildHolidays()
  219.     {
  220.     }
  221.     
  222.    /**
  223.     * Add a driver component
  224.     *
  225.     *
  226.     *
  227.     * @abstract
  228.     * @access   public
  229.     * @param    object Date_Holidays_Driver $driver driver-object
  230.     */
  231.     function addDriver($driver)
  232.     {
  233.     }
  234.     
  235.    /**
  236.     * Remove a driver component
  237.     *
  238.     * @abstract
  239.     * @access   public
  240.     * @param    object Date_Holidays_Driver $driver driver-object
  241.     * @return   boolean true on success, otherwise a PEAR_Error object
  242.     * @throws   object PEAR_Error   DATE_HOLIDAYS_DRIVER_NOT_FOUND
  243.     */
  244.     function removeDriver($driver)
  245.     {
  246.     }
  247.     
  248.    /**
  249.     * Returns the internal names of holidays that were calculated
  250.     *
  251.     * @access   public
  252.     * @return   array 
  253.     */
  254.     function getInternalHolidayNames()
  255.     {
  256.         return $this->_internalNames;
  257.     }
  258.     
  259.    /**
  260.     * Returns localized titles of all holidays or those accepted by the filter
  261.     *
  262.     * @access   public
  263.     * @param    Date_Holidays_Filter    filter-object (or an array !DEPRECATED!)
  264.     * @param    string  $locale         locale setting that shall be used by this method
  265.     * @return   array   array with localized holiday titles on success, otherwise a PEAR_Error object
  266.     * @throws   object PEAR_Error   DATE_HOLIDAYS_INVALID_INTERNAL_NAME
  267.     * @uses     getHolidayTitle()
  268.     */
  269.     function getHolidayTitles($filter = null$locale = null)
  270.     {
  271.         if (is_null($filter)) {
  272.             $filter = new Date_Holidays_Filter_Blacklist(array());
  273.         elseif (is_array($filter)) {
  274.             $filter = new Date_Holidays_Filter_Whitelist($filter);
  275.         }
  276.         
  277.         $titles =   array();
  278.         
  279.         foreach ($this->_internalNames as $internalName{
  280.             if ($filter->accept($internalName)) {
  281.                 $title $this->getHolidayTitle($internalName$locale);
  282.                 if (Date_Holidays::isError($title)) {
  283.                     return $title;
  284.                 }
  285.                 $titles[$internalName$title;
  286.             }
  287.         }
  288.         
  289.         return $titles;
  290.     }
  291.     
  292.    /**
  293.     * Returns localized title for a holiday
  294.     *
  295.     * @access   public
  296.     * @param    string  $internalName   internal name for holiday
  297.     * @param    string  $locale         locale setting that shall be used by this method
  298.     * @return   string  title on success, otherwise a PEAR_Error object
  299.     * @throws   object PEAR_Error   DATE_HOLIDAYS_INVALID_INTERNAL_NAME, DATE_HOLIDAYS_TITLE_UNAVAILABLE
  300.     */
  301.     function getHolidayTitle($internalName$locale = null)
  302.     {
  303.         if (in_array($internalName$this->_internalNames)) {
  304.             return Date_Holidays::raiseError(DATE_HOLIDAYS_INVALID_INTERNAL_NAME'Invalid internal name: ' $internalName);
  305.         }
  306.         
  307.         if (is_null($locale)) {
  308.             $locale =   $this->_findBestLocale($this->_locale);
  309.         else {
  310.             $locale =   $this->_findBestLocale($locale);
  311.         }
  312.         
  313.         if (isset($this->_titles[$locale][$internalName])) {
  314.             if (Date_Holidays::staticGetProperty('DIE_ON_MISSING_LOCALE')) {
  315.                 return Date_Holidays::raiseError(DATE_HOLIDAYS_TITLE_UNAVAILABLE'The internal name (' $internalName 
  316.                     ') for the holiday was correct but no localized title could be found');
  317.             }
  318.         }
  319.  
  320.         return isset($this->_titles[$locale][$internalName]
  321.             $this->_titles[$locale][$internalName$this->_titles['C'][$internalName];
  322.     }
  323.     
  324.     
  325.    /**
  326.     * Returns the localized properties of a holiday. If no properties have been stored an empty
  327.     * array will be returned.
  328.     *
  329.     * @access   public
  330.     * @param    string  $internalName   internal name for holiday
  331.     * @param    string  $locale         locale setting that shall be used by this method
  332.     * @return   array   array of properties on success, otherwise a PEAR_Error object
  333.     * @throws   object PEAR_Error   DATE_HOLIDAYS_INVALID_INTERNAL_NAME
  334.     */
  335.     function getHolidayProperties($internalName$locale = null)
  336.     {
  337.         if (in_array($internalName$this->_internalNames)) {
  338.             return Date_Holidays::raiseError(DATE_HOLIDAYS_INVALID_INTERNAL_NAME'Invalid internal name: ' $internalName);
  339.         }
  340.         
  341.         if (is_null($locale)) {
  342.             $locale =   $this->_findBestLocale($this->_locale);
  343.         else {
  344.             $locale =   $this->_findBestLocale($locale);
  345.         }
  346.  
  347.         
  348.         $properties = array();
  349.         if (isset($this->_holidayProperties[$internalName][$locale])) {
  350.             $properties $this->_holidayProperties[$internalName][$locale];
  351.         }
  352.         return $properties;
  353.     }
  354.     
  355.     
  356.    /**
  357.     * Returns all holidays that the driver knows.
  358.     *
  359.     * You can limit the holidays by passing a filter, then only those
  360.     * holidays accepted by the filter will be returned.
  361.     *
  362.     * Return format:
  363.     * <pre>
  364.     *   array(
  365.     *       'easter'        =>  object of type Date_Holidays_Holiday,
  366.     *       'eastermonday'  =>  object of type Date_Holidays_Holiday,
  367.     *       ...
  368.     *   )
  369.     * </pre>
  370.     *
  371.     * @access   public
  372.     * @param    Date_Holidays_Filter $filter    filter-object (or an array !DEPRECATED!)
  373.     * @param    string  $locale     locale setting that shall be used by this method
  374.     * @return   array   numeric array containing objects of Date_Holidays_Holiday on success,
  375.     *        otherwise a PEAR_Error object
  376.     * @throws   object PEAR_Error   DATE_HOLIDAYS_INVALID_INTERNAL_NAME
  377.     * @see      getHoliday()
  378.     */
  379.     function getHolidays($filter = null$locale = null)
  380.     {
  381.         if (is_null($filter)) {
  382.             $filter = new Date_Holidays_Filter_Blacklist(array());
  383.         elseif (is_array($filter)) {
  384.             $filter = new Date_Holidays_Filter_Whitelist($filter);
  385.         }
  386.         
  387.         if (is_null($locale)) {
  388.             $locale $this->_locale;
  389.         }
  390.         
  391.         $holidays       = array();
  392.         
  393.         foreach ($this->_internalNames as $internalName{
  394.             if ($filter->accept($internalName)) {
  395.                 // no need to check for valid internal-name, will be done by #getHoliday()
  396.                 $holidays[$internalName$this->getHoliday($internalName$locale);
  397.             }
  398.         }
  399.         
  400.         return $holidays;
  401.     }
  402.     
  403.    /**
  404.     * Returns the specified holiday
  405.     *
  406.     * Return format:
  407.     * <pre>
  408.     *   array(
  409.     *       'title' =>  'Easter Sunday'
  410.     *       'date'  =>  '2004-04-11'
  411.     *   )
  412.     * </pre>
  413.     *
  414.     * @access   public
  415.     * @param    string  $internalName   internal name of the holiday
  416.     * @param    string  $locale         locale setting that shall be used by this method
  417.     * @return   object Date_Holidays_Holiday    holiday's information on success, otherwise a PEAR_Error object
  418.     * @throws   object PEAR_Error       DATE_HOLIDAYS_INVALID_INTERNAL_NAME
  419.     * @uses     getHolidayTitle()
  420.     * @uses     getHolidayDate()
  421.     */
  422.     function getHoliday($internalName$locale = null)
  423.     {
  424.         if (in_array($internalName$this->_internalNames)) {
  425.                 'Invalid internal name: ' $internalName);
  426.         }
  427.         if (is_null($locale)) {
  428.             $locale $this->_locale;
  429.         }
  430.         
  431.         $title      $this->getHolidayTitle($internalName$locale);
  432.         if (Date_Holidays::isError($title)) {
  433.             return $title;
  434.         }
  435.         $date       $this->getHolidayDate($internalName);
  436.         if (Date_Holidays::isError($date)) {
  437.             return $date;
  438.         }
  439.         $properties $this->getHolidayProperties($internalName$locale);
  440.         if (Date_Holidays::isError($properties)) {
  441.             return $properties;
  442.         }
  443.         
  444.         $holiday = new Date_Holidays_Holiday($internalName$title$date$properties);
  445.         return $holiday;
  446.     }
  447.     
  448.    /**
  449.     * Determines whether a date represents a holiday or not
  450.     *
  451.     * @access   public
  452.     * @param    mixed   $date       date (can be a timestamp, string or PEAR::Date object)
  453.     * @param    Date_Holidays_Filter $filter    filter-object (or an array !DEPRECATED!)
  454.     * @return   boolean true if date represents a holiday, otherwise false
  455.     * @throws   object PEAR_Error   DATE_HOLIDAYS_INVALID_DATE, DATE_HOLIDAYS_INVALID_DATE_FORMAT
  456.     */
  457.     function isHoliday($date$filter = null)
  458.     {
  459.         if (is_a($date'Date')) {
  460.             $date   $this->_convertDate($date);
  461.             if (Date_Holidays::isError($date)) {
  462.                 return $date;
  463.             }
  464.         }
  465.  
  466.         if (is_null($filter)) {
  467.             $filter = new Date_Holidays_Filter_Blacklist(array());
  468.         elseif (is_array($filter)) {
  469.             $filter = new Date_Holidays_Filter_Whitelist($filter);
  470.         }
  471.         
  472.         foreach (array_keys($this->_datesas $internalName{
  473.             if ($filter->accept($internalName)) {
  474.                 if (Date_Holidays_Driver::dateSloppyCompare(
  475.                         $date$this->_dates[$internalName]!= 0{
  476.                     continue;
  477.                 }
  478.                 return true;
  479.             }
  480.         }
  481.         return false;
  482.     }
  483.     
  484.    /**
  485.     * Returns a <code>Date_Holidays_Holiday</code> object, if any was found,
  486.     * matching the specified date.
  487.     *
  488.     * Normally the method will return the object of the first holiday matching the date.
  489.     * If you want the method to continue searching holidays for the specified date,
  490.     * set the 4th param to true.
  491.     *
  492.     * If multiple holidays match your date, the return value will be an array containing a number
  493.     * of <code>Date_Holidays_Holiday</code> items.
  494.     *
  495.     * @access   public
  496.     * @param    mixed   $date       date (timestamp | string | PEAR::Date object)
  497.     * @param    string  $locale     locale setting that shall be used by this method
  498.     * @param    boolean $multiple 
  499.     * @return   object  object of type Date_Holidays_Holiday on success
  500.     *                    (numeric array of those on multiple search),
  501.     *                    if no holiday was found, matching this date, null is returned
  502.     * @throws   object PEAR_Error   DATE_HOLIDAYS_INVALID_DATE, DATE_HOLIDAYS_INVALID_DATE_FORMAT
  503.     * @uses     getHoliday()
  504.     * @uses     getHolidayTitle()
  505.     * @see      getHoliday()
  506.     ***/
  507.     function getHolidayForDate($date$locale = null$multiple = false)
  508.     {
  509.         if (!is_a($date'Date')) {
  510.             $date $this->_convertDate($date);
  511.             if (Date_Holidays::isError($date)) {
  512.                 return $date;
  513.             }
  514.         }
  515.         $isodate mktime(000$date->getMonth()$date->getDay()$date->getYear());
  516.         unset($date);
  517.         if (is_null($locale)) {
  518.             $locale $this->_locale;
  519.         }
  520.         if (array_key_exists($isodate$this->_holidays)) {
  521.             if (!$multiple{
  522.                 //get only the first feast for this day
  523.                 $internalName $this->_holidays[$isodate][0];
  524.                 $result $this->getHoliday($internalName$locale);
  525.                 return Date_Holidays::isError($result? null : $result;
  526.             }
  527.             // array that collects data, if multiple searching is done
  528.             $data = array();
  529.             foreach($this->_holidays[$isodateas $internalName{
  530.                 $result $this->getHoliday($internalName$locale);
  531.                 if (Date_Holidays::isError($result)) {
  532.                     continue;
  533.                 }
  534.                 $data[$result;
  535.             }
  536.             return $data;
  537.         }
  538.         return null;
  539.     }
  540.     
  541.    /**
  542.     * Returns an array containing a number of <code>Date_Holidays_Holiday</code> items.
  543.     * 
  544.     * If no items have been found the returned array will be empty.
  545.     * 
  546.     * @access   public
  547.     * @param    mixed   $start  date (timestamp | string | PEAR::Date object)
  548.     * @param    mixed   $end    date (timestamp | string | PEAR::Date object)
  549.     * @param    Date_Holidays_Filter $filter    filter-object (or an array !DEPRECATED!)
  550.     * @param    string  $locale locale setting that shall be used by this method
  551.     * @throws   object PEAR_Error   DATE_HOLIDAYS_INVALID_DATE, DATE_HOLIDAYS_INVALID_DATE_FORMAT
  552.     * @return   array   an array containing a number of <code>Date_Holidays_Holiday</code> items
  553.     */
  554.     function getHolidaysForDatespan($start$end$filter = null$locale = null)
  555.     {
  556.         if (is_null($filter)) {
  557.             $filter = new Date_Holidays_Filter_Blacklist(array());
  558.         elseif (is_array($filter)) {
  559.             $filter = new Date_Holidays_Filter_Whitelist($filter);
  560.         }
  561.         
  562.         if (!is_a($start'Date')) {
  563.             $start $this->_convertDate($start);
  564.             if (Date_Holidays::isError($start)) {
  565.                 return $start;
  566.             }
  567.         }
  568.         if (!is_a($end'Date')) {
  569.             $end $this->_convertDate($end);
  570.             if (Date_Holidays::isError($end)) {
  571.                 return $end;
  572.             }
  573.         }
  574.         
  575.         $isodateStart   mktime(000$start->getMonth()$start->getDay()
  576.                 $start->getYear());
  577.         unset($start);
  578.         $isodateEnd     mktime(000$end->getMonth()$end->getDay()$end->getYear());
  579.         unset($end);
  580.         if (is_null($locale)) {
  581.             $locale $this->_locale;
  582.         }
  583.     
  584.         $internalNames = array();
  585.         
  586.         foreach ($this->_holidays as $isoDateTS => $arHolidays{
  587.             if ($isoDateTS >= $isodateStart && $isoDateTS <= $isodateEnd{
  588.                 $internalNames array_merge($internalNames$arHolidays);
  589.             }
  590.         }
  591.         
  592.         $retval = array();
  593.         foreach ($internalNames as $internalName{
  594.             if ($filter->accept($internalName)) {
  595.                 $retval[$this->getHoliday($internalName$locale);
  596.             }
  597.         }
  598.         return $retval;
  599.         
  600.     }
  601.     
  602.    /**
  603.     * Converts timestamp or date-string into da PEAR::Date object
  604.     *
  605.     * @static
  606.     * @access   private
  607.     * @param    mixed   $date   date
  608.     * @return   object PEAR_Date 
  609.     * @throws   object PEAR_Error   DATE_HOLIDAYS_INVALID_DATE, DATE_HOLIDAYS_INVALID_DATE_FORMAT
  610.     */
  611.     function _convertDate($date)
  612.     {
  613.         if (is_string($date)) {
  614.             if (preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}/'$date)) {
  615.                 return Date_Holidays::raiseError(DATE_HOLIDAYS_INVALID_DATE_FORMAT
  616.                     'Date-string has wrong format (must be YYYY-MM-DD)');
  617.             }
  618.             $date = new Date($date);
  619.             return $date;
  620.         }
  621.         
  622.         if (is_int($date)) {
  623.             $date = new Date(date('Y-m-d'$date));
  624.             return $date;
  625.         }
  626.         
  627.             'The date you specified is invalid');
  628.     }
  629.     
  630.    /**
  631.     * Adds all holidays in the array to the driver's internal list of holidays.
  632.     * 
  633.     * Format of the array:
  634.     * <pre>
  635.     *   array(
  636.     *       'newYearsDay'   => array(
  637.     *           'date'          => '01-01',
  638.     *           'title'         => 'New Year\'s Day',
  639.     *           'translations'  => array(
  640.     *               'de_DE' =>  'Neujahr',
  641.     *               'en_EN' =>  'New Year\'s Day'
  642.     *           )
  643.     *       ),
  644.     *       'valentinesDay' => array(
  645.     *           ...
  646.     *       )
  647.     *   );
  648.     * </pre>
  649.     * 
  650.     * @access   protected
  651.     * @param    array       $holidays   static holidays' data
  652.     * @uses     _addHoliday()
  653.     */
  654.     function _addStaticHolidays($holidays)
  655.     {
  656.         foreach ($holidays as $internalName => $holiday{
  657.             // add the holiday's basic data
  658.             $this->_addHoliday($internalName$this->_year . '-' $holiday['date']$holiday['title']);
  659.         }
  660.     }
  661.     
  662.    /**
  663.     * Adds a holiday to the driver's holidays
  664.     *
  665.     * @access   protected
  666.     * @param    string  $internalName   internal name - must not contain characters that aren't allowed as variable-names
  667.     * @param    mixed   $date           date (timestamp | string | PEAR::Date object)
  668.     * @param    string  $title          holiday title
  669.     */
  670.     function _addHoliday($internalName$date$title)
  671.     {
  672.         if (is_a($date'Date')) {
  673.             $date   = new Date($date);
  674.         }
  675.         
  676.         $this->_dates[$internalName]        $date;
  677.         $this->_titles['C'][$internalName]  $title;
  678.         $isodate mktime(000$date->getMonth()$date->getDay()$date->getYear());
  679.         if (!isset($this->_holidays[$isodate])) {
  680.             $this->_holidays[$isodate= array();
  681.         }
  682.         array_push($this->_holidays[$isodate]$internalName);
  683.         array_push($this->_internalNames$internalName);
  684.     }
  685.     
  686.    /**
  687.     * Add a localized translation for a holiday's title. Overwrites existing data.
  688.     *
  689.     * @access   protected
  690.     * @param    string  $internalName   internal name of an existing holiday
  691.     * @param    string  $locale         locale setting that shall be used by this method
  692.     * @param    string  $title          title
  693.     * @return   true on success, otherwise a PEAR_Error object
  694.     * @throws   object PEAR_Error       DATE_HOLIDAYS_INVALID_INTERNAL_NAME
  695.     */
  696.     function _addTranslationForHoliday($internalName$locale$title)
  697.     {
  698.         if (in_array($internalName$this->_internalNames)) {
  699.                 'Couldn\'t add translation (' $locale ') for holiday with this internal name: ' $internalName);
  700.         }
  701.         
  702.         if (in_array($locale$this->_availableLocales)) {
  703.             array_push($this->_availableLocales$locale);
  704.         }
  705.         $this->_titles[$locale][$internalName]  $title;
  706.         return true;
  707.     }
  708.     
  709.     
  710.    /**
  711.     * Adds a localized (regrading translation etc.) string-property for a holiday.
  712.     * Overwrites existing data.
  713.     *  
  714.     * @access   public
  715.     * @param    string  internal-name
  716.     * @param    string  locale-setting
  717.     * @param    string  property-identifier
  718.     * @param    mixed   property-value
  719.     * @return   boolean true on success, false otherwise
  720.     * @throws   PEAR_ErrorStack if internal-name does not exist
  721.     */ 
  722.     function _addStringPropertyForHoliday($internalName$locale$propId$propVal
  723.     {
  724.         if (in_array($internalName$this->_internalNames)) {
  725.                 'Couldn\'t add property (locale: ' $locale ') for holiday with this internal name: ' $internalName);
  726.         }
  727.         
  728.         if (!isset($this->_holidayProperties[$internalName]|| 
  729.                 !is_array($this->_holidayProperties[$internalName])) {
  730.  
  731.             $this->_holidayProperties[$internalName= array();
  732.         }
  733.         
  734.         if (isset($this->_holidayProperties[$internalName][$locale]||
  735.                 !is_array($this->_holidayProperties[$internalName][$locale])) {
  736.                     
  737.             $this->_holidayProperties[$internalName][$locale= array();
  738.         }
  739.         
  740.         $this->_holidayProperties[$internalName][$locale][$propId$propVal;
  741.         return true;
  742.     }
  743.     
  744.    /**
  745.     * Adds a arbitrary number of localized string-properties for the specified holiday.
  746.     * 
  747.     * @access   public
  748.     * @param    string  internal-name
  749.     * @param    string  locale-setting
  750.     * @param    array   associative array: array(propId1 => value1, propid2 => value2, ...)
  751.     * @return   boolean true on success, false otherwise
  752.     * @throws   PEAR_ErrorStack if internal-name does not exist
  753.     */
  754.     function _addStringPropertiesForHoliday($internalName$locale$properties
  755.     {        
  756.         foreach ($properties as $propId => $propValue{
  757.             return $this->_addStringPropertyForHoliday($internalName$locale
  758.                    $propId$propValue);
  759.         }
  760.         
  761.         return true;
  762.     }
  763.     
  764.    /**
  765.     * Add a language-file's content
  766.     * 
  767.     * The language-file's content will be parsed and translations, properties, etc. for
  768.     * holidays will be made available with the specified locale.
  769.     * 
  770.     * @access   public
  771.     * @param    string  $file   filename of the language file
  772.     * @param    string  $locale locale-code of the translation
  773.     * @return   boolean true on success, otherwise a PEAR_ErrorStack object
  774.     * @throws   object PEAR_Errorstack
  775.     */
  776.     function addTranslationFile($file$locale)
  777.     {
  778.         if (file_exists($file)) {
  779.                     'Language-file not found: ' $file);
  780.             return Date_Holidays::getErrorStack();
  781.         }
  782.         
  783.         require_once 'XML/Unserializer.php';
  784.         $options = array(
  785.                             'parseAttributes'   =>  false,
  786.                             'attributesArray'   =>  false,
  787.                             'keyAttribute'      => array('property' => 'id'),
  788.                             'forceEnum'      => array('holiday')
  789.                         );
  790.         $unserializer = new XML_Unserializer($options);
  791.     
  792.         // unserialize the document
  793.         $status $unserializer->unserialize($filetrue);    
  794.     
  795.         if (PEAR::isError($status)) {
  796.             return Date_Holidays::raiseError($status->getCode()$status->getMessage());
  797.         
  798.         
  799.         $content $unserializer->getUnserializedData();
  800.         return $this->_addTranslationData($content$locale);
  801.     }
  802.     
  803.    /**
  804.     * Add a compiled language-file's content
  805.     * 
  806.     * The language-file's content will be unserialized and translations, properties, etc. for
  807.     * holidays will be made available with the specified locale.
  808.     * 
  809.     * @access   public
  810.     * @param    string  $file   filename of the compiled language file
  811.     * @param    string  $locale locale-code of the translation
  812.     * @return   boolean true on success, otherwise a PEAR_ErrorStack object
  813.     * @throws   object PEAR_Errorstack
  814.     */
  815.     function addCompiledTranslationFile($file$locale)
  816.     {
  817.         if (file_exists($file)) {
  818.                     'Language-file not found: ' $file);
  819.             return Date_Holidays::getErrorStack();
  820.         }
  821.         
  822.         $content file_get_contents($file);
  823.         if ($content === false{
  824.             return false;
  825.         }
  826.         $data unserialize($content);
  827.         if ($data === false{
  828.                     'Unable to read translation-data - file maybe damaged: ' $file);
  829.         }
  830.         return $this->_addTranslationData($data$locale);
  831.     }
  832.     
  833.    /**
  834.     * Add a language-file's content. Translations, properties, etc. for
  835.     * holidays will be made available with the specified locale.
  836.     * 
  837.     * @access   public
  838.     * @param    array   $data   translated data
  839.     * @param    string  $locale locale-code of the translation
  840.     * @return   boolean true on success, otherwise a PEAR_ErrorStack object
  841.     * @throws   object PEAR_Errorstack
  842.     */
  843.     function _addTranslationData($data$locale)
  844.     {
  845.         foreach ($data['holidays']['holiday'as $holiday{
  846.             $this->_addTranslationForHoliday($holiday['internal-name']$locale
  847.                     $holiday['translation']);
  848.     
  849.             if (isset($holiday['properties']&& is_array($holiday['properties'])) {
  850.                 foreach ($holiday['properties'as $propId => $propVal{
  851.                     $this->_addStringPropertyForHoliday($holiday['internal-name']$locale
  852.                         $propId$propVal);
  853.                 }
  854.             }
  855.             
  856.         }
  857.         
  858.         if (Date_Holidays::errorsOccurred()) {
  859.             return Date_Holidays::getErrorStack();
  860.         }
  861.         
  862.         return true;
  863.     }
  864.     
  865.    /**
  866.     * Remove a holiday from internal storage
  867.     *
  868.     * This method should be used within driver classes to unset holidays that were inherited from
  869.     * parent-drivers
  870.     *
  871.     * @access   protected
  872.     * @param    $string     $internalName   internal name
  873.     * @return   boolean     true on success, otherwise a PEAR_Error object
  874.     * @throws   object PEAR_Error   DATE_HOLIDAYS_INVALID_INTERNAL_NAME
  875.     */
  876.     function _removeHoliday($internalName)
  877.     {
  878.         if (in_array($internalName$this->_internalNames)) {
  879.                 'Couldn\'t remove holiday with this internal name: ' $internalName);
  880.         }
  881.         
  882.         if (isset($this->_dates[$internalName])) {
  883.             unset($this->_dates[$internalName]);
  884.         }
  885.         $locales    array_keys($this->_titles);
  886.         foreach ($locales as $locale{
  887.             if (isset($this->_titles[$locale][$internalName])) {
  888.                 unset($this->_titles[$locale][$internalName]);
  889.             }
  890.         }
  891.         $index      array_search($internalName$this->_internalNames);
  892.         if (is_null($index)) {
  893.             unset($this->_internalNames[$index]);
  894.         }
  895.         return true;
  896.     }
  897.     
  898.    /**
  899.     * Finds the best internally available locale for the specified one
  900.     *
  901.     * @access   protected
  902.     * @param    string  $locale locale
  903.     * @return   string  best locale available
  904.     */
  905.     function _findBestLocale($locale)
  906.     {
  907.         /* exact locale is available */
  908.         if (in_array($locale$this->_availableLocales)) {
  909.             return $locale;
  910.         }
  911.         
  912.         /* first two letter are equal */
  913.         foreach ($this->_availableLocales as $aLocale{
  914.             if (strncasecmp($aLocale$locale2== 0{
  915.                 return $aLocale;
  916.             }
  917.         }
  918.         
  919.         /* no appropriate locale available, will use driver's internal locale */
  920.         return 'C';
  921.     }
  922.     
  923.    /**
  924.     * Returns date of a holiday
  925.     *
  926.     * @access   public
  927.     * @param    string  $internalName   internal name for holiday
  928.     * @return   object Date             date of the holiday as PEAR::Date object on success, otherwise a PEAR_Error object
  929.     * @throws   object PEAR_Error       DATE_HOLIDAYS_INVALID_INTERNAL_NAME, DATE_HOLIDAYS_DATE_UNAVAILABLE
  930.     */
  931.     function getHolidayDate($internalName)
  932.     {
  933.         if (in_array($internalName$this->_internalNames)) {
  934.             return Date_Holidays::raiseError(DATE_HOLIDAYS_INVALID_INTERNAL_NAME'Invalid internal name: ' $internalName);
  935.         }
  936.         
  937.         if (isset($this->_dates[$internalName])) {
  938.             return Date_Holidays::raiseError(DATE_HOLIDAYS_DATE_UNAVAILABLE'Date for holiday with internal name ' $internalName 
  939.                 ' is not available');
  940.         }
  941.         
  942.         return $this->_dates[$internalName];
  943.     }
  944.     
  945.    /**
  946.     * Returns dates of all holidays or those accepted by the applied filter.
  947.     *
  948.     * Structure of the returned array:
  949.     * <pre>
  950.     * array(
  951.     *   'internalNameFoo' => object of type date,
  952.     *   'internalNameBar' => object of type date
  953.     * )
  954.     * </pre>
  955.     *
  956.     * @access   public
  957.     * @param    Date_Holidays_Filter    filter-object (or an array !DEPRECATED!)
  958.     * @return   array with holidays' dates on success, otherwise a PEAR_Error object
  959.     * @throws   object PEAR_Error   DATE_HOLIDAYS_INVALID_INTERNAL_NAME
  960.     * @uses     getHolidayDate()
  961.     */
  962.     function getHolidayDates($filter = null)
  963.     {
  964.         if (is_null($filter)) {
  965.             $filter = new Date_Holidays_Filter_Blacklist(array());
  966.         elseif (is_array($filter)) {
  967.             $filter = new Date_Holidays_Filter_Whitelist($filter);
  968.         }
  969.         
  970.         $dates = array();
  971.         
  972.         foreach ($this->_internalNames as $internalName{
  973.             if ($filter->accept($internalName)) {
  974.                 $date $this->getHolidayDate($internalName);
  975.                 if (Date_Holidays::isError($date)) {
  976.                     return $date;
  977.                 }
  978.                 $dates[$internalName$this->getHolidayDate($internalName);
  979.             }
  980.         }
  981.         return $dates;
  982.     }
  983.     
  984.    /**
  985.     * Sets the driver's locale
  986.     *
  987.     * @access   public
  988.     * @param    string  $locale locale
  989.     */
  990.     function setLocale($locale)
  991.     {
  992.         $this->_locale  =   $locale;
  993.     }
  994.     
  995.    /**
  996.     * Sloppily compares two date objects (only year, month and day are compared).
  997.     * Does not take the date's timezone into account.
  998.     * 
  999.     * @static
  1000.     * @access private
  1001.     * @param Date $d1 a date object
  1002.     * @param Date $d2 another date object
  1003.     * @return int 0 if the dates are equal, -1 if d1 is before d2, 1 if d1 is after d2
  1004.     * 
  1005.     */
  1006.     function dateSloppyCompare($d1$d2
  1007.     {
  1008.         $d1->setTZ(new Date_TimeZone('UTC'));
  1009.         $d2->setTZ(new Date_TimeZone('UTC'));
  1010.         $days1 = Date_Calc::dateToDays($d1->day$d1->month$d1->year);
  1011.         $days2 = Date_Calc::dateToDays($d2->day$d2->month$d2->year);
  1012.         if ($days1 $days2return -1;
  1013.         if ($days1 $days2return 1;
  1014.         return 0;
  1015.     }
  1016. }
  1017. ?>

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