Validation Class Reference
[Lib]
Validate several values. More...
Static Public Member Functions |
|
static | is_domain ($value) |
Check if string is s domai name. |
|
static | is_double ($value, $min=false, $max=false) |
Check if value is a double within given
range. |
|
static | is_email ($value) |
Check if value is a valid e-mail address.
|
|
static | is_int ($value, $min=false, $max=false) |
Check if value is a int within given range.
|
|
static | is_ip (&$ip) |
Check if given string is either a IPv4 or
IPv6. |
|
static | is_ip4 (&$ip) |
Check if $ip is a valid IPv4 address in
dotted decimal format. |
|
static | is_ip6 ($ip) |
Checks if given string is an IPv6. |
|
static | is_string_of_length ($value, $min_length=0, $max_length=-1) |
Check if value is a string and validate its
length. |
|
static | is_url ($value) |
Check if value is an url. |
Detailed Description
Validate several values.
- Todo:
- Can be transformed into an adapter to filter extension with PHP 5.2
Definition at line 10 of file validation.cls.php.
Member Function Documentation
static Validation::is_domain | ( | $ | value | ) | [static] |
Check if string is s domai name.
Does not check for valid TLD, use Url validation for that
Definition at line 39 of file validation.cls.php.
00039 { 00040 $ret = true; 00041 $ret = $ret && (strlen($value) <= 255); 00042 00043 $elements = explode('.', $value); 00044 $ret = $ret && (count($elements) > 1); 00045 00046 // Check elements 00047 foreach($elements as $e) { 00048 $l = strlen($e); 00049 $ret = $ret && ($l > 0); 00050 $ret = $ret && ($l <= 63); 00051 $ret = $ret && preg_match('|^[^@ ]+$|', $e); 00052 } 00053 00054 // Check TLD 00055 $tld = array_pop($elements); 00056 $ret = $ret && !preg_match('|^\d+$|', $tld); // TLD not only numbers 00057 00058 return $ret; 00059 }
Check if value is a double within given range.
- Parameters:
-
mixed The value to check mixed Min value or false to not check for min value mixed Max value or false to not check for max value
Definition at line 95 of file validation.cls.php.
00095 { 00096 $ret = is_numeric($value); 00097 if ($ret) { 00098 $value = Cast::float($value); 00099 if ($min !== false) { 00100 $ret = $ret && ($value >= $min); 00101 } 00102 if ($max !== false) { 00103 $ret = $ret && ($value <= $max); 00104 } 00105 } 00106 return $ret; 00107 }
static Validation::is_email | ( | $ | value | ) | [static] |
Check if value is a valid e-mail address.
Definition at line 14 of file validation.cls.php.
00014 { 00015 $parts = explode('@', $value, 2); 00016 $local_part = array_shift($parts); 00017 $domain = array_shift($parts); 00018 00019 $ret = self::is_domain($domain); 00020 // local part may be up to 64 characters 00021 $ret = $ret && (strlen($local_part) <= 64); 00022 // dot is not allowed at the end or beginning 00023 // There is also a rule that 2 or more dots are illegal like in 'forname..lastname@web.de' 00024 // Unfortunately: my neighbor's address IS forname..lastname@web.de! And I can't lock my neighbor 00025 // out of the services I program, can I? 00026 $ret = $ret && (substr($local_part, 0, 1) !== '.'); 00027 $ret = $ret && (substr($local_part, -1) !== '.'); 00028 // Only a-z, A-Z, 0-9 and !#$%&'*+-/=?^_`{|}~ and . are allowed 00029 // (There is quoting and escaping, but we do not hear, we do not hear, we do not hear...) 00030 $pattern = "@^[a-zA-Z0-9!#$%&'*+\-/=?^_`{|}~.]+$@s"; 00031 $ret = $ret && preg_match($pattern, strtr($local_part, "\r\n", ' ')); 00032 00033 return $ret; 00034 }
Check if value is a int within given range.
- Parameters:
-
mixed The value to check mixed Min value or false to not check for min value mixed Max value or false to not check for max value
- Returns:
- bool
Definition at line 117 of file validation.cls.php.
00117 { 00118 $ret = ($value == strval(intval($value))); 00119 if ($ret) { 00120 $value = Cast::int($value); 00121 if ($min !== false) { 00122 $ret = $ret && ($value >= $min); 00123 } 00124 if ($max !== false) { 00125 $ret = $ret && ($value <= $max); 00126 } 00127 } 00128 return $ret; 00129 }
static Validation::is_ip | ( | &$ | ip | ) | [static] |
Check if given string is either a IPv4 or IPv6.
Definition at line 190 of file validation.cls.php.
00190 { 00191 return self::is_ip4($ip) || self::is_ip6($ip); 00192 }
static Validation::is_ip4 | ( | &$ | ip | ) | [static] |
Check if $ip is a valid IPv4 address in dotted decimal format.
If the validation succeeds, the given string is replaced by a _very_ well formatted version, that will even pass tests that check for 3 digits
Example: 192.0168.1.2 is valid, but a lot of IP tests will fail because of 0168. This function will pass, and set $ip to 192.168.1.2
- Attention:
- Note that Octal, Hexadecimal, Decimal, Dotted Hexadecimal and Dotted Octal are not supported. They are rarely used, though
Definition at line 143 of file validation.cls.php.
00143 { 00144 // Using filter_var here fails in recognizing 255.255.255.0255 as a valid IP 00145 // See http://en.wikipedia.org/wiki/IPv4#Address_representations 00146 $test = explode('.', $ip); 00147 $ints = array(); 00148 $ret = (count($test) == 4); 00149 foreach($test as $datum) { 00150 $ret = $ret && self::is_int($datum, 0, 255); 00151 if ($ret) { $ints[] = intval($datum); } 00152 } 00153 00154 $regex = '@^[0.]*$@'; // Contains only ., and 0 00155 $ret = $ret && !preg_match($regex, $ip); 00156 00157 if ($ret) { 00158 $ip = implode('.', $ints); 00159 } 00160 00161 return $ret; 00162 }
static Validation::is_ip6 | ( | $ | ip | ) | [static] |
Checks if given string is an IPv6.
Definition at line 167 of file validation.cls.php.
00167 { 00168 $ret = false; 00169 if (function_exists('filter_var')) { 00170 // This regards :: as valid, also ::0 or ::0.0.0.0 as OK, which is wrong 00171 $ret = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6); 00172 } 00173 else { 00174 // This regards :: as invalid, but ::0 or ::0.0.0.0 as OK, which is wrong 00175 // Taken from here: http://regexlib.com/REDetails.aspx?regexp_id=1000 00176 $regex = "@^((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){1,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){1}:([0-9A-Fa-f]{1,4}:){0,4}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,2}:([0-9A-Fa-f]{1,4}:){0,3}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,3}:([0-9A-Fa-f]{1,4}:){0,2}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,4}:([0-9A-Fa-f]{1,4}:){1}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))$@"; 00177 $ret = preg_match($regex, $ip); 00178 } 00179 if ($ret) { 00180 // :: in any combination (::, ::0, 0::0:0.0.0.0) is invalid! 00181 $regex = '@^[0.:]*$@'; // Contains only ., :, and 0 00182 $ret = !preg_match($regex, $ip); 00183 } 00184 return $ret; 00185 }
static Validation::is_string_of_length | ( | $ | value, | |
$ | min_length = 0 , |
|||
$ | max_length = -1 |
|||
) | [static] |
Check if value is a string and validate its length.
- Parameters:
-
Definition at line 66 of file validation.cls.php.
00066 { 00067 if (is_null($value)) { 00068 $value = ''; 00069 } 00070 $ret = is_string($value); 00071 if ($ret) { 00072 $ret = $ret && ( String::length($value) >= $min_length); 00073 if ($max_length > 0) { 00074 $ret = $ret && ( String::length($value) <= $max_length); 00075 } 00076 } 00077 return $ret; 00078 }
static Validation::is_url | ( | $ | value | ) | [static] |
Check if value is an url.
Definition at line 83 of file validation.cls.php.
00083 { 00084 $test = new Url($value); 00085 return $test->is_valid(); 00086 }
The documentation for this class was generated from the following file:
- gyro/core/lib/helpers/validation.cls.php