Extend text validation to accept multiple inputs

This commit is contained in:
2011-12-26 00:56:54 +00:00
parent da57ccf8de
commit 990effc237

View File

@@ -17,25 +17,37 @@ class SihnonFramework_Validation_Text extends SihnonFramework_Validation {
static::Whitespace => ':space:', static::Whitespace => ':space:',
); );
public static function charset($input, $charset = static::Defaults) { public static function charset($inputs, $charset = static::Defaults) {
static::pattern($input, static::buildCharsetPattern($charset)); static::pattern($inputs, static::buildCharsetPattern($charset));
} }
public static function length($input, $min_length = null, $max_length = null) { public static function length($inputs, $min_length = null, $max_length = null) {
$length = strlen($input); if ( ! is_array($inputs)) {
$inputs = array($inputs);
if ($min_length !== null && $length < $min_length) {
throw new SihnonFramework_Exception_InvalidLength();
} }
if ($max_length !== null && $length > $max_length) { foreach ($inputs as $input) {
throw new SihnonFramework_Exception_InvalidLength(); $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) { public static function pattern($inputs, $pattern) {
if ( ! preg_match($pattern, $input)) { if ( ! is_array($inputs)) {
throw new SihnonFramework_Exception_InvalidContent(); $inputs = array($inputs);
}
foreach ($inputs as $input) {
if ( ! preg_match($pattern, $input)) {
throw new SihnonFramework_Exception_InvalidContent();
}
} }
} }