Passed
Push — master ( 5534ff...bbc089 )
by tsms
01:47
created

Validator::isValidDay()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 3
nop 0
dl 0
loc 19
rs 9.7998
c 0
b 0
f 0
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 <[email protected]>
33
 * @copyright 2003-2007 Harry Fuecks
34
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
35
 * @version   CVS: $Id$
36
 * @link      http://pear.php.net/package/Calendar
37
 */
38
namespace PEAR\Calendar;
39
40
/**
41
 * Validation Error Messages
42
 */
43
if (!defined('CALENDAR_VALUE_TOOSMALL')) {
44
    define('CALENDAR_VALUE_TOOSMALL', 'Too small: min = ');
45
}
46
if (!defined('CALENDAR_VALUE_TOOLARGE')) {
47
    define('CALENDAR_VALUE_TOOLARGE', 'Too large: max = ');
48
}
49
50
/**
51
 * Used to validate any given Calendar date object. Instances of this class
52
 * can be obtained from any data object using the getValidator method
53
 *
54
 * @category  Date and Time
55
 * @package   Calendar
56
 * @author    Harry Fuecks <[email protected]>
57
 * @copyright 2003-2007 Harry Fuecks
58
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
59
 * @link      http://pear.php.net/package/Calendar
60
 * @see       Calendar::getValidator()
61
 * @access    public
62
 */
63
class Validator
64
{
65
    /**
66
     * Instance of the Calendar date object to validate
67
     * @var object
68
     * @access private
69
     */
70
    var $calendar;
71
72
    /**
73
     * Instance of the Calendar_Engine
74
     * @var object
75
     * @access private
76
     */
77
    var $cE;
78
79
    /**
80
     * Array of errors for validation failures
81
     * @var array
82
     * @access private
83
     */
84
    var $errors = array();
85
86
    /**
87
     * Constructs Calendar_Validator
88
     *
89
     * @param object &$calendar subclass of Calendar
90
     *
91
     * @access public
92
     */
93
    function __construct(&$calendar)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
94
    {
95
        $this->calendar = & $calendar;
96
        $this->cE       = & $calendar->getEngine();
97
    }
98
99
    /**
100
     * Calls all the other isValidXXX() methods in the validator
101
     *
102
     * @return boolean
103
     * @access public
104
     */
105
    function isValid()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
106
    {
107
        $checks = array('isValidYear', 'isValidMonth', 'isValidDay',
108
            'isValidHour', 'isValidMinute', 'isValidSecond');
109
        $valid  = true;
110
        foreach ($checks as $check) {
111
            if (!$this->{$check}()) {
112
                $valid = false;
113
            }
114
        }
115
        return $valid;
116
    }
117
118
    /**
119
     * Check whether this is a valid year
120
     *
121
     * @return boolean
122
     * @access public
123
     */
124
    function isValidYear()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
125
    {
126
        $y   = $this->calendar->thisYear();
127
        $min = $this->cE->getMinYears();
128
        if ($min > $y) {
129
            $this->errors[] = new ValidationError(
130
                'Year', $y, CALENDAR_VALUE_TOOSMALL.$min);
131
            return false;
132
        }
133
        $max = $this->cE->getMaxYears();
134
        if ($y > $max) {
135
            $this->errors[] = new ValidationError(
136
                'Year', $y, CALENDAR_VALUE_TOOLARGE.$max);
137
            return false;
138
        }
139
        return true;
140
    }
141
142
    /**
143
     * Check whether this is a valid month
144
     *
145
     * @return boolean
146
     * @access public
147
     */
148
    function isValidMonth()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
149
    {
150
        $m   = $this->calendar->thisMonth();
151
        $min = 1;
152
        if ($min > $m) {
153
            $this->errors[] = new ValidationError(
154
                'Month', $m, CALENDAR_VALUE_TOOSMALL.$min);
155
            return false;
156
        }
157
        $max = $this->cE->getMonthsInYear($this->calendar->thisYear());
158
        if ($m > $max) {
159
            $this->errors[] = new ValidationError(
160
                'Month', $m, CALENDAR_VALUE_TOOLARGE.$max);
161
            return false;
162
        }
163
        return true;
164
    }
165
166
    /**
167
     * Check whether this is a valid day
168
     *
169
     * @return boolean
170
     * @access public
171
     */
172
    function isValidDay()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
173
    {
174
        $d   = $this->calendar->thisDay();
175
        $min = 1;
176
        if ($min > $d) {
177
            $this->errors[] = new ValidationError(
178
                'Day', $d, CALENDAR_VALUE_TOOSMALL.$min);
179
            return false;
180
        }
181
        $max = $this->cE->getDaysInMonth(
182
            $this->calendar->thisYear(), 
183
            $this->calendar->thisMonth()
184
        );
185
        if ($d > $max) {
186
            $this->errors[] = new ValidationError(
187
                'Day', $d, CALENDAR_VALUE_TOOLARGE.$max);
188
            return false;
189
        }
190
        return true;
191
    }
192
193
    /**
194
     * Check whether this is a valid hour
195
     *
196
     * @return boolean
197
     * @access public
198
     */
199
    function isValidHour()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
200
    {
201
        $h   = $this->calendar->thisHour();
202
        $min = 0;
203
        if ($min > $h) {
204
            $this->errors[] = new ValidationError(
205
                'Hour', $h, CALENDAR_VALUE_TOOSMALL.$min);
206
            return false;
207
        }
208
        $max = ($this->cE->getHoursInDay($this->calendar->thisDay())-1);
209
        if ($h > $max) {
210
            $this->errors[] = new ValidationError(
211
                'Hour', $h, CALENDAR_VALUE_TOOLARGE.$max);
212
            return false;
213
        }
214
        return true;
215
    }
216
217
    /**
218
     * Check whether this is a valid minute
219
     *
220
     * @return boolean
221
     * @access public
222
     */
223
    function isValidMinute()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
224
    {
225
        $i   = $this->calendar->thisMinute();
226
        $min = 0;
227
        if ($min > $i) {
228
            $this->errors[] = new ValidationError(
229
                'Minute', $i, CALENDAR_VALUE_TOOSMALL.$min);
230
            return false;
231
        }
232
        $max = ($this->cE->getMinutesInHour($this->calendar->thisHour())-1);
233
        if ($i > $max) {
234
            $this->errors[] = new ValidationError(
235
                'Minute', $i, CALENDAR_VALUE_TOOLARGE.$max);
236
            return false;
237
        }
238
        return true;
239
    }
240
241
    /**
242
     * Check whether this is a valid second
243
     *
244
     * @return boolean
245
     * @access public
246
     */
247
    function isValidSecond()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
248
    {
249
        $s   = $this->calendar->thisSecond();
250
        $min = 0;
251
        if ($min > $s) {
252
            $this->errors[] = new ValidationError(
253
                'Second', $s, CALENDAR_VALUE_TOOSMALL.$min);
254
            return false;
255
        }
256
        $max = ($this->cE->getSecondsInMinute($this->calendar->thisMinute())-1);
257
        if ($s > $max) {
258
            $this->errors[] = new ValidationError(
259
                'Second', $s, CALENDAR_VALUE_TOOLARGE.$max);
260
            return false;
261
        }
262
        return true;
263
    }
264
265
    /**
266
     * Iterates over any validation errors
267
     *
268
     * @return mixed either ValidationError or false
269
     * @access public
270
     */
271
    function fetch()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
272
    {
273
        $error = each($this->errors);
0 ignored issues
show
Deprecated Code introduced by
The function each() has been deprecated: 7.2 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

273
        $error = /** @scrutinizer ignore-deprecated */ each($this->errors);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
274
        if ($error) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $error of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
275
            return $error['value'];
276
        } else {
277
            reset($this->errors);
278
            return false;
279
        }
280
    }
281
}
282