3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * DB_Table_Valid validates values against DB_Table column types.
12 * Copyright (c) 1997-2007, Paul M. Jones <pmjones@php.net>
13 * David C. Morse <morse@php.net>
14 * Mark Wiesemann <wiesemann@php.net>
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
21 * * Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * * Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * * The names of the authors may not be used to endorse or promote products
27 * derived from this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
30 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
31 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
33 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
34 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
35 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
36 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
37 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
38 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 * @author Paul M. Jones <pmjones@php.net>
44 * @author David C. Morse <morse@php.net>
45 * @author Mark Wiesemann <wiesemann@php.net>
46 * @license http://opensource.org/licenses/bsd-license.php New BSD License
47 * @version CVS: $Id: Valid.php,v 1.10 2007/12/13 16:52:15 wiesemann Exp $
48 * @link http://pear.php.net/package/DB_Table
52 * DB_Table class for constants and other globals.
54 require_once 'DB/Table.php';
58 * validation ranges for integers
60 if (! isset($GLOBALS['_DB_TABLE']['valid'])) {
61 $GLOBALS['_DB_TABLE']['valid'] = array(
62 'smallint' => array(pow(-2, 15), pow(+
2, 15) - 1),
63 'integer' => array(pow(-2, 31), pow(+
2, 31) - 1),
64 'bigint' => array(pow(-2, 63), pow(+
2, 63) - 1)
70 * DB_Table_Valid validates values against DB_Table column types.
74 * @author Paul M. Jones <pmjones@php.net>
75 * @author David C. Morse <morse@php.net>
76 * @author Mark Wiesemann <wiesemann@php.net>
77 * @version Release: 1.5.6
78 * @link http://pear.php.net/package/DB_Table
81 class DB_Table_Valid
{
85 * Check if a value validates against the 'boolean' data type.
91 * @param mixed $value The value to validate.
93 * @return boolean True if the value is valid for the data type, false
98 function isBoolean($value)
100 if ($value === true ||
$value === false) {
102 } elseif (is_numeric($value) && ($value == 0 ||
$value == 1)) {
112 * Check if a value validates against the 'char' and 'varchar' data type.
114 * We allow most anything here, only checking that the length is in range.
120 * @param mixed $value The value to validate.
122 * @return boolean True if the value is valid for the data type, false
127 function isChar($value, $colsize)
129 $is_scalar = (! is_array($value) && ! is_object($value));
130 $in_range = (strlen($value) <= $colsize);
131 return $is_scalar && $in_range;
137 * Check if a value validates against the 'smallint' data type.
143 * @param mixed $value The value to validate.
145 * @return boolean True if the value is valid for the data type, false
150 function isSmallint($value)
152 return is_integer($value) &&
153 ($value >= $GLOBALS['_DB_TABLE']['valid']['smallint'][0]) &&
154 ($value <= $GLOBALS['_DB_TABLE']['valid']['smallint'][1]);
160 * Check if a value validates against the 'integer' data type.
166 * @param mixed $value The value to validate.
168 * @return boolean True if the value is valid for the data type, false
173 function isInteger($value)
175 return is_integer($value) &&
176 ($value >= $GLOBALS['_DB_TABLE']['valid']['integer'][0]) &&
177 ($value <= $GLOBALS['_DB_TABLE']['valid']['integer'][1]);
183 * Check if a value validates against the 'bigint' data type.
189 * @param mixed $value The value to validate.
191 * @return boolean True if the value is valid for the data type, false
196 function isBigint($value)
198 return is_integer($value) &&
199 ($value >= $GLOBALS['_DB_TABLE']['valid']['bigint'][0]) &&
200 ($value <= $GLOBALS['_DB_TABLE']['valid']['bigint'][1]);
206 * Check if a value validates against the 'decimal' data type.
208 * For the column defined "DECIMAL(5,2)" standard SQL requires that
209 * the column be able to store any value with 5 digits and 2
210 * decimals. In this case, therefore, the range of values that can be
211 * stored in the column is from -999.99 to 999.99. DB_Table attempts
212 * to enforce this behavior regardless of the RDBMS backend behavior.
218 * @param mixed $value The value to validate.
220 * @param string $colsize The 'size' to use for validation (to make
221 * sure of min/max and decimal places).
223 * @param string $colscope The 'scope' to use for validation (to make
224 * sure of min/max and decimal places).
226 * @return boolean True if the value is valid for the data type, false
231 function isDecimal($value, $colsize, $colscope)
233 if (! is_numeric($value)) {
237 // maximum number of digits allowed to the left
238 // and right of the decimal point.
239 $right_max = $colscope;
240 $left_max = $colsize - $colscope;
242 // ignore negative signs in all validation
243 $value = str_replace('-', '', $value);
245 // find the decimal point, then get the left
246 // and right portions.
247 $pos = strpos($value, '.');
248 if ($pos === false) {
252 $left = substr($value, 0, $pos);
253 $right = substr($value, $pos+
1);
256 // how long are the left and right portions?
257 $left_len = strlen($left);
258 $right_len = strlen($right);
260 // do the portions exceed their maxes?
261 if ($left_len > $left_max ||
262 $right_len > $right_max) {
263 // one or the other exceeds the max lengths
266 // both are within parameters
274 * Check if a value validates against the 'single' data type.
280 * @param mixed $value The value to validate.
282 * @return boolean True if the value is valid for the data type, false
287 function isSingle($value)
289 return is_float($value);
295 * Check if a value validates against the 'double' data type.
301 * @param mixed $value The value to validate.
303 * @return boolean True if the value is valid for the data type, false
308 function isDouble($value)
310 return is_float($value);
316 * Check if a value validates against the 'time' data type.
322 * @param mixed $value The value to validate.
324 * @return boolean True if the value is valid for the data type, false
329 function isTime($value)
333 $h = substr($value, 0, 2);
334 $s1 = substr($value, 2, 1);
335 $i = substr($value, 3, 2);
336 $s2 = substr($value, 5, 1);
337 $s = substr($value, 6, 2);
340 if (strlen($value) != 8 ||
341 ! is_numeric($h) ||
$h < 0 ||
$h > 23 ||
343 ! is_numeric($i) ||
$i < 0 ||
$i > 59 ||
345 ! is_numeric($s) ||
$s < 0 ||
$s > 59) {
359 * Check if a value validates against the 'date' data type.
365 * @param mixed $value The value to validate.
367 * @return boolean True if the value is valid for the data type, false
372 function isDate($value)
376 $y = substr($value, 0, 4);
377 $s1 = substr($value, 4, 1);
378 $m = substr($value, 5, 2);
379 $s2 = substr($value, 7, 1);
380 $d = substr($value, 8, 2);
383 if (strlen($value) != 10 ||
$s1 != '-' ||
$s2 != '-' ||
384 ! checkdate($m, $d, $y)) {
398 * Check if a value validates against the 'timestamp' data type.
404 * @param mixed $value The value to validate.
406 * @return boolean True if the value is valid for the data type, false
411 function isTimestamp($value)
413 // yyyy-mm-dd hh:ii:ss
414 // 0123456789012345678
415 $date = substr($value, 0, 10);
416 $sep = substr($value, 10, 1);
417 $time = substr($value, 11, 8);
419 if (strlen($value) != 19 ||
$sep != ' ' ||
420 ! DB_Table_Valid
::isDate($date) ||
421 ! DB_Table_Valid
::isTime($time)) {
435 * Check if a value validates against the 'clob' data type.
441 * @param mixed $value The value to validate.
443 * @return boolean True if the value is valid for the data type, false
448 function isClob($value)
450 return is_string($value);