1 | <?php
|
---|
2 | /* vim: set expandtab tabstop=4 shiftwidth=4: */
|
---|
3 |
|
---|
4 | /**
|
---|
5 | * Contains the Calendar_Validator 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 | * @copyright 2003-2007 Harry Fuecks
|
---|
34 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
|
---|
35 | * @version CVS: $Id: Validator.php 247251 2007-11-28 19:42:33Z quipo $
|
---|
36 | * @link http://pear.php.net/package/Calendar
|
---|
37 | */
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Validation Error Messages
|
---|
41 | */
|
---|
42 | if (!defined('CALENDAR_VALUE_TOOSMALL')) {
|
---|
43 | define('CALENDAR_VALUE_TOOSMALL', 'Too small: min = ');
|
---|
44 | }
|
---|
45 | if (!defined('CALENDAR_VALUE_TOOLARGE')) {
|
---|
46 | define('CALENDAR_VALUE_TOOLARGE', 'Too large: max = ');
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Used to validate any given Calendar date object. Instances of this class
|
---|
51 | * can be obtained from any data object using the getValidator method
|
---|
52 | *
|
---|
53 | * @category Date and Time
|
---|
54 | * @package Calendar
|
---|
55 | * @author Harry Fuecks <hfuecks@phppatterns.com>
|
---|
56 | * @copyright 2003-2007 Harry Fuecks
|
---|
57 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
|
---|
58 | * @link http://pear.php.net/package/Calendar
|
---|
59 | * @see Calendar::getValidator()
|
---|
60 | * @access public
|
---|
61 | */
|
---|
62 | class Calendar_Validator
|
---|
63 | {
|
---|
64 | /**
|
---|
65 | * Instance of the Calendar date object to validate
|
---|
66 | * @var object
|
---|
67 | * @access private
|
---|
68 | */
|
---|
69 | var $calendar;
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Instance of the Calendar_Engine
|
---|
73 | * @var object
|
---|
74 | * @access private
|
---|
75 | */
|
---|
76 | var $cE;
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Array of errors for validation failures
|
---|
80 | * @var array
|
---|
81 | * @access private
|
---|
82 | */
|
---|
83 | var $errors = array();
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Constructs Calendar_Validator
|
---|
87 | *
|
---|
88 | * @param object &$calendar subclass of Calendar
|
---|
89 | *
|
---|
90 | * @access public
|
---|
91 | */
|
---|
92 | function Calendar_Validator(&$calendar)
|
---|
93 | {
|
---|
94 | $this->calendar = & $calendar;
|
---|
95 | $this->cE = & $calendar->getEngine();
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Calls all the other isValidXXX() methods in the validator
|
---|
100 | *
|
---|
101 | * @return boolean
|
---|
102 | * @access public
|
---|
103 | */
|
---|
104 | function isValid()
|
---|
105 | {
|
---|
106 | $checks = array('isValidYear', 'isValidMonth', 'isValidDay',
|
---|
107 | 'isValidHour', 'isValidMinute', 'isValidSecond');
|
---|
108 | $valid = true;
|
---|
109 | foreach ($checks as $check) {
|
---|
110 | if (!$this->{$check}()) {
|
---|
111 | $valid = false;
|
---|
112 | }
|
---|
113 | }
|
---|
114 | return $valid;
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Check whether this is a valid year
|
---|
119 | *
|
---|
120 | * @return boolean
|
---|
121 | * @access public
|
---|
122 | */
|
---|
123 | function isValidYear()
|
---|
124 | {
|
---|
125 | $y = $this->calendar->thisYear();
|
---|
126 | $min = $this->cE->getMinYears();
|
---|
127 | if ($min > $y) {
|
---|
128 | $this->errors[] = new Calendar_Validation_Error(
|
---|
129 | 'Year', $y, CALENDAR_VALUE_TOOSMALL.$min);
|
---|
130 | return false;
|
---|
131 | }
|
---|
132 | $max = $this->cE->getMaxYears();
|
---|
133 | if ($y > $max) {
|
---|
134 | $this->errors[] = new Calendar_Validation_Error(
|
---|
135 | 'Year', $y, CALENDAR_VALUE_TOOLARGE.$max);
|
---|
136 | return false;
|
---|
137 | }
|
---|
138 | return true;
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Check whether this is a valid month
|
---|
143 | *
|
---|
144 | * @return boolean
|
---|
145 | * @access public
|
---|
146 | */
|
---|
147 | function isValidMonth()
|
---|
148 | {
|
---|
149 | $m = $this->calendar->thisMonth();
|
---|
150 | $min = 1;
|
---|
151 | if ($min > $m) {
|
---|
152 | $this->errors[] = new Calendar_Validation_Error(
|
---|
153 | 'Month', $m, CALENDAR_VALUE_TOOSMALL.$min);
|
---|
154 | return false;
|
---|
155 | }
|
---|
156 | $max = $this->cE->getMonthsInYear($this->calendar->thisYear());
|
---|
157 | if ($m > $max) {
|
---|
158 | $this->errors[] = new Calendar_Validation_Error(
|
---|
159 | 'Month', $m, CALENDAR_VALUE_TOOLARGE.$max);
|
---|
160 | return false;
|
---|
161 | }
|
---|
162 | return true;
|
---|
163 | }
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Check whether this is a valid day
|
---|
167 | *
|
---|
168 | * @return boolean
|
---|
169 | * @access public
|
---|
170 | */
|
---|
171 | function isValidDay()
|
---|
172 | {
|
---|
173 | $d = $this->calendar->thisDay();
|
---|
174 | $min = 1;
|
---|
175 | if ($min > $d) {
|
---|
176 | $this->errors[] = new Calendar_Validation_Error(
|
---|
177 | 'Day', $d, CALENDAR_VALUE_TOOSMALL.$min);
|
---|
178 | return false;
|
---|
179 | }
|
---|
180 | $max = $this->cE->getDaysInMonth(
|
---|
181 | $this->calendar->thisYear(),
|
---|
182 | $this->calendar->thisMonth()
|
---|
183 | );
|
---|
184 | if ($d > $max) {
|
---|
185 | $this->errors[] = new Calendar_Validation_Error(
|
---|
186 | 'Day', $d, CALENDAR_VALUE_TOOLARGE.$max);
|
---|
187 | return false;
|
---|
188 | }
|
---|
189 | return true;
|
---|
190 | }
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Check whether this is a valid hour
|
---|
194 | *
|
---|
195 | * @return boolean
|
---|
196 | * @access public
|
---|
197 | */
|
---|
198 | function isValidHour()
|
---|
199 | {
|
---|
200 | $h = $this->calendar->thisHour();
|
---|
201 | $min = 0;
|
---|
202 | if ($min > $h) {
|
---|
203 | $this->errors[] = new Calendar_Validation_Error(
|
---|
204 | 'Hour', $h, CALENDAR_VALUE_TOOSMALL.$min);
|
---|
205 | return false;
|
---|
206 | }
|
---|
207 | $max = ($this->cE->getHoursInDay($this->calendar->thisDay())-1);
|
---|
208 | if ($h > $max) {
|
---|
209 | $this->errors[] = new Calendar_Validation_Error(
|
---|
210 | 'Hour', $h, CALENDAR_VALUE_TOOLARGE.$max);
|
---|
211 | return false;
|
---|
212 | }
|
---|
213 | return true;
|
---|
214 | }
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * Check whether this is a valid minute
|
---|
218 | *
|
---|
219 | * @return boolean
|
---|
220 | * @access public
|
---|
221 | */
|
---|
222 | function isValidMinute()
|
---|
223 | {
|
---|
224 | $i = $this->calendar->thisMinute();
|
---|
225 | $min = 0;
|
---|
226 | if ($min > $i) {
|
---|
227 | $this->errors[] = new Calendar_Validation_Error(
|
---|
228 | 'Minute', $i, CALENDAR_VALUE_TOOSMALL.$min);
|
---|
229 | return false;
|
---|
230 | }
|
---|
231 | $max = ($this->cE->getMinutesInHour($this->calendar->thisHour())-1);
|
---|
232 | if ($i > $max) {
|
---|
233 | $this->errors[] = new Calendar_Validation_Error(
|
---|
234 | 'Minute', $i, CALENDAR_VALUE_TOOLARGE.$max);
|
---|
235 | return false;
|
---|
236 | }
|
---|
237 | return true;
|
---|
238 | }
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * Check whether this is a valid second
|
---|
242 | *
|
---|
243 | * @return boolean
|
---|
244 | * @access public
|
---|
245 | */
|
---|
246 | function isValidSecond()
|
---|
247 | {
|
---|
248 | $s = $this->calendar->thisSecond();
|
---|
249 | $min = 0;
|
---|
250 | if ($min > $s) {
|
---|
251 | $this->errors[] = new Calendar_Validation_Error(
|
---|
252 | 'Second', $s, CALENDAR_VALUE_TOOSMALL.$min);
|
---|
253 | return false;
|
---|
254 | }
|
---|
255 | $max = ($this->cE->getSecondsInMinute($this->calendar->thisMinute())-1);
|
---|
256 | if ($s > $max) {
|
---|
257 | $this->errors[] = new Calendar_Validation_Error(
|
---|
258 | 'Second', $s, CALENDAR_VALUE_TOOLARGE.$max);
|
---|
259 | return false;
|
---|
260 | }
|
---|
261 | return true;
|
---|
262 | }
|
---|
263 |
|
---|
264 | /**
|
---|
265 | * Iterates over any validation errors
|
---|
266 | *
|
---|
267 | * @return mixed either Calendar_Validation_Error or false
|
---|
268 | * @access public
|
---|
269 | */
|
---|
270 | function fetch()
|
---|
271 | {
|
---|
272 | $error = each($this->errors);
|
---|
273 | if ($error) {
|
---|
274 | return $error['value'];
|
---|
275 | } else {
|
---|
276 | reset($this->errors);
|
---|
277 | return false;
|
---|
278 | }
|
---|
279 | }
|
---|
280 | }
|
---|
281 |
|
---|
282 | /**
|
---|
283 | * For Validation Error messages
|
---|
284 | *
|
---|
285 | * @category Date and Time
|
---|
286 | * @package Calendar
|
---|
287 | * @author Harry Fuecks <hfuecks@phppatterns.com>
|
---|
288 | * @copyright 2003-2007 Harry Fuecks
|
---|
289 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
|
---|
290 | * @link http://pear.php.net/package/Calendar
|
---|
291 | * @see Calendar::fetch()
|
---|
292 | * @access public
|
---|
293 | */
|
---|
294 | class Calendar_Validation_Error
|
---|
295 | {
|
---|
296 | /**
|
---|
297 | * Date unit (e.g. month,hour,second) which failed test
|
---|
298 | * @var string
|
---|
299 | * @access private
|
---|
300 | */
|
---|
301 | var $unit;
|
---|
302 |
|
---|
303 | /**
|
---|
304 | * Value of unit which failed test
|
---|
305 | * @var int
|
---|
306 | * @access private
|
---|
307 | */
|
---|
308 | var $value;
|
---|
309 |
|
---|
310 | /**
|
---|
311 | * Validation error message
|
---|
312 | * @var string
|
---|
313 | * @access private
|
---|
314 | */
|
---|
315 | var $message;
|
---|
316 |
|
---|
317 | /**
|
---|
318 | * Constructs Calendar_Validation_Error
|
---|
319 | *
|
---|
320 | * @param string $unit Date unit (e.g. month,hour,second)
|
---|
321 | * @param int $value Value of unit which failed test
|
---|
322 | * @param string $message Validation error message
|
---|
323 | *
|
---|
324 | * @access protected
|
---|
325 | */
|
---|
326 | function Calendar_Validation_Error($unit, $value, $message)
|
---|
327 | {
|
---|
328 | $this->unit = $unit;
|
---|
329 | $this->value = $value;
|
---|
330 | $this->message = $message;
|
---|
331 | }
|
---|
332 |
|
---|
333 | /**
|
---|
334 | * Returns the Date unit
|
---|
335 | *
|
---|
336 | * @return string
|
---|
337 | * @access public
|
---|
338 | */
|
---|
339 | function getUnit()
|
---|
340 | {
|
---|
341 | return $this->unit;
|
---|
342 | }
|
---|
343 |
|
---|
344 | /**
|
---|
345 | * Returns the value of the unit
|
---|
346 | *
|
---|
347 | * @return int
|
---|
348 | * @access public
|
---|
349 | */
|
---|
350 | function getValue()
|
---|
351 | {
|
---|
352 | return $this->value;
|
---|
353 | }
|
---|
354 |
|
---|
355 | /**
|
---|
356 | * Returns the validation error message
|
---|
357 | *
|
---|
358 | * @return string
|
---|
359 | * @access public
|
---|
360 | */
|
---|
361 | function getMessage()
|
---|
362 | {
|
---|
363 | return $this->message;
|
---|
364 | }
|
---|
365 |
|
---|
366 | /**
|
---|
367 | * Returns a string containing the unit, value and error message
|
---|
368 | *
|
---|
369 | * @return string
|
---|
370 | * @access public
|
---|
371 | */
|
---|
372 | function toString ()
|
---|
373 | {
|
---|
374 | return $this->unit.' = '.$this->value.' ['.$this->message.']';
|
---|
375 | }
|
---|
376 | }
|
---|
377 | ?> |
---|