|
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 <[email protected]> |
|
33
|
|
|
* @author Lorenzo Alberton <[email protected]> |
|
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$ |
|
37
|
|
|
* @link http://pear.php.net/package/Calendar |
|
38
|
|
|
*/ |
|
39
|
|
|
namespace PEAR\Calendar; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Contains a factory method to return a Singleton instance of a class |
|
43
|
|
|
* implementing the Calendar_Engine_Interface.<br> |
|
44
|
|
|
* For Month objects, to control type of month returned, use CALENDAR_MONTH_STATE |
|
45
|
|
|
* constact e.g.; |
|
46
|
|
|
* <code> |
|
47
|
|
|
* require_once 'Calendar/Factory.php'; |
|
48
|
|
|
* define ('CALENDAR_MONTH_STATE',CALENDAR_USE_MONTH_WEEKDAYS); // Use Calendar_Month_Weekdays |
|
49
|
|
|
* // define ('CALENDAR_MONTH_STATE',CALENDAR_USE_MONTH_WEEKS); // Use Calendar_Month_Weeks |
|
50
|
|
|
* // define ('CALENDAR_MONTH_STATE',CALENDAR_USE_MONTH); // Use Calendar_Month |
|
51
|
|
|
* </code> |
|
52
|
|
|
* It defaults to building Calendar_Month objects.<br> |
|
53
|
|
|
* Use the constract CALENDAR_FIRST_DAY_OF_WEEK to control the first day of the week |
|
54
|
|
|
* for Month or Week objects (e.g. 0 = Sunday, 6 = Saturday) |
|
55
|
|
|
* |
|
56
|
|
|
* @category Date and Time |
|
57
|
|
|
* @package Calendar |
|
58
|
|
|
* @author Harry Fuecks <[email protected]> |
|
59
|
|
|
* @author Lorenzo Alberton <[email protected]> |
|
60
|
|
|
* @copyright 2003-2007 Harry Fuecks, Lorenzo Alberton |
|
61
|
|
|
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) |
|
62
|
|
|
* @link http://pear.php.net/package/Calendar |
|
63
|
|
|
* @access protected |
|
64
|
|
|
*/ |
|
65
|
|
|
class Factory |
|
66
|
|
|
{ |
|
67
|
|
|
/** |
|
68
|
|
|
* Creates a calendar object given the type and units |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $type class of calendar object to create |
|
71
|
|
|
* @param int $y year |
|
72
|
|
|
* @param int $m month |
|
73
|
|
|
* @param int $d day |
|
74
|
|
|
* @param int $h hour |
|
75
|
|
|
* @param int $i minute |
|
76
|
|
|
* @param int $s second |
|
77
|
|
|
* |
|
78
|
|
|
* @return object subclass of Calendar |
|
79
|
|
|
* @access public |
|
80
|
|
|
* @static |
|
81
|
|
|
*/ |
|
82
|
|
|
function create($type, $y = 2000, $m = 1, $d = 1, $h = 0, $i = 0, $s = 0) |
|
|
|
|
|
|
83
|
|
|
{ |
|
84
|
|
|
$firstDay = defined('CALENDAR_FIRST_DAY_OF_WEEK') ? CALENDAR_FIRST_DAY_OF_WEEK : 1; |
|
85
|
|
|
switch ($type) { |
|
86
|
|
|
case 'Day': |
|
87
|
|
|
return new Day($y, $m, $d); |
|
88
|
|
|
case 'Month': |
|
89
|
|
|
// Set default state for which month type to build |
|
90
|
|
|
if (!defined('CALENDAR_MONTH_STATE')) { |
|
91
|
|
|
define('CALENDAR_MONTH_STATE', CALENDAR_USE_MONTH); |
|
92
|
|
|
} |
|
93
|
|
|
switch (CALENDAR_MONTH_STATE) { |
|
94
|
|
|
case CALENDAR_USE_MONTH_WEEKDAYS: |
|
95
|
|
|
$class = 'PEAR\Calendar\Month\Weekdays'; |
|
96
|
|
|
break; |
|
97
|
|
|
case CALENDAR_USE_MONTH_WEEKS: |
|
98
|
|
|
$class = 'PEAR\Calendar\Month\Weeks'; |
|
99
|
|
|
break; |
|
100
|
|
|
case CALENDAR_USE_MONTH: |
|
101
|
|
|
default: |
|
102
|
|
|
$class = 'PEAR\Calendar\Month'; |
|
103
|
|
|
break; |
|
104
|
|
|
} |
|
105
|
|
|
return new $class($y, $m, $firstDay); |
|
106
|
|
|
case 'Week': |
|
107
|
|
|
return new Week($y, $m, $d, $firstDay); |
|
108
|
|
|
case 'Hour': |
|
109
|
|
|
return new Hour($y, $m, $d, $h); |
|
110
|
|
|
case 'Minute': |
|
111
|
|
|
return new Minute($y, $m, $d, $h, $i); |
|
112
|
|
|
case 'Second': |
|
113
|
|
|
return new Second($y, $m, $d, $h, $i, $s); |
|
114
|
|
|
case 'Year': |
|
115
|
|
|
return new Year($y); |
|
116
|
|
|
default: |
|
117
|
|
|
include_once 'PEAR.php'; |
|
118
|
|
|
PEAR::raiseError('Calendar_Factory::create() unrecognised type: '.$type, |
|
|
|
|
|
|
119
|
|
|
null, PEAR_ERROR_TRIGGER, E_USER_NOTICE, 'Calendar_Factory::create()'); |
|
|
|
|
|
|
120
|
|
|
return false; |
|
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Creates an instance of a calendar object, given a type and timestamp |
|
126
|
|
|
* |
|
127
|
|
|
* @param string $type type of object to create |
|
128
|
|
|
* @param mixed $stamp timestamp (depending on Calendar engine being used) |
|
129
|
|
|
* |
|
130
|
|
|
* @return object subclass of Calendar |
|
131
|
|
|
* @access public |
|
132
|
|
|
* @static |
|
133
|
|
|
*/ |
|
134
|
|
|
function & createByTimestamp($type, $stamp) |
|
135
|
|
|
{ |
|
136
|
|
|
$cE = & Calendar_Engine_Factory::getEngine(); |
|
137
|
|
|
$y = $cE->stampToYear($stamp); |
|
138
|
|
|
$m = $cE->stampToMonth($stamp); |
|
139
|
|
|
$d = $cE->stampToDay($stamp); |
|
140
|
|
|
$h = $cE->stampToHour($stamp); |
|
141
|
|
|
$i = $cE->stampToMinute($stamp); |
|
142
|
|
|
$s = $cE->stampToSecond($stamp); |
|
143
|
|
|
$cal = Factory::create($type, $y, $m, $d, $h, $i, $s); |
|
|
|
|
|
|
144
|
|
|
return $cal; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.