Source for file Driver.php
Documentation is available at Driver.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Carsten Lucke <luckec@tool-garage.de> |
// +----------------------------------------------------------------------+
// $Id: Driver.php,v 1.22 2004/08/09 18:03:49 luckec Exp $s
require_once 'PEAR/ErrorStack.php';
define('DATE_HOLIDAYS_INVALID_INTERNAL_NAME', 51 );
* title for a holiday is not available
define('DATE_HOLIDAYS_TITLE_UNAVAILABLE', 52 );
* date could not be converted into a PEAR::Date object
* date was neither a timestamp nor a string
* @deprecated will certainly be removed
define('DATE_HOLIDAYS_INVALID_DATE', 53 );
* string that represents a date has wrong format
* format must be YYYY-MM-DD
* @deprecated will certainly be removed
define('DATE_HOLIDAYS_INVALID_DATE_FORMAT', 54 );
* date for a holiday is not available
define('DATE_HOLIDAYS_DATE_UNAVAILABLE', 55 );
* language-file doesn't exist
define('DATE_HOLIDAYS_LANGUAGEFILE_NOT_FOUND', 56 );
* class that helps you to locate holidays for a year
* @version $Id: Driver.php,v 1.22 2004/08/09 18:03:49 luckec Exp $
* @author Carsten Lucke <luckec@tool-garage.de>
* locale setting for output
* locales for which translations of holiday titles are available
var $_availableLocales = array ('C');
* internal names for the available holidays
* dates of the available holidays
* localized names of the available holidays
* Use the Date_Holidays::factory() method to construct an object of a certain driver
* Sets the driver's current year
* Calling this method forces the object to rebuild the holidays
* @return boolean true on success, otherwise a PEAR_ErrorStack object
* @throws object PEAR_ErrorStack
* Returns the driver's current year
* @return int current year
* Build the internal arrays that contain data about the calculated holidays
* @return boolean true on success, otherwise a PEAR_ErrorStack object
* @throws object PEAR_ErrorStack
* @param object Date_Holidays_Driver $driver driver-object
* Remove a driver component
* @param object Date_Holidays_Driver $driver driver-object
* @return boolean true on success, otherwise a PEAR_Error object
* @throws object PEAR_Error DATE_HOLIDAYS_DRIVER_NOT_FOUND
* Returns the internal names of holidays that were calculated
* Returns localized titles of all holidays or those specififed in $restrict array
* @param array $restrict internal names of desired holidays
* @param string $locale locale setting that shall be used by this method
* @return array with localized holiday titles on success, otherwise a PEAR_Error object
* @throws object PEAR_Error DATE_HOLIDAYS_INVALID_INTERNAL_NAME
* @uses getHolidayTitle()
foreach ($restrict as $internalName) {
$titles[$internalName] = $title;
* Returns localized title for a holiday
* @param string $internalName internal name for holiday
* @param string $locale locale setting that shall be used by this method
* @return string title on success, otherwise a PEAR_Error object
* @throws object PEAR_Error DATE_HOLIDAYS_INVALID_INTERNAL_NAME, DATE_HOLIDAYS_TITLE_UNAVAILABLE
if (! isset ($this->_titles[$locale][$internalName])) {
') for the holiday was correct but no localized title could be found');
return isset ($this->_titles[$locale][$internalName]) ?
$this->_titles[$locale][$internalName] : $this->_titles['C'][$internalName];
* Returns all holidays that the driver knows.
* You can limit the holidays by setting the $restrict array, then only those
* will be returned, whose internal name occurrs in this array.
* 'easter' => object of type Date_Holidays_Holiday,
* 'eastermonday' => object of type Date_Holidays_Holiday,
* @param array $restrict internal names of desired holidays
* @return array numeric array containing objects of Date_Holidays_Holiday on success, otherwise a PEAR_Error object
* @throws object PEAR_Error DATE_HOLIDAYS_INVALID_INTERNAL_NAME
foreach ($restrict as $internalName) {
$holidays[$internalName] = &$this->getHoliday($internalName);
* Returns the specified holiday
* 'title' => 'Easter Sunday'
* @param string $internalName internal name of the holiday
* @param string $locale locale setting that shall be used by this method
* @return object Date_Holidays_Holiday holiday's information on success, otherwise a PEAR_Error object
* @throws object PEAR_Error DATE_HOLIDAYS_INVALID_INTERNAL_NAME
* @uses getHolidayTitle()
function getHoliday($internalName, $locale = null )
'Invalid internal name: ' . $internalName);
* Determines whether a date represents a holiday or not
* @param mixed $date date (can be a timestamp, string or PEAR::Date object)
* @return boolean true if date represents a holiday, otherwise false
* @throws object PEAR_Error DATE_HOLIDAYS_INVALID_DATE, DATE_HOLIDAYS_INVALID_DATE_FORMAT
if (! is_a($date, 'Date')) {
$date = & $this->_convertDate ($date);
if ($date->compare ($date, $holidayDate) != 0 ) {
* Returns the title of the holiday, if any was found, matching the specified date.
* Normally the method will return the title/data for the first holiday matching the date.
* If you want the mthod to continue searching holidays for the specified date, set the 4th param to true
* If multiple holidays match your date, the return value will be an array of the titles/data.
* 'date' => Object of type Date
* 'title' => 'Circumcision of Jesus',
* 'date' => Object of type Date
* @param mixed $date date (timestamp | string | PEAR::Date object)
* @param string $locale locale setting that shall be used by this method
* @param boolean $multiple
* @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
* @throws object PEAR_Error DATE_HOLIDAYS_INVALID_DATE, DATE_HOLIDAYS_INVALID_DATE_FORMAT
* @uses getHolidayTitle()
// array that collects data, if multiple searching is done
if (! is_a($date, 'Date')) {
$date = &$this->_convertDate ($date);
foreach ($this->_dates as $internalName => $holidayDate) {
if ($date->compare ($date, $holidayDate) != 0 ) {
$result = &$this->getHoliday($internalName, $locale);
* Converts timestamp or date-string into da PEAR::Date object
* @param mixed $date date
* @return object PEAR_Date
* @throws object PEAR_Error DATE_HOLIDAYS_INVALID_DATE, DATE_HOLIDAYS_INVALID_DATE_FORMAT
function _convertDate ($date)
if (! preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}/', $date)) {
'Date-string has wrong format (must be YYYY-MM-DD)');
'The date you specified is invalid');
* Adds all holidays in the array to the driver's internal list of holidays.
* 'newYearsDay' => array(
* 'title' => 'New Year\'s Day',
* 'translations' => array(
* 'en_EN' => 'New Year\'s Day'
* 'valentinesDay' => array(
* @param array $holidays static holidays' data
* @uses _addTranslationForHoliday()
foreach ($holidays as $internalName => $holiday) {
// add the holiday's basic data
$this->_addHoliday($internalName, $this->_year . '-' . $holiday['date'], $holiday['title']);
* Adds a holiday to the driver's holidays
* @param string $internalName internal name - must not contain characters that aren\'t allowes as variable-names
* @param mixed $date date (timestamp | string | PEAR::Date object)
* @param string $title holiday title
if (! is_a($date, 'Date')) {
$date = & new Date ($date);
$this->_dates[$internalName] = &$date;
$this->_titles['C'][$internalName] = $title;
* Add a localized translation for a holiday's title
* @param string $internalName internal name of an existing holiday
* @param string $locale locale setting that shall be used by this method
* @param string $title title
* @return true on success, otherwise a PEAR_Error object
* @throws object PEAR_Error DATE_HOLIDAYS_INVALID_INTERNAL_NAME
'Couldn\'t add translation (' . $locale . ') for holiday with this internal name: ' . $internalName);
if (! in_array($locale, $this->_availableLocales)) {
$this->_titles[$locale][$internalName] = $title;
* Add a translation-file's content
* The translation-file's content will be parsed and translations for
* holidays will be made available with the specified locale.
* @param string $file filename of the language file
* @param string $locale locale-code of the translation
* @return boolean true on success, otherwise a PEAR_ErrorStack object
* @throws object PEAR_Errorstack
foreach ($content as $internalName => $translation) {
* Remove a holiday from internal storage
* This method should be used within driver classes to unset holidays that were inherited from
* @param $string $internalName internal name
* @return boolean true on success, otherwise a PEAR_Error object
* @throws object PEAR_Error DATE_HOLIDAYS_INVALID_INTERNAL_NAME
'Couldn\'t remove holiday with this internal name: ' . $internalName);
if (isset ($this->_dates[$internalName])) {
unset ($this->_dates[$internalName]);
foreach ($locales as $locale) {
if (isset ($this->_titles[$locale][$internalName])) {
unset ($this->_titles[$locale][$internalName]);
* Finds the best internally available locale for the specified one
* @param string $locale locale
* @return string best locale available
/* exact locale is available */
if (in_array($locale, $this->_availableLocales)) {
/* first two letter are equal */
foreach ($this->_availableLocales as $aLocale) {
/* no appropriate locale available, will use driver's internal locale */
* Returns date of a holiday
* @param string $internalName internal name for holiday
* @return object Date date of the holiday as PEAR::Date object on success, otherwise a PEAR_Error object
* @throws object PEAR_Error DATE_HOLIDAYS_INVALID_INTERNAL_NAME, DATE_HOLIDAYS_DATE_UNAVAILABLE
if (! isset ($this->_dates[$internalName])) {
return $this->_dates[$internalName];
* Returns dates of all holidays or those specififed in $restrict array
* Structure of the returned array:
* 'internalNameFoo' => object of type date,
* 'internalNameBar' => object of type date
* @param array $restrict internal names of desired holidays
* @return array with holidays' dates on success, otherwise a PEAR_Error object
* @throws object PEAR_Error DATE_HOLIDAYS_INVALID_INTERNAL_NAME
foreach ($restrict as $internalName) {
* Sets the driver's locale
* @param string $locale locale
Documentation generated on Mon, 11 Mar 2019 13:54:58 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|