Added first attempt at input validation framework

This commit is contained in:
2011-12-26 00:52:10 +00:00
parent 786076b5c6
commit da57ccf8de
3 changed files with 67 additions and 0 deletions

View File

@@ -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 {};
?>

View File

@@ -0,0 +1,9 @@
<?php
abstract class SihnonFramework_Validation {
}
?>

View 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}]*$/";
}
}
?>