Browse files

Add StringLengthValidator

  • Loading branch information...
1 parent 61e0426 commit bb449b05de92fe58a8f67caba3250196ca4b643f @c9s committed Sep 8, 2012
Showing with 37 additions and 0 deletions.
  1. +20 −0 src/ValidationKit/StringLengthValidator.php
  2. +17 −0 tests/ValidationKit/StringLengthValidatorTest.php
View
20 src/ValidationKit/StringLengthValidator.php
@@ -0,0 +1,20 @@
+<?php
+namespace ValidationKit;
+
+class StringLengthValidator extends Validator
+{
+ public function validate($val)
+ {
+ if($max = $this->getOption('max')) {
+ if(strlen($val) > $max)
+ return $this->invalid('max_error');
+ }
+ if($min = $this->getOption('min')) {
+ if(strlen($val) < $min)
+ return $this->invalid('min_error');
+ }
+ return $this->valid('valid');
+ }
+}
+
+
View
17 tests/ValidationKit/StringLengthValidatorTest.php
@@ -0,0 +1,17 @@
+<?php
+
+class StringLengthValidatorTest extends PHPUnit_Framework_TestCase
+{
+ function test()
+ {
+ $v = new ValidationKit\StringLengthValidator(array(
+ 'min' => 5,
+ 'max' => 10,
+ ));
+ ok($v);
+ not_ok($v->validate(str_repeat('x',3)));
+ ok($v->validate(str_repeat('x',10)));
+ not_ok($v->validate(str_repeat('x',11)));
+ }
+}
+

0 comments on commit bb449b0

Please sign in to comment.