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