Permalink
Browse files

Refactor string validator

  • Loading branch information...
1 parent 0546f7e commit 2c0ae40b9b8886a74f7615c77391f4f7fe546d22 @c9s committed Sep 8, 2012
Showing with 26 additions and 22 deletions.
  1. +26 −22 src/ValidationKit/StringValidator.php
@@ -34,57 +34,61 @@ public function __construct($options = array(), $messages = array())
public function validate($value)
{
- $ret = 1;
if( $is = $this->getOption('is') ) {
- if( isset($this->options['ignore_case']) ) {
- $ret = $ret && (strlen( $is ) === strlen( $value )
- && stripos($value,$is) === 0 );
+ if( $this->getOption('ignore_case') ) {
+ if (strlen($is) !== strlen( $value )
+ || stripos($value,$is) !== 0 )
+ return $this->invalid('is_error');
} else {
- $ret = $ret && (strlen( $is ) === strlen( $value )
- && strpos($value,$is) === 0 );
+ if (strlen( $is ) !== strlen( $value )
+ || strpos($value,$is) !== 0 )
+ return $this->invalid('is_error');
}
}
if( $startWith = $this->getOption('starts_with') ) {
if( $this->getOption('ignore_case') ) {
- $ret = $ret && stripos( $value,$startWith ) === 0;
+ if( stripos( $value,$startWith ) !== 0 )
+ return $this->invalid('starts_with_error');
} else {
- $ret = $ret && strpos( $value,$startWith ) === 0;
+ if( strpos( $value,$startWith ) !== 0 )
+ return $this->invalid('ends_with_error');
}
}
if( $endWith = $this->getOption('ends_with') ) {
- $len = strlen( $endWith );
- $pos = strlen( $value ) - $len;
+ $len = strlen($endWith);
+ $pos = strlen($value) - $len;
if( $this->getOption('ignore_case') ) {
- $ret = $ret && strripos($value, $endWith) === $pos;
+ if( strripos($value, $endWith) !== $pos )
+ return $this->invalid('ends_with_error');
} else {
- $ret = $ret && strrpos($value, $endWith) === $pos;
+ if( strrpos($value, $endWith) !== $pos )
+ return $this->invalid('ends_with_error');
}
}
if( $contains = $this->getOption('contains') ) {
if( $this->getOption('ignore_case') ) {
- $ret = $ret && strripos( $value, $contains ) !== false;
+ if( strripos( $value, $contains ) === false )
+ return $this->invalid('contains_error');
} else {
- $ret = $ret && strrpos( $value, $contains ) !== false;
+ if( strrpos( $value, $contains ) === false )
+ return $this->invalid('contains_error');
}
}
if( $except = $this->getOption('except') ) {
if( $this->getOption('ignore_case') ) {
- $ret = $ret && stripos($value, $except) === false;
+ if( stripos($value, $except) !== false)
+ return $this->invalid('except_error');
} else {
- $ret = $ret && strpos($value, $except) === false;
+ if( strpos($value, $except) !== false)
+ return $this->invalid('except_error');
}
}
-
- if( $ret === 1 ) {
- throw new Exception("Nothing compared, empty option?");
- } else {
- return $this->saveResult( $ret );
- }
+ return $this->valid();
}
}

0 comments on commit 2c0ae40

Please sign in to comment.