From 276af65256aa9f26411a565261dcea2464c8d7c5 Mon Sep 17 00:00:00 2001 From: zYne Date: Mon, 4 Sep 2006 06:55:16 +0000 Subject: [PATCH] Support for passing an array as constraint/validator argument --- Doctrine/DataDict.php | 13 ++++++++++-- Doctrine/Validator.php | 37 ++++++++++++++++++++--------------- Doctrine/Validator/Range.php | 7 +++---- Doctrine/Validator/Regexp.php | 12 ++++++++++-- tests/ValidatorTestCase.php | 6 +++++- tests/classes.php | 3 +++ tests/run.php | 3 +-- 7 files changed, 54 insertions(+), 27 deletions(-) diff --git a/Doctrine/DataDict.php b/Doctrine/DataDict.php index 40c5b88fc..a93f2ae28 100644 --- a/Doctrine/DataDict.php +++ b/Doctrine/DataDict.php @@ -34,8 +34,17 @@ class Doctrine_DataDict { foreach($columns as $name => $args) { if( ! is_array($args[2])) $args[2] = array(); - - $r[] = $name." ".$this->getADOType($args[0],$args[1])." ".implode(' ',$args[2]); + + $constraints = array(); + + foreach($args[2] as $k => $v) { + if(is_string($k)) + $constraints[] = $k; + else + $constraints[] = $v; + } + + $r[] = $name." ".$this->getADOType($args[0],$args[1])." ".implode(' ', $constraints); } diff --git a/Doctrine/Validator.php b/Doctrine/Validator.php index 67d7ca10a..cd008e7ea 100644 --- a/Doctrine/Validator.php +++ b/Doctrine/Validator.php @@ -67,7 +67,10 @@ class Doctrine_Validator { * constant for range validation error */ const ERR_RANGE = 8; - + /** + * constant for regexp validation error + */ + const ERR_REGEXP = 9; @@ -119,8 +122,8 @@ class Doctrine_Validator { * @return void */ public function validateRecord(Doctrine_Record $record) { - $columns = $record->getTable()->getColumns(); - $name = $record->getTable()->getComponentName(); + $columns = $record->getTable()->getColumns(); + $component = $record->getTable()->getComponentName(); switch($record->getState()): case Doctrine_Record::STATE_TDIRTY: @@ -165,21 +168,23 @@ class Doctrine_Validator { foreach($e as $k => $arg) { - if(empty($arg) || $arg == "primary" || $arg == "protected" || $arg == "autoincrement") + if(is_string($k)) { + $name = $k; + $args = $arg; + } else { + $args = explode(":",$arg); + $name = array_shift($args); + if( ! isset($args[0])) + $args[0] = ''; + } + + if(empty($name) || $name == "primary" || $name == "protected" || $name == "autoincrement") continue; - if( ! is_integer($k)) { - $args = $arg; - } else - $args = explode(":",$arg); - - if( ! isset($args[1])) - $args[1] = ''; + $validator = self::getValidator($name); + if( ! $validator->validate($record, $key, $value, $args)) { - $validator = self::getValidator($args[0]); - if( ! $validator->validate($record, $key, $value, $args[1])) { - - $constant = 'Doctrine_Validator::ERR_'.strtoupper($args[0]); + $constant = 'Doctrine_Validator::ERR_'.strtoupper($name); if(defined($constant)) $err[$key] = constant($constant); @@ -197,7 +202,7 @@ class Doctrine_Validator { } if( ! empty($err)) { - $this->stack[$name][] = $err; + $this->stack[$component][] = $err; return false; } diff --git a/Doctrine/Validator/Range.php b/Doctrine/Validator/Range.php index 21d662e27..fd1fb6446 100644 --- a/Doctrine/Validator/Range.php +++ b/Doctrine/Validator/Range.php @@ -8,11 +8,10 @@ class Doctrine_Validator_Range { * @return boolean */ public function validate(Doctrine_Record $record, $key, $value, $args) { - $e = explode("-",$args); - if($value < $e[0]) + if(isset($args[0]) && $value < $args[0]) return false; - - if(isset($e[1]) && $value > $e[1]) + + if(isset($args[1]) && $value > $args[1]) return false; return true; diff --git a/Doctrine/Validator/Regexp.php b/Doctrine/Validator/Regexp.php index c9a0ec39b..a40683b6a 100644 --- a/Doctrine/Validator/Regexp.php +++ b/Doctrine/Validator/Regexp.php @@ -8,9 +8,17 @@ class Doctrine_Validator_Regexp { * @return boolean */ public function validate(Doctrine_Record $record, $key, $value, $args) { - if(preg_match("/$args/", $value)) + if(is_array($args)) { + foreach($args as $regexp) { + if( ! preg_match("/$args/", $value)) + return false; + } return true; - + } else { + if(preg_match("/$args/", $value)) + return true; + } + return false; } } diff --git a/tests/ValidatorTestCase.php b/tests/ValidatorTestCase.php index ddadcec46..f930772fd 100644 --- a/tests/ValidatorTestCase.php +++ b/tests/ValidatorTestCase.php @@ -72,7 +72,9 @@ class Doctrine_ValidatorTestCase extends Doctrine_UnitTestCase { public function testValidate2() { $test = new ValidatorTest(); $test->mymixed = "message"; - + $test->myrange = 1; + $test->myregexp = '123a'; + $validator = new Doctrine_Validator(); $validator->validateRecord($test); @@ -85,6 +87,8 @@ class Doctrine_ValidatorTestCase extends Doctrine_UnitTestCase { $this->assertEqual($stack['mystring'], Doctrine_Validator::ERR_NOTNULL); $this->assertEqual($stack['myemail2'], Doctrine_Validator::ERR_NOTBLANK); + $this->assertEqual($stack['myrange'], Doctrine_Validator::ERR_RANGE); + $this->assertEqual($stack['myregexp'], Doctrine_Validator::ERR_REGEXP); $test->mystring = 'str'; diff --git a/tests/classes.php b/tests/classes.php index 1f6cefca4..900e54b17 100644 --- a/tests/classes.php +++ b/tests/classes.php @@ -397,6 +397,9 @@ class ValidatorTest extends Doctrine_Record { $this->hasColumn("myarray", "array", 1000); $this->hasColumn("myobject", "object", 1000); $this->hasColumn("myinteger", "integer", 11); + $this->hasColumn("myrange", "integer", 11, array('range' => array(4,123))); + $this->hasColumn("myregexp", "string", 5, array('regexp' => '^[0-9]+$')); + $this->hasColumn("myemail", "string", 100, "email"); $this->hasColumn("myemail2", "string", 100, "email|notblank"); } diff --git a/tests/run.php b/tests/run.php index bc4e6de0c..9cf5c444b 100644 --- a/tests/run.php +++ b/tests/run.php @@ -62,8 +62,6 @@ $test->addTestCase(new Doctrine_Filter_TestCase()); $test->addTestCase(new Doctrine_ValueHolder_TestCase()); -$test->addTestCase(new Doctrine_ValidatorTestCase()); - $test->addTestCase(new Doctrine_QueryTestCase()); $test->addTestCase(new Doctrine_RawSql_TestCase()); @@ -76,6 +74,7 @@ $test->addTestCase(new Doctrine_ImportTestCase()); $test->addTestCase(new Doctrine_CollectionTestCase()); +$test->addTestCase(new Doctrine_ValidatorTestCase()); //$test->addTestCase(new Doctrine_Cache_FileTestCase()); //$test->addTestCase(new Doctrine_Cache_SqliteTestCase());