Added first attempt at input validation framework
This commit is contained in:
@@ -44,5 +44,8 @@ class SihnonFramework_Exception_AuthException extends SihnonFramework_E
|
||||
class SihnonFramework_Exception_UnknownUser extends SihnonFramework_Exception_AuthException {};
|
||||
class SihnonFramework_Exception_IncorrectPassword extends SihnonFramework_Exception_AuthException {};
|
||||
|
||||
class SihnonFramework_Exception_ValidationException extends SihnonFramework_Exception {};
|
||||
class SihnonFramework_Exception_InvalidContent extends SihnonFramework_ValidationException {};
|
||||
class SihnonFramework_Exception_InvalidLength extends SihnonFramework_ValidationoException {};
|
||||
|
||||
?>
|
||||
|
||||
9
source/lib/SihnonFramework/Validation.class.php
Normal file
9
source/lib/SihnonFramework/Validation.class.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
abstract class SihnonFramework_Validation {
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
55
source/lib/SihnonFramework/Validation/Text.class.php
Normal file
55
source/lib/SihnonFramework/Validation/Text.class.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
class SihnonFramework_Validation_Text extends SihnonFramework_Validation {
|
||||
|
||||
const Defaults = 0xff;
|
||||
const Alphabetical = 0x01;
|
||||
const Digit = 0x02;
|
||||
const Numeric = 0x04;
|
||||
const Symbol = 0x08;
|
||||
const Whitespace = 0x16;
|
||||
|
||||
protected static $charsets = array(
|
||||
static::Alphabetical => ':alpha:',
|
||||
static::Digit => ':digit:',
|
||||
static::Numeric => ':digit:\.-',
|
||||
static::Symbol => ':punct:',
|
||||
static::Whitespace => ':space:',
|
||||
);
|
||||
|
||||
public static function charset($input, $charset = static::Defaults) {
|
||||
static::pattern($input, static::buildCharsetPattern($charset));
|
||||
}
|
||||
|
||||
public static function length($input, $min_length = null, $max_length = null) {
|
||||
$length = strlen($input);
|
||||
|
||||
if ($min_length !== null && $length < $min_length) {
|
||||
throw new SihnonFramework_Exception_InvalidLength();
|
||||
}
|
||||
|
||||
if ($max_length !== null && $length > $max_length) {
|
||||
throw new SihnonFramework_Exception_InvalidLength();
|
||||
}
|
||||
}
|
||||
|
||||
public static function pattern($input, $pattern) {
|
||||
if ( ! preg_match($pattern, $input)) {
|
||||
throw new SihnonFramework_Exception_InvalidContent();
|
||||
}
|
||||
}
|
||||
|
||||
protected static function buildCharsetPattern($charset) {
|
||||
$classes = '';
|
||||
foreach (static::$charsets as $set => $class) {
|
||||
if ($charset & $set) {
|
||||
$classes .= $class;
|
||||
}
|
||||
}
|
||||
|
||||
return "/^[{$classes}]*$/";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user