Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4: */
3
 
4
/**
5
 * Contains the Calendar_Factory class
6
 *
7
 * PHP versions 4 and 5
8
 *
9
 * LICENSE: Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 * 3. The name of the author may not be used to endorse or promote products
17
 *    derived from this software without specific prior written permission.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
20
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22
 * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
23
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * @category  Date and Time
31
 * @package   Calendar
32
 * @author    Harry Fuecks <hfuecks@phppatterns.com>
33
 * @author    Lorenzo Alberton <l.alberton@quipo.it>
34
 * @copyright 2003-2007 Harry Fuecks, Lorenzo Alberton
35
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
36
 * @version   CVS: $Id: Factory.php 246404 2007-11-18 21:46:43Z quipo $
37
 * @link      http://pear.php.net/package/Calendar
38
 */
39
 
40
/**
41
 * Allows Calendar include path to be redefined
42
 * @ignore
43
 */
44
if (!defined('CALENDAR_ROOT')) {
45
    define('CALENDAR_ROOT', 'Calendar'.DIRECTORY_SEPARATOR);
46
}
47
 
48
/**
49
 * Load Calendar base class
50
 */
51
require_once CALENDAR_ROOT.'Calendar.php';
52
 
53
/**
54
 * Contains a factory method to return a Singleton instance of a class
55
 * implementing the Calendar_Engine_Interface.<br>
56
 * For Month objects, to control type of month returned, use CALENDAR_MONTH_STATE
57
 * constact e.g.;
58
 * <code>
59
 * require_once 'Calendar/Factory.php';
60
 * define ('CALENDAR_MONTH_STATE',CALENDAR_USE_MONTH_WEEKDAYS); // Use Calendar_Month_Weekdays
61
 * // define ('CALENDAR_MONTH_STATE',CALENDAR_USE_MONTH_WEEKS); // Use Calendar_Month_Weeks
62
 * // define ('CALENDAR_MONTH_STATE',CALENDAR_USE_MONTH); // Use Calendar_Month
63
 * </code>
64
 * It defaults to building Calendar_Month objects.<br>
65
 * Use the constract CALENDAR_FIRST_DAY_OF_WEEK to control the first day of the week
66
 * for Month or Week objects (e.g. 0 = Sunday, 6 = Saturday)
67
 *
68
 * @category  Date and Time
69
 * @package   Calendar
70
 * @author    Harry Fuecks <hfuecks@phppatterns.com>
71
 * @author    Lorenzo Alberton <l.alberton@quipo.it>
72
 * @copyright 2003-2007 Harry Fuecks, Lorenzo Alberton
73
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
74
 * @link      http://pear.php.net/package/Calendar
75
 * @access protected
76
 */
77
class Calendar_Factory
78
{
79
    /**
80
     * Creates a calendar object given the type and units
81
     *
82
     * @param string $type class of calendar object to create
83
     * @param int    $y    year
84
     * @param int    $m    month
85
     * @param int    $d    day
86
     * @param int    $h    hour
87
     * @param int    $i    minute
88
     * @param int    $s    second
89
     *
90
     * @return object subclass of Calendar
91
     * @access public
92
     * @static
93
     */
94
    function create($type, $y = 2000, $m = 1, $d = 1, $h = 0, $i = 0, $s = 0)
95
    {
96
        $firstDay = defined('CALENDAR_FIRST_DAY_OF_WEEK') ? CALENDAR_FIRST_DAY_OF_WEEK : 1;
97
        switch ($type) {
98
        case 'Day':
99
            include_once CALENDAR_ROOT.'Day.php';
100
            return new Calendar_Day($y, $m, $d);
101
        case 'Month':
102
            // Set default state for which month type to build
103
            if (!defined('CALENDAR_MONTH_STATE')) {
104
                define('CALENDAR_MONTH_STATE', CALENDAR_USE_MONTH);
105
            }
106
            switch (CALENDAR_MONTH_STATE) {
107
            case CALENDAR_USE_MONTH_WEEKDAYS:
108
                include_once CALENDAR_ROOT.'Month/Weekdays.php';
109
                $class = 'Calendar_Month_Weekdays';
110
                break;
111
            case CALENDAR_USE_MONTH_WEEKS:
112
                include_once CALENDAR_ROOT.'Month/Weeks.php';
113
                $class = 'Calendar_Month_Weeks';
114
                break;
115
            case CALENDAR_USE_MONTH:
116
            default:
117
                include_once CALENDAR_ROOT.'Month.php';
118
                $class = 'Calendar_Month';
119
                break;
120
            }
121
            return new $class($y, $m, $firstDay);
122
        case 'Week':
123
            include_once CALENDAR_ROOT.'Week.php';
124
            return new Calendar_Week($y, $m, $d, $firstDay);
125
        case 'Hour':
126
            include_once CALENDAR_ROOT.'Hour.php';
127
            return new Calendar_Hour($y, $m, $d, $h);
128
        case 'Minute':
129
            include_once CALENDAR_ROOT.'Minute.php';
130
            return new Calendar_Minute($y, $m, $d, $h, $i);
131
        case 'Second':
132
            include_once CALENDAR_ROOT.'Second.php';
133
            return new Calendar_Second($y, $m, $d, $h, $i, $s);
134
        case 'Year':
135
            include_once CALENDAR_ROOT.'Year.php';
136
            return new Calendar_Year($y);
137
        default:
138
            include_once 'PEAR.php';
139
            PEAR::raiseError('Calendar_Factory::create() unrecognised type: '.$type,
140
                null, PEAR_ERROR_TRIGGER, E_USER_NOTICE, 'Calendar_Factory::create()');
141
            return false;
142
        }
143
    }
144
 
145
    /**
146
     * Creates an instance of a calendar object, given a type and timestamp
147
     *
148
     * @param string $type  type of object to create
149
     * @param mixed  $stamp timestamp (depending on Calendar engine being used)
150
     *
151
     * @return object subclass of Calendar
152
     * @access public
153
     * @static
154
     */
155
    function & createByTimestamp($type, $stamp)
156
    {
157
        $cE  = & Calendar_Engine_Factory::getEngine();
158
        $y   = $cE->stampToYear($stamp);
159
        $m   = $cE->stampToMonth($stamp);
160
        $d   = $cE->stampToDay($stamp);
161
        $h   = $cE->stampToHour($stamp);
162
        $i   = $cE->stampToMinute($stamp);
163
        $s   = $cE->stampToSecond($stamp);
164
        $cal = Calendar_Factory::create($type, $y, $m, $d, $h, $i, $s);
165
        return $cal;
166
    }
167
}
168
?>