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

Documentation generated on Mon, 11 Mar 2019 13:54:58 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.