Subversion Repositories Applications.papyrus

Rev

Rev 1688 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
493 ddelon 1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4: */
3
//
4
// +----------------------------------------------------------------------+
5
// | PHP Version 4                                                        |
6
// +----------------------------------------------------------------------+
7
// | Copyright (c) 1997-2002 The PHP Group                                |
8
// +----------------------------------------------------------------------+
9
// | This source file is subject to version 2.02 of the PHP license,      |
10
// | that is bundled with this package in the file LICENSE, and is        |
11
// | available at through the world-wide-web at                           |
12
// | http://www.php.net/license/3_0.txt.                                  |
13
// | If you did not receive a copy of the PHP license and are unable to   |
14
// | obtain it through the world-wide-web, please send a note to          |
15
// | license@php.net so we can mail you a copy immediately.               |
16
// +----------------------------------------------------------------------+
17
// | Authors: Harry Fuecks <hfuecks@phppatterns.com>                      |
18
// |          Lorenzo Alberton <l dot alberton at quipo dot it>           |
19
// +----------------------------------------------------------------------+
20
//
21
// $Id: Factory.php,v 1.1 2005-09-30 14:58:00 ddelon Exp $
22
//
23
/**
24
 * @package Calendar
25
 * @version $Id: Factory.php,v 1.1 2005-09-30 14:58:00 ddelon Exp $
26
 */
27
 
28
/**
29
 * Allows Calendar include path to be redefined
30
 * @ignore
31
 */
32
if (!defined('CALENDAR_ROOT')) {
33
    define('CALENDAR_ROOT', 'Calendar'.DIRECTORY_SEPARATOR);
34
}
35
 
36
/**
37
 * Load Calendar base class
38
 */
39
require_once CALENDAR_ROOT.'Calendar.php';
40
 
41
/**
42
 * Constant for the first day of the week (integer e.g. 0-6)
43
 */
44
if ( !defined ('CALENDAR_FIRST_DAY_OF_WEEK') ) {
45
    define ('CALENDAR_FIRST_DAY_OF_WEEK',1);
46
}
47
 
48
/**
49
 * Contains a factory method to return a Singleton instance of a class
50
 * implementing the Calendar_Engine_Interface.<br>
51
 * For Month objects, to control type of month returned, use CALENDAR_MONTH_STATE
52
 * constact e.g.;
53
 * <code>
54
 * require_once 'Calendar/Factory.php';
55
 * define ('CALENDAR_MONTH_STATE',CALENDAR_USE_MONTH_WEEKDAYS); // Use Calendar_Month_Weekdays
56
 * // define ('CALENDAR_MONTH_STATE',CALENDAR_USE_MONTH_WEEKS); // Use Calendar_Month_Weeks
57
 * // define ('CALENDAR_MONTH_STATE',CALENDAR_USE_MONTH); // Use Calendar_Month
58
 * </code>
59
 * It defaults to building Calendar_Month objects.<br>
60
 * Use the constract CALENDAR_FIRST_DAY_OF_WEEK to control the first day of the week
61
 * for Month or Week objects (e.g. 0 = Sunday, 6 = Saturday)
62
 * @package Calendar
63
 * @access protected
64
 */
65
class Calendar_Factory
66
{
67
    /**
68
     * Creates a calendar object given the type and units
69
     * @param string class of calendar object to create
70
     * @param int year
71
     * @param int month
72
     * @param int day
73
     * @param int hour
74
     * @param int minute
75
     * @param int second
76
     * @return object subclass of Calendar
77
     * @access public
78
     * @static
79
     */
80
    function create($type, $y = 2000, $m = 1, $d = 1, $h = 0, $i = 0, $s = 0)
81
    {
82
        switch ( $type ) {
83
            case 'Day':
84
                require_once CALENDAR_ROOT.'Day.php';
85
                return new Calendar_Day($y,$m,$d);
86
            break;
87
            case 'Month':
88
                // Set default state for which month type to build
89
                if (!defined('CALENDAR_MONTH_STATE')) {
90
                    define('CALENDAR_MONTH_STATE', CALENDAR_USE_MONTH);
91
                }
92
                switch (CALENDAR_MONTH_STATE) {
93
                    case CALENDAR_USE_MONTH_WEEKDAYS:
94
                        require_once CALENDAR_ROOT.'Month'.DIRECTORY_SEPARATOR.'Weekdays.php';
95
                        $class = 'Calendar_Month_Weekdays';
96
                        break;
97
                    case CALENDAR_USE_MONTH_WEEKS:
98
                        require_once CALENDAR_ROOT.'Month'.DIRECTORY_SEPARATOR.'Weeks.php';
99
                        $class = 'Calendar_Month_Weeks';
100
                        break;
101
                    case CALENDAR_USE_MONTH:
102
                    default:
103
                        require_once CALENDAR_ROOT.'Month.php';
104
                        $class = 'Calendar_Month';
105
                        break;
106
                }
107
                return new $class($y,$m,CALENDAR_FIRST_DAY_OF_WEEK);
108
            break;
109
            case 'Week':
110
                require_once CALENDAR_ROOT.'Week.php';
111
                return new Calendar_Week($y,$m,$d,CALENDAR_FIRST_DAY_OF_WEEK);
112
            break;
113
            case 'Hour':
114
                require_once CALENDAR_ROOT.'Hour.php';
115
                return new Calendar_Hour($y,$m,$d,$h);
116
            break;
117
            case 'Minute':
118
                require_once CALENDAR_ROOT.'Minute.php';
119
                return new Calendar_Minute($y,$m,$d,$h,$i);
120
            break;
121
            case 'Second':
122
                require_once CALENDAR_ROOT.'Second.php';
123
                return new Calendar_Second($y,$m,$d,$h,$i,$s);
124
            break;
125
            case 'Year':
126
                require_once CALENDAR_ROOT.'Year.php';
127
                return new Calendar_Year($y);
128
            break;
129
            default:
130
                require_once 'PEAR.php';
131
                PEAR::raiseError(
132
                    'Calendar_Factory::create() unrecognised type: '.$type, null, PEAR_ERROR_TRIGGER,
133
                    E_USER_NOTICE, 'Calendar_Factory::create()');
134
                return false;
135
            break;
136
        }
137
    }
138
    /**
139
     * Creates an instance of a calendar object, given a type and timestamp
140
     * @param string type of object to create
141
     * @param mixed timestamp (depending on Calendar engine being used)
142
     * @return object subclass of Calendar
143
     * @access public
144
     * @static
145
     */
146
    function & createByTimestamp($type, $stamp)
147
    {
148
        $cE = & Calendar_Engine_Factory::getEngine();
149
        $y = $cE->stampToYear($stamp);
150
        $m = $cE->stampToMonth($stamp);
151
        $d = $cE->stampToDay($stamp);
152
        $h = $cE->stampToHour($stamp);
153
        $i = $cE->stampToMinute($stamp);
154
        $s = $cE->stampToSecond($stamp);
155
        return Calendar_Factory::create($type, $y, $m, $d, $h, $i, $s);
156
    }
157
}
158
?>