|
1
|
|
|
<?php |
|
2
|
|
|
/* vim: set expandtab tabstop=4 shiftwidth=4: */ |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Contains the Calendar_Decorator_Weekday 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\Decorator; |
|
40
|
|
|
|
|
41
|
|
|
use PEAR\Calendar\Decorator; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Decorator for fetching the day of the week |
|
45
|
|
|
* <code> |
|
46
|
|
|
* $Day = new Calendar_Day(2003, 10, 23); |
|
47
|
|
|
* $Weekday = new Calendar_Decorator_Weekday($Day); |
|
48
|
|
|
* $Weekday->setFirstDay(0); // Set first day of week to Sunday (default Mon) |
|
49
|
|
|
* echo $Weekday->thisWeekDay(); // Displays 5 - fifth day of week relative to Sun |
|
50
|
|
|
* </code> |
|
51
|
|
|
* |
|
52
|
|
|
* @category Date and Time |
|
53
|
|
|
* @package Calendar |
|
54
|
|
|
* @author Harry Fuecks <[email protected]> |
|
55
|
|
|
* @author Lorenzo Alberton <[email protected]> |
|
56
|
|
|
* @copyright 2003-2007 Harry Fuecks, Lorenzo Alberton |
|
57
|
|
|
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) |
|
58
|
|
|
* @link http://pear.php.net/package/Calendar |
|
59
|
|
|
* @access public |
|
60
|
|
|
*/ |
|
61
|
|
|
class Weekday extends Decorator |
|
62
|
|
|
{ |
|
63
|
|
|
/** |
|
64
|
|
|
* First day of week |
|
65
|
|
|
* @var int (default = 1 for Monday) |
|
66
|
|
|
* @access private |
|
67
|
|
|
*/ |
|
68
|
|
|
var $firstDay = 1; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Constructs Calendar_Decorator_Weekday |
|
72
|
|
|
* |
|
73
|
|
|
* @param object &$Calendar subclass of Calendar |
|
74
|
|
|
* |
|
75
|
|
|
* @access public |
|
76
|
|
|
*/ |
|
77
|
|
|
function __construct(&$Calendar) |
|
|
|
|
|
|
78
|
|
|
{ |
|
79
|
|
|
parent::__construct($Calendar); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Sets the first day of the week (0 = Sunday, 1 = Monday (default) etc) |
|
84
|
|
|
* |
|
85
|
|
|
* @param int $firstDay first day of week |
|
86
|
|
|
* |
|
87
|
|
|
* @return void |
|
88
|
|
|
* @access public |
|
89
|
|
|
*/ |
|
90
|
|
|
function setFirstDay($firstDay) |
|
|
|
|
|
|
91
|
|
|
{ |
|
92
|
|
|
$this->firstDay = (int)$firstDay; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Returns the previous weekday |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $format (default = 'int') return value format |
|
99
|
|
|
* |
|
100
|
|
|
* @return int $format numeric day of week or timestamp |
|
101
|
|
|
* @access public |
|
102
|
|
|
*/ |
|
103
|
|
|
function prevWeekDay($format = 'int') |
|
|
|
|
|
|
104
|
|
|
{ |
|
105
|
|
|
$ts = $this->calendar->prevDay('timestamp'); |
|
106
|
|
|
$Day = new Calendar_Day(2000, 1, 1); |
|
|
|
|
|
|
107
|
|
|
$Day->setTimeStamp($ts); |
|
108
|
|
|
$day = $this->calendar->cE->getDayOfWeek( |
|
109
|
|
|
$Day->thisYear(), |
|
110
|
|
|
$Day->thisMonth(), |
|
111
|
|
|
$Day->thisDay() |
|
112
|
|
|
); |
|
113
|
|
|
$day = $this->adjustWeekScale($day); |
|
114
|
|
|
return $this->returnValue('Day', $format, $ts, $day); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Returns the current weekday |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $format (default = 'int') return value format |
|
121
|
|
|
* |
|
122
|
|
|
* @return int numeric day of week or timestamp |
|
123
|
|
|
* @access public |
|
124
|
|
|
*/ |
|
125
|
|
|
function thisWeekDay($format = 'int') |
|
|
|
|
|
|
126
|
|
|
{ |
|
127
|
|
|
$ts = $this->calendar->thisDay('timestamp'); |
|
128
|
|
|
$day = $this->calendar->cE->getDayOfWeek( |
|
129
|
|
|
$this->calendar->year, |
|
130
|
|
|
$this->calendar->month, |
|
131
|
|
|
$this->calendar->day |
|
132
|
|
|
); |
|
133
|
|
|
$day = $this->adjustWeekScale($day); |
|
134
|
|
|
return $this->returnValue('Day', $format, $ts, $day); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Returns the next weekday |
|
139
|
|
|
* |
|
140
|
|
|
* @param string $format (default = 'int') return value format |
|
141
|
|
|
* |
|
142
|
|
|
* @return int numeric day of week or timestamp |
|
143
|
|
|
* @access public |
|
144
|
|
|
*/ |
|
145
|
|
|
function nextWeekDay($format = 'int') |
|
|
|
|
|
|
146
|
|
|
{ |
|
147
|
|
|
$ts = $this->calendar->nextDay('timestamp'); |
|
148
|
|
|
$Day = new Calendar_Day(2000, 1, 1); |
|
149
|
|
|
$Day->setTimeStamp($ts); |
|
150
|
|
|
$day = $this->calendar->cE->getDayOfWeek( |
|
151
|
|
|
$Day->thisYear(), |
|
152
|
|
|
$Day->thisMonth(), |
|
153
|
|
|
$Day->thisDay() |
|
154
|
|
|
); |
|
155
|
|
|
$day = $this->adjustWeekScale($day); |
|
156
|
|
|
return $this->returnValue('Day', $format, $ts, $day); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Adjusts the day of the week relative to the first day of the week |
|
161
|
|
|
* |
|
162
|
|
|
* @param int $dayOfWeek day of week calendar from Calendar_Engine |
|
163
|
|
|
* |
|
164
|
|
|
* @return int day of week adjusted to first day |
|
165
|
|
|
* @access private |
|
166
|
|
|
*/ |
|
167
|
|
|
function adjustWeekScale($dayOfWeek) |
|
|
|
|
|
|
168
|
|
|
{ |
|
169
|
|
|
$dayOfWeek = $dayOfWeek - $this->firstDay; |
|
170
|
|
|
if ($dayOfWeek >= 0) { |
|
171
|
|
|
return $dayOfWeek; |
|
172
|
|
|
} else { |
|
173
|
|
|
return $this->calendar->cE->getDaysInWeek( |
|
174
|
|
|
$this->calendar->year, |
|
175
|
|
|
$this->calendar->month, |
|
176
|
|
|
$this->calendar->day |
|
177
|
|
|
) + $dayOfWeek; |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
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.