Subversion Repositories Applications.papyrus

Rev

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