Source for file RSValidation.php

Documentation is available at RSValidation.php


1 <?php
2 // RSValidation: The Rocket Science Validation Class
3 // Copyright (C) 2003 Lukas Feiler
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 /**This file contains the class RSValidation.
20 *
21 * @package RSValidation
22 * @author Lukas Feiler <lukas.feiler@chello.at>
23 * @copyright Lukas Feiler 2003
24 * @filesource
25 */
26
27 /**RSValidation extends PEAR::Validate.
28 */
29 require_once('Validate.php');
30
31 /**A Utility Class for Validating all kinds of data.
32 *
33 * General order of arguments for all methods:<br>
34 * The string to operate on is always the first argument.
35 *
36 * @author Lukas Feiler <lukas.feiler@chello.at>
37 * @version 0.1.9
38 * @copyright Lukas Feiler 2003
39 * @package RSValidation
40 * @static
41 */
42 class RSValidation extends Validate {
43
44 /**An alias for isStringExt.
45 *
46 * @see isStringExt
47 */
48 function isString($str, $minLength = null, $maxLength = null)
49 {
50 return RSValidation::isStringExt($str, $minLength, $maxLength);
51 }
52
53 /**Returns true if the first argument is a string ({@link PHP_MANUAL#is_string}) and has a minimum and/or maximum length.
54 * @param String $str The string to validate.
55 * @param int $minLength The minimal length of the string; ignored if is false.
56 * @param int $maxLength The maximal length of the string; ignored if is false.
57 * @return boolean True if the string passed as first argument matched all conditions.
58 * @source
59 */
60 function isStringExt($str, $minLength = null, $maxLength = null, $format = null)
61 {
62 return Validate::string(
63 $str,
64 array(
65 'min_length' => $minLength,
66 'max_length' => $maxLength,
67 'format' => $format
68 )
69 );
70 }
71
72 /**Returns true if the string passed as first argument starts with the string passed as second argument. Otherwise false is returned.
73 *
74 * @param String $string
75 * @param String $startsWith
76 * @returns boolean True if the first argument starts with the second argument; otherwise false
77 */
78 function startsWith($string, $startsWith)
79 {
80 return strpos($string, $startsWith) === 0;
81 }
82
83 /**Returns true if the string passed as first argument ends with the string passed as second argument. Otherwise false is returned.
84 *
85 * @param String $string
86 * @param String $startsWith
87 * @returns boolean True if the first argument ends with the second argument; otherwise false
88 */
89 function endsWith($string, $endsWith)
90 {
91 return $endsWith == substr($string, strlen($string) - strlen($endsWith));
92 }
93
94 /**Returns true if the passed string contains only alpha characters.
95 *
96 * @param String $string The string to validate
97 * @returns boolean True if the string only contains alph characters.
98 */
99 function isAlpha($string)
100 {
101 return eregi("^[[:alpha:]]+$", $string);
102 }
103
104 /**Returns true if the passed argument is a number or a numeric string.
105 *
106 * In fact just a nicer way to call the native function {@link PHP_MANUAL#is_numeric}.
107 *
108 * @param mixed $number The value to validate.
109 * @returns boolean The return value of {@link PHP_MANUAL#is_numeric} is returned.
110 */
111 function isNumeric($number)
112 {
113 return is_numeric($number);
114 }
115
116 /**Validate date and times. The default format is YYYY-MM-DD.
117 *
118 * This method is just a simple front-end for Validate::date. Thanks to Tomas V.V.Cox and Pierre-Alain Joye.
119 *
120 * @param string $date Date to validate
121 * @param string $format The format of the date (%d-%m-%Y)
122 * @param array $min The date has to be greater than this array($day, $month, $year)
123 * @param array $max The date has to be smaller than this array($day, $month, $year)
124 *
125 * @return boolean
126 */
127 function isDate($date, $format = '%Y-%m-%d', $min = array(), $max = array())
128 {
129 $options = array(
130 'format' => $format,
131 'min' => $min,
132 'max' => $max
133 );
134 echo $date;
135 return Validate::date($date, $options);
136 }
137
138 /**This method is just like RSValidation::isDate but with a default format of HH:MM:SS.
139 */
140 function isTime($date, $format = '%H:%i:%s', $min = array(), $max = array())
141 {
142 return RSValidation::isDate($date, $format, $min, $max);
143 }
144
145 /**This method is just like RSValidation::isDate but with a default format of YYYY-MM-DD HH:MM:SS.
146 */
147 function isDateTime($date, $format = '%Y-%m-%d %H:%i:%s', $min = array(), $max = array())
148 {
149 return RSValidation::isDate($date, $format, $min, $max);
150 }
151
152 /**This method is just like RSValidation::isDate but with a default format of YYYYMMDDHHMMSS.
153 */
154 function isTimestamp($date, $format = '%Y%m%d%H%i%s', $min = array(), $max = array())
155 {
156 return RSValidation::isDate($date, $format, $min, $max);
157 }
158
159
160 /**Returns true if the first argument is a number of a numeric string
161 * and has the maximum and/or minumum size as specified by the second and third argument.
162 *
163 * This method is just a simple front-end for Validate::number. Thanks to Tomas V.V.Cox and Pierre-Alain Joye.
164 *
165 * @param mixed $number A string or a number
166 * @param int $minSize The minimal size; ignored if false;
167 * @param int $minSize The maximal size; ignored if false;
168 * @return boolean True if the first argument matched all conditions.
169 */
170 function isInt($number, $minSize = null, $maxSize = null)
171 {
172 $decimal = null;
173 $dec_prec = null;
174 return Validate::number(
175 $number,
176 array(
177 'decimal' => $decimal,
178 'dec_prec' => $dec_prec,
179 'min' => $minSize,
180 'max' => $maxSize
181 )
182 );
183 }
184
185 /**Returns true if the first argument is a float.
186 *
187 * This method is just a simple front-end for Validate::number. Thanks to Tomas V.V.Cox and Pierre-Alain Joye.
188 *
189 * @param mixed $number A string or a number
190 * @param int $minSize The minimal size; ignored if false;
191 * @param int $minSize The maximal size; ignored if false;
192 * @param mixed $decimal The decimal char or false when decimal not allowed; optional; The default is false.
193 * @param mixed $decPrec The precision of the decimal value; optional; The default is false.
194 * @return boolean
195 */
196 function isFloat($number, $minSize = null, $maxSize = null, $decimal = ',', $decPrec = false)
197 {
198 return Validate::number(
199 $number,
200 array(
201 'decimal' => $decimal,
202 'dec_prec' => $decPrec,
203 'min' => $minSize,
204 'max' => $maxSize
205 )
206 );
207 }
208
209 /**Returns true if the passed string contains only alpha-numeric characters.
210 *
211 * @param String $string The string to validate.
212 * @returns boolean
213 */
214 function isAlphanumeric($string)
215 {
216 return eregi("^[[:alnum:]]+$", $string);
217 }
218
219 /**Returns true if the passed string contains only alpha-numeric characters and underscores (_)
220 *
221 * This method is especially useful to find out if a value is avalid identifier for let's say a database object.
222 * If you want do find out just that use {@see isValidDatabaseObjectName}.
223 *
224 * @param String $string The string to validate.
225 * @returns boolean True if the string only contains alpha-numeric characters and underscores.
226 */
227 function isAlphanumericIncludingUnderscore($string)
228 {
229 return eregi("^[[:alnum:]_]+$", $string);
230 }
231
232 /**Returns true if the passed string is valid database object name.
233 *
234 * Returns true if the passed string contains only alpha-numeric characters and underscores.
235 * {@see isAlphanumericIncludingUnderscore} is called to find that out.
236 *
237 * @param String $string The string to validate.
238 * @returns boolean True if the string is a valid database object name.
239 */
240 function isValidDatabaseObjectName($string)
241 {
242 return RSValidation::isAlphanumericIncludingUnderscore($string);
243 }
244
245 /**Returns true if the passed string is formatted as a valid e-mail adress.
246 *
247 * This method is just a simple front-end for Validate::email. Thanks to Tomas V.V.Cox and Pierre-Alain Joye.
248 *
249 * @param String $string The string to check.
250 * @returns boolean true if the string is a valid e-mail address.
251 */
252 function isEmail($string, $checkDomain = false)
253 {
254 return Validate::email($string, $checkDomain);
255 }
256
257 /**Returns true if the passed string contains only alphanumeric characters of the Western alphabets
258 *
259 * Checks if a string contains only a subset of alphanumerics characters
260 * allowed in the Western alphabets. Useful for validation of names.
261 *
262 * @param String $string The string to validate.
263 * @return boolean True if $string contains only alphanumerics characters of the Western alphabets; otherwise false is returned.
264 */
265 function isCleanText($string)
266 {
267 return eregi("^[a-zA-Z[:space:]ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ`´']+$", $string);
268 }
269
270 /**Returns true if the passed argument is valid credit card number.
271 *
272 * This method is just a simple front-end for Validate::creditCard. Thanks to Ondrej Jombik.
273 *
274 * @param String $number number
275 * @return boolean True if number is valid, otherwise false.
276 */
277 function creditCard($number)
278 {
279 return Validate::creditCard($number);
280 }
281 }
282 ?>

Documentation generated on Mon, 8 Dec 2003 13:13:11 +0100 by phpDocumentor 1.2.3