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

Source for file Composite.php

Documentation is available at Composite.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: Composite.php,v 1.8 2006/04/01 17:15:31 schst Exp $
  20.  
  21. /**
  22.  * driver not found
  23.  *
  24.  * @access  public
  25.  */
  26. define('DATE_HOLIDAYS_DRIVER_NOT_FOUND'100);
  27.  
  28. /**
  29.  * Composite driver - you can use this one to combine two or more drivers
  30.  *
  31.  * @category    Date
  32.  * @package     Date_Holidays
  33.  * @subpackage  Driver
  34.  * @version     $Id: Composite.php,v 1.8 2006/04/01 17:15:31 schst Exp $
  35.  * @author      Carsten Lucke <luckec@tool-garage.de>
  36.  */
  37. {
  38.    /**
  39.     * compound of drivers
  40.     *
  41.     * @access   private
  42.     * @var      array 
  43.     */
  44.     var $_drivers = array();
  45.  
  46.    /**
  47.     * Driver-ids ordered by importance
  48.     *
  49.     * @access   private
  50.     * @var      array 
  51.     */
  52.     var $_driverIds = array();
  53.  
  54.    /**
  55.     * Constructor
  56.     *
  57.     * Use the Date_Holidays::factory() method to construct an object of a certain driver
  58.     *
  59.     * @access   protected
  60.     */
  61.     function Date_Holidays_Driver_Composite()
  62.     {
  63.     }
  64.  
  65.    /**
  66.     * Build the internal arrays that contain data about the calculated holidays
  67.     *
  68.     * @access   private
  69.     * @return   boolean true on success, otherwise a PEAR_ErrorStack object
  70.     * @throws   object PEAR_ErrorStack
  71.     */
  72.     function _buildHolidays()
  73.     {
  74.     }
  75.  
  76.    /**
  77.     * Add a driver component
  78.     *
  79.     * @access   public
  80.     * @param    object Date_Holidays_Driver $driver driver-object
  81.     * @return   boolean true on success, false otherwise
  82.     */
  83.     function addDriver($driver)
  84.     {
  85.         if (is_a($driver'Date_Holidays_Driver')) {
  86.             return false;
  87.         }
  88.  
  89.         $id                     md5(serialize($driver));
  90.         $this->_drivers[$id]    $driver;
  91.         array_push($this->_driverIds$id);
  92.  
  93.         $this->_internalNames   = array_merge(
  94.                 $driver->getInternalHolidayNames()$this->_internalNames);
  95.         return true;
  96.     }
  97.  
  98.    /**
  99.     * Remove a driver component
  100.     *
  101.     * @access   public
  102.     * @param    object Date_Holidays_Driver $driver driver-object
  103.     * @return   boolean true on success, otherwise a PEAR_Error object
  104.     * @throws   object PEAR_Error   DATE_HOLIDAYS_DRIVER_NOT_FOUND
  105.     */
  106.     function removeDriver($driver)
  107.     {
  108.         if (is_a($driver'Date_Holidays_Driver')) {
  109.             return false;
  110.         }
  111.  
  112.         $id md5(serialize($driver));
  113.         // unset driver object
  114.         if (isset($this->_drivers[$id])) {
  115.             return Date_Holidays::raiseError(DATE_HOLIDAYS_DRIVER_NOT_FOUND'Driver not found');
  116.         }
  117.         unset($this->_drivers[$id]);
  118.  
  119.         // unset driver's prio
  120.         $index array_search($id$this->_driverIds);
  121.         unset($this->_driverIds[$index]);
  122.  
  123.         // rebuild the internal-names array
  124.         $this->_internalNames = array();
  125.         foreach ($this->_driverIds as $id{
  126.             $this->_internalNames =
  127.                     array_merge($this->_drivers[$id]->_internalNames$this->_internalNames);
  128.         }
  129.  
  130.         return true;
  131.     }
  132.  
  133.    /**
  134.     * Returns the specified holiday
  135.     *
  136.     * Return format:
  137.     * <pre>
  138.     *   array(
  139.     *       'title' =>  'Easter Sunday'
  140.     *       'date'  =>  '2004-04-11'
  141.     *   )
  142.     * </pre>
  143.     *
  144.     * @access   public
  145.     * @param    string  $internalName   internal name of the holiday
  146.     * @param    string  $locale         locale setting that shall be used by this method
  147.     * @return   object Date_Holidays_Holiday    holiday's information on success, otherwise a PEAR_Error object
  148.     * @throws   object PEAR_Error       DATE_HOLIDAYS_INVALID_INTERNAL_NAME
  149.     */
  150.     function getHoliday($internalName$locale = null)
  151.     {
  152.         foreach ($this->_driverIds as $id{
  153.             $holiday $this->_drivers[$id]->getHoliday($internalName$locale);
  154.             if (Date_Holidays::isError($holiday)) {
  155.                /**
  156.                 * lets skip this error, perhaps another driver knows this internal-name
  157.                 */
  158.                 continue;
  159.             }
  160.             return $holiday;
  161.         }
  162.  
  163.         return Date_Holidays::raiseError(DATE_HOLIDAYS_INVALID_INTERNAL_NAME'Invalid internal name: ' $internalName);
  164.     }
  165.  
  166.    /**
  167.     * Returns date of a holiday
  168.     *
  169.     * @access   public
  170.     * @param    string  $internalName   internal name for holiday
  171.     * @return   object Date             date of the holiday as PEAR::Date object on success, otherwise a PEAR_Error object
  172.     * @throws   object PEAR_Error       DATE_HOLIDAYS_INVALID_INTERNAL_NAME
  173.     */
  174.     function getHolidayDate($internalName)
  175.     {
  176.         foreach ($this->_driverIds as $id{
  177.             $date $this->_drivers[$id]->getHolidayDate($internalName);
  178.             if (Date_Holidays::isError($date)) {
  179.                /**
  180.                 * lets skip this error, perhaps another driver knows this internal-name
  181.                 */
  182.                 continue;
  183.             }
  184.             return $date;
  185.         }
  186.  
  187.         return Date_Holidays::raiseError(DATE_HOLIDAYS_INVALID_INTERNAL_NAME'Invalid internal name: ' $internalName);
  188.     }
  189.  
  190.    /**
  191.     * Returns dates of all holidays or those accepted by the specified filter.
  192.     *
  193.     * @access   public
  194.     * @param    Date_Holidays_Filter    filter-object (or an array !DEPRECATED!)
  195.     * @return   array   array with holidays' dates on success, otherwise a PEAR_ErrorStack object
  196.     * @throws   object PEAR_ErrorStack   DATE_HOLIDAYS_INVALID_INTERNAL_NAME
  197.     */
  198.     function getHolidayDates($filter = null)
  199.     {
  200.         if (is_null($filter)) {
  201.             $filter = new Date_Holidays_Filter_Blacklist(array());
  202.         elseif (is_array($filter)) {
  203.             $filter = new Date_Holidays_Filter_Whitelist($filter);
  204.         }
  205.  
  206.         $errorStack &Date_Holidays::getErrorStack();
  207.         $dates      = array();
  208.         $notFound   = array();
  209.  
  210.         foreach ($this->_internalNames as $internalName{
  211.             // check if the filter permits further processing
  212.             if ($filter->accept($internalName)) {
  213.                 continue;
  214.             }
  215.  
  216.             foreach ($this->_driverIds as $id{
  217.                 $date $this->_drivers[$id]->getHolidayDate($internalName);
  218.                 if (Date_Holidays::isError($date)) {
  219.                     if ($date->getCode(== DATE_HOLIDAYS_DATE_UNAVAILABLE{
  220.                        /**
  221.                         * this means a fatal error (would be the right place for sth. like an assert,
  222.                         * as this should normally never happen)
  223.                         */
  224.                         $errorStack->push(DATE_HOLIDAYS_DATE_UNAVAILABLE'error'array(),
  225.                             'No date found for holiday with internal name: ' $internalNamefalsedebug_backtrace());
  226.                         continue;
  227.                     }
  228.  
  229.                    /**
  230.                     * current driver doesn't have this internalName, trying next driver
  231.                     */
  232.                     array_push($notFound$internalName);
  233.                     continue;
  234.                 }
  235.                /**
  236.                 * internal name found in highest priorized driver, stepping to next internal name
  237.                 * checks if internal name is existent in $notFound array and unsets this entry as it has been found now
  238.                 */
  239.                 $notFound array_unique($notFound);
  240.                 if (in_array($internalName$notFound)) {
  241.                     unset($notFound[array_search($internalName$notFound)]);
  242.                 }
  243.                 $dates[$internalName]   $date;
  244.                 continue 2;
  245.             }
  246.         }
  247.  
  248.         if (empty($notFound)) {
  249.             foreach ($notFound as $internalName{
  250.                 $errorStack->push(DATE_HOLIDAYS_INVALID_INTERNAL_NAME'error'array(),
  251.                     'Invalid internal name: ' $internalNamefalsedebug_backtrace());
  252.             }
  253.         }
  254.  
  255.         if ($errorStack->hasErrors(&& empty($notFound)) {
  256.             return $errorStack;
  257.         }
  258.         return $dates;
  259.     }
  260.  
  261.    /**
  262.     * Returns the title of the holiday, if any was found, matching the specified date.
  263.     *
  264.     * Normally the method will return the title/data for the first holiday matching the date.
  265.     * If you want the mthod to continue searching holidays for the specified date, set the 4th param to true
  266.     * If multiple holidays match your date, the return value will be an array of the titles/data.
  267.     * <pre>
  268.     * array(
  269.     *   array(
  270.     *       'title' => 'New Year',
  271.     *       'date'  => Object of type Date
  272.     *   ),
  273.     *   array(
  274.     *       'title' => 'Circumcision of Jesus',
  275.     *       'date'  => Object of type Date
  276.     *   )
  277.     * )
  278.     * </pre>
  279.     *
  280.     * @access   public
  281.     * @param    mixed   $date       date (timestamp | string | PEAR::Date object)
  282.     * @param    string  $locale     locale setting that shall be used by this method
  283.     * @param    boolean $multiple 
  284.     * @return   object  object of type Date_Holidays_Holiday on success (numeric array of those on multiple search); if no holiday was found, matching this date, null is returned
  285.     * @uses     getHoliday()
  286.     * @uses     getHolidayTitle()
  287.     * @see      getHoliday()
  288.     ***/
  289.     function getHolidayForDate($date$locale = null$multiple = false)
  290.     {
  291.         $holidays = array();
  292.         foreach ($this->_driverIds as $id{
  293.             $holiday $this->_drivers[$id]->getHolidayForDate($date$locale$multiple);
  294.             if (is_null($holiday)) {
  295.                /**
  296.                 * No holiday found for this date in the current driver, trying next one
  297.                 */
  298.                 continue;
  299.             }
  300.  
  301.             if (is_array($holiday)) {
  302.                 for ($i = 0; $i count($holiday); ++$i{
  303.                     $holidays[$holiday[$i];
  304.                 }
  305.             else {
  306.                 $holidays[]     $holiday;
  307.             }
  308.  
  309.             if ($multiple{
  310.                 return $holiday;
  311.             }
  312.         }
  313.  
  314.         if (empty($holidays)) {
  315.             return null;
  316.         }
  317.         return $holidays;
  318.     }
  319.  
  320.    /**
  321.     * Returns all holidays that were found
  322.     *
  323.     * Return format:
  324.     * <pre>
  325.     *   array(
  326.     *       'easter' =>  array(
  327.     *           'title' =>  'Easter Sunday'
  328.     *           'date'  =>  '2004-04-11'
  329.     *       ),
  330.     *       'eastermonday'  =>  array(
  331.     *           'title' =>  'Easter Monday'
  332.     *           'date'  =>  '2004-04-12'
  333.     *       ),
  334.     *       ...
  335.     *   )
  336.     * </pre>
  337.     *
  338.     * @access   public
  339.     * @param    Date_Holidays_Filter    filter-object (or an array !DEPRECATED!)
  340.     * @return   array   numeric array containing objects of Date_Holidays_Holiday on success, otherwise a PEAR_ErrorStack object
  341.     * @throws   object PEAR_ErrorStack   DATE_HOLIDAYS_INVALID_INTERNAL_NAME
  342.     */
  343.     function getHolidays($filter = null)
  344.     {
  345.         if (is_null($filter)) {
  346.             $filter = new Date_Holidays_Filter_Blacklist(array());
  347.         elseif (is_array($filter)) {
  348.             $filter = new Date_Holidays_Filter_Whitelist($filter);
  349.         }
  350.  
  351.         $errorStack &Date_Holidays::getErrorStack();
  352.         $holidays   = array();
  353.         $notFound   = array();
  354.  
  355.         foreach ($this->_internalNames as $internalName{
  356.             // check if the filter permits further processing
  357.             if ($filter->accept($internalName)) {
  358.                 continue;
  359.             }
  360.  
  361.             foreach ($this->_driverIds as $id{
  362.                 $holiday $this->_drivers[$id]->getHoliday($internalName);
  363.                 if (Date_Holidays::isError($holiday)) {
  364.                    /**
  365.                     * current driver doesn't have this internalName, trying next driver
  366.                     */
  367.                     array_push($notFound$internalName);
  368.                     continue;
  369.                 }
  370.                /**
  371.                 * internal name found in highest priorized driver, stepping to next internal name
  372.                 * checks if internal name is existent in $notFound array and unsets this entry as it has been found now
  373.                 */
  374.                 $notFound array_unique($notFound);
  375.                 if (in_array($internalName$notFound)) {
  376.                     unset($notFound[array_search($internalName$notFound)]);
  377.                 }
  378.                 $holidays[$internalName$holiday;
  379.                 continue 2;
  380.             }
  381.         }
  382.  
  383.         if (empty($notFound)) {
  384.             foreach ($notFound as $internalName{
  385.                 $errorStack->push(DATE_HOLIDAYS_INVALID_INTERNAL_NAME'error'array(),
  386.                     'Invalid internal name: ' $internalNamefalsedebug_backtrace());
  387.             }
  388.         }
  389.  
  390.         if ($errorStack->hasErrors(&& empty($notFound)) {
  391.             return $errorStack;
  392.         }
  393.         return $holidays;
  394.     }
  395.  
  396.    /**
  397.     * Returns localized title for a holiday
  398.     *
  399.     * @access   public
  400.     * @param    string  $internalName   internal name for holiday
  401.     * @param    string  $locale         locale setting that shall be used by this method
  402.     * @return   string  title on success, otherwise a PEAR_Error object
  403.     * @throws   object PEAR_Error   DATE_HOLIDAYS_INVALID_INTERNAL_NAME, DATE_HOLIDAYS_TITLE_UNAVAILABLE
  404.     */
  405.     function getHolidayTitle($internalName$locale = null)
  406.     {
  407.         foreach ($this->_driverIds as $id{
  408.             $title $this->_drivers[$id]->getHolidayTitle($internalName$locale);
  409.             if (Date_Holidays::isError($title)) {
  410.                /**
  411.                 * lets skip this error, perhaps another driver knows this internal-name
  412.                 */
  413.                 continue;
  414.             }
  415.             return $title;
  416.         }
  417.  
  418.         return Date_Holidays::raiseError(DATE_HOLIDAYS_INVALID_INTERNAL_NAME'Invalid internal name: ' $internalName);
  419.     }
  420.  
  421.    /**
  422.     * Returns localized titles of all holidays or those specififed in $restrict array
  423.     *
  424.     * @access   public
  425.     * @param    Date_Holidays_Filter    filter-object (or an array !DEPRECATED!)
  426.     * @param    string  $locale     locale setting that shall be used by this method
  427.     * @return   array   array with localized holiday titles on success, otherwise a PEAR_Error object
  428.     * @throws   object PEAR_Error   DATE_HOLIDAYS_INVALID_INTERNAL_NAME
  429.     */
  430.     function getHolidayTitles($filter = null$locale = null)
  431.     {
  432.         if (is_null($filter)) {
  433.             $filter = new Date_Holidays_Filter_Blacklist(array());
  434.         elseif (is_array($filter)) {
  435.             $filter = new Date_Holidays_Filter_Whitelist($filter);
  436.         }
  437.  
  438.         $errorStack &Date_Holidays::getErrorStack();
  439.         $titles     = array();
  440.         $notFound   = array();
  441.  
  442.         foreach ($this->_internalNames as $internalName{
  443.             // check if the filter permits further processing
  444.             if ($filter->accept($internalName)) {
  445.                 continue;
  446.             }
  447.  
  448.             foreach ($this->_driverIds as $id{
  449.                 $title $this->_drivers[$id]->getHolidayTitle($internalName$locale);
  450.                 if (Date_Holidays::isError($title)) {
  451.                    /**
  452.                     * current driver doesn't have this internalName, trying next driver
  453.                     */
  454.                     array_push($notFound$internalName);
  455.                     continue;
  456.                 }
  457.                /**
  458.                 * internal name found in highest priorized driver, stepping to next internal name
  459.                 * checks if internal name is existent in $notFound array and unsets this entry as it has been found now
  460.                 */
  461.                 $notFound array_unique($notFound);
  462.                 if (in_array($internalName$notFound)) {
  463.                     unset($notFound[array_search($internalName$notFound)]);
  464.                 }
  465.                 $titles[$internalName]   $title;
  466.                 continue 2;
  467.             }
  468.         }
  469.  
  470.         if (empty($notFound)) {
  471.             foreach ($notFound as $internalName{
  472.                 $errorStack->push(DATE_HOLIDAYS_INVALID_INTERNAL_NAME'error'array(),
  473.                     'Invalid internal name: ' $internalNamefalsedebug_backtrace());
  474.             }
  475.         }
  476.  
  477.         if ($errorStack->hasErrors(&& empty($notFound)) {
  478.             return $errorStack;
  479.         }
  480.         return $titles;
  481.     }
  482.  
  483.    /**
  484.     * Using this method doesn't affect anything. If you have been able to add your driver to this compound,
  485.     * you should also be able to directly execute this action.
  486.     * This method is only available to keep abstraction working.
  487.     *
  488.     * @access   public
  489.     */
  490.     function getYear()
  491.     {
  492.     }
  493.  
  494.    /**
  495.     * This (re)sets the year of every driver-object in the compound.
  496.     *
  497.     * Note that this will cause every attached driver to recalculate the holidays!
  498.     *
  499.     * @access   public
  500.     * @param    int     year
  501.     * @return   boolean true on success, otherwise a PEAR_ErrorStack object
  502.     * @throws   object PEAR_ErrorStack
  503.     */
  504.     function setYear($year)
  505.     {
  506.         $errors = false;
  507.  
  508.         foreach ($this->_driverIds as $id{
  509.             if ($this->_drivers[$id]->setYear($year!= true{
  510.                 $errors = true;
  511.             }
  512.         }
  513.  
  514.         if ($errors{
  515.             return Date_Holidays::getErrorStack();
  516.         }
  517.         return true;
  518.     }
  519.  
  520.    /**
  521.     * Determines whether a date represents a holiday or not.
  522.     *
  523.     * The method searches all added drivers for this date, to determine whether it's a holiday.
  524.     *
  525.     * @access   public
  526.     * @param    mixed   $date   date (can be a timestamp, string or PEAR::Date object)
  527.     * @param    Date_Holidays_Filter $filter    filter-object (or an array !DEPRECATED!)
  528.     * @return   boolean true if date represents a holiday, otherwise false
  529.     */
  530.     function isHoliday($date$filter = null)
  531.     {
  532.         foreach ($this->_driverIds as $id{
  533.             if ($this->_drivers[$id]->isHoliday($date$filter)) {
  534.                 return true;
  535.             }
  536.             continue;
  537.         }
  538.  
  539.         return false;
  540.     }
  541.  
  542.    /**
  543.     * Using this method doesn't affect anything. If you have bben able to add your driver to this compound,
  544.     * you should also be able to directly execute this action.
  545.     * This method is only available to keep abstraction working.
  546.     *
  547.     * @access   public
  548.     */
  549.     function setLocale($locale)
  550.     {
  551.     }
  552. }
  553. ?>

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