From 70ff3b261f0d354c43878f898d075f72b7ebb2a8 Mon Sep 17 00:00:00 2001 From: zYne Date: Sun, 2 Sep 2007 20:00:27 +0000 Subject: [PATCH] refactored Validator API --- lib/Doctrine/Validator.php | 19 +++-- lib/Doctrine/Validator/Country.php | 10 ++- lib/Doctrine/Validator/Creditcard.php | 10 +-- lib/Doctrine/Validator/Date.php | 10 +-- lib/Doctrine/Validator/Driver.php | 8 -- lib/Doctrine/Validator/Email.php | 35 ++++---- lib/Doctrine/Validator/ErrorStack.php | 5 -- lib/Doctrine/Validator/Htmlcolor.php | 7 +- lib/Doctrine/Validator/Ip.php | 7 +- lib/Doctrine/Validator/Minlength.php | 98 +++++++++++----------- lib/Doctrine/Validator/Nospace.php | 11 ++- lib/Doctrine/Validator/Notblank.php | 12 +-- lib/Doctrine/Validator/Notnull.php | 10 +-- lib/Doctrine/Validator/Range.php | 11 ++- lib/Doctrine/Validator/Regexp.php | 19 +++-- lib/Doctrine/Validator/Unique.php | 18 ++-- lib/Doctrine/Validator/Unsigned.php | 12 +-- lib/Doctrine/Validator/Usstate.php | 113 +++++++++++++------------- 18 files changed, 201 insertions(+), 214 deletions(-) diff --git a/lib/Doctrine/Validator.php b/lib/Doctrine/Validator.php index 16014fb77..325210a7d 100644 --- a/lib/Doctrine/Validator.php +++ b/lib/Doctrine/Validator.php @@ -136,7 +136,11 @@ class Doctrine_Validator extends Doctrine_Object } $validator = self::getValidator($name); - if ( ! $validator->validate($record, $key, $value, $args)) { + $validator->invoker = $record; + $validator->field = $key; + $validator->args = $args; + + if ( ! $validator->validate($value)) { $errorStack->add($key, $name); //$err[$key] = 'not valid'; @@ -182,14 +186,15 @@ class Doctrine_Validator extends Doctrine_Object return (count($this->stack) > 0); } /** + * phpType * converts a doctrine type to native php type * - * @param $doctrineType + * @param $portableType portable doctrine type * @return string */ - public static function phpType($doctrineType) + public static function phpType($portableType) { - switch ($doctrineType) { + switch ($portableType) { case 'enum': return 'integer'; case 'blob': @@ -201,7 +206,7 @@ class Doctrine_Validator extends Doctrine_Object return 'string'; break; default: - return $doctrineType; + return $portableType; } } /** @@ -236,7 +241,7 @@ class Doctrine_Validator extends Doctrine_Object case 'NULL': return true; break; - }; + } } /** * returns the type of loosely typed variable @@ -259,6 +264,6 @@ class Doctrine_Validator extends Doctrine_Object break; default: return $type; - }; + } } } diff --git a/lib/Doctrine/Validator/Country.php b/lib/Doctrine/Validator/Country.php index fc75580e8..8cef1570d 100644 --- a/lib/Doctrine/Validator/Country.php +++ b/lib/Doctrine/Validator/Country.php @@ -277,6 +277,8 @@ class Doctrine_Validator_Country 'zr' => 'Zaire', 'zw' => 'Zimbabwe'); /** + * returns all available country codes + * * @return array */ public static function getCountries() @@ -284,15 +286,15 @@ class Doctrine_Validator_Country return self::$countries; } /** - * @param Doctrine_Record $record - * @param string $key + * checks if given value is a valid country code + * * @param mixed $value - * @param string $args * @return boolean */ - public function validate(Doctrine_Record $record, $key, $value, $args) + public function validate($value) { $value = strtolower($value); + return isset(self::$countries[$value]); } diff --git a/lib/Doctrine/Validator/Creditcard.php b/lib/Doctrine/Validator/Creditcard.php index 91768c382..56e5b80a1 100644 --- a/lib/Doctrine/Validator/Creditcard.php +++ b/lib/Doctrine/Validator/Creditcard.php @@ -31,17 +31,15 @@ * @author Konsta Vesterinen */ class Doctrine_Validator_Creditcard -{ - +{ /** + * checks if given value is a valid credit card number + * * @link http://www.owasp.org/index.php/OWASP_Validation_Regex_Repository - * @param Doctrine_Record $record - * @param string $key * @param mixed $value - * @param string $args * @return boolean */ - public function validate(Doctrine_Record $record, $key, $value, $args) + public function validate($value) { return preg_match('#^((4\d{3})|(5[1-5]\d{2})|(6011)|(7\d{3}))-?\d{4}-?\d{4}-?\d{4}|3[4,7]\d{13}$#', $value); } diff --git a/lib/Doctrine/Validator/Date.php b/lib/Doctrine/Validator/Date.php index fb38a30a7..388a99710 100644 --- a/lib/Doctrine/Validator/Date.php +++ b/lib/Doctrine/Validator/Date.php @@ -33,18 +33,18 @@ class Doctrine_Validator_Date { /** - * @param Doctrine_Record $record - * @param string $key + * checks if given value is a valid date + * * @param mixed $value - * @param string $args * @return boolean */ - public function validate(Doctrine_Record $record, $key, $value, $args) + public function validate($value) { if (empty($value)) { return true; } - $e = explode("-", $value); + $e = explode('-', $value); + if (count($e) !== 3) { return false; } diff --git a/lib/Doctrine/Validator/Driver.php b/lib/Doctrine/Validator/Driver.php index 7199be853..8e6a7d17e 100644 --- a/lib/Doctrine/Validator/Driver.php +++ b/lib/Doctrine/Validator/Driver.php @@ -67,10 +67,6 @@ class Doctrine_Validator_Driver */ public function __set($arg, $value) { - if ( ! isset($this->_args[$arg])) { - throw new Doctrine_Plugin_Exception('Unknown argument ' . $arg); - } - $this->_args[$arg] = $value; return $this; @@ -98,10 +94,6 @@ class Doctrine_Validator_Driver */ public function setArg($arg, $value) { - if ( ! isset($this->_args[$arg])) { - throw new Doctrine_Plugin_Exception('Unknown argument ' . $arg); - } - $this->_args[$arg] = $value; return $this; diff --git a/lib/Doctrine/Validator/Email.php b/lib/Doctrine/Validator/Email.php index 0fda141fb..e19e57547 100644 --- a/lib/Doctrine/Validator/Email.php +++ b/lib/Doctrine/Validator/Email.php @@ -33,22 +33,21 @@ class Doctrine_Validator_Email { /** + * checks if given value is a valid email address + * * @link http://iamcal.com/publish/articles/php/parsing_email/pdf/ - * @param Doctrine_Record $record - * @param string $key * @param mixed $value - * @param string $args * @return boolean */ - public function validate(Doctrine_Record $record, $key, $value, $args) + public function validate($value) { if (empty($value)) { return true; } - if (isset($args[0])) { - $parts = explode("@", $value); - if (isset($parts[1]) && function_exists("checkdnsrr")) { - if ( ! checkdnsrr($parts[1], "MX")) { + if (isset($this->args)) { + $parts = explode('@', $value); + if (isset($parts[1]) && function_exists('checkdnsrr')) { + if ( ! checkdnsrr($parts[1], 'MX')) { return false; } } @@ -57,24 +56,24 @@ class Doctrine_Validator_Email $qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]'; $dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]'; $atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+'; - $quoted_pair = '\\x5c[\\x00-\\x7f]'; - $domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d"; - $quoted_string = "\\x22($qtext|$quoted_pair)*\\x22"; + $quotedPair = '\\x5c[\\x00-\\x7f]'; + $domainLiteral = "\\x5b($dtext|$quotedPair)*\\x5d"; + $quotedString = "\\x22($qtext|$quotedPair)*\\x22"; $domain_ref = $atom; - $sub_domain = "($domain_ref|$domain_literal)"; - $word = "($atom|$quoted_string)"; - $domain = "$sub_domain(\\x2e$sub_domain)+"; + $subDomain = "($domain_ref|$domainLiteral)"; + $word = "($atom|$quotedString)"; + $domain = "$subDomain(\\x2e$subDomain)+"; /* - following psudocode to allow strict checking - ask pookey about this if you're puzzled + following pseudocode to allow strict checking - ask pookey about this if you're puzzled if ($this->getValidationOption('strict_checking') == true) { $domain = "$sub_domain(\\x2e$sub_domain)*"; } */ - $local_part = "$word(\\x2e$word)*"; - $addr_spec = "$local_part\\x40$domain"; + $localPart = "$word(\\x2e$word)*"; + $addrSpec = "$localPart\\x40$domain"; - return (bool)preg_match("!^$addr_spec$!D", $value); + return (bool) preg_match("!^$addrSpec$!D", $value); } } diff --git a/lib/Doctrine/Validator/ErrorStack.php b/lib/Doctrine/Validator/ErrorStack.php index cb0e368c3..9797fc7ca 100644 --- a/lib/Doctrine/Validator/ErrorStack.php +++ b/lib/Doctrine/Validator/ErrorStack.php @@ -109,8 +109,6 @@ class Doctrine_Validator_ErrorStack extends Doctrine_Access implements Countable $this->errors = array(); } - /** IteratorAggregate implementation */ - /** * Enter description here... * @@ -120,9 +118,6 @@ class Doctrine_Validator_ErrorStack extends Doctrine_Access implements Countable { return new ArrayIterator($this->errors); } - - /** Countable implementation */ - /** * Enter description here... * diff --git a/lib/Doctrine/Validator/Htmlcolor.php b/lib/Doctrine/Validator/Htmlcolor.php index ae6d00f10..9d72f9379 100644 --- a/lib/Doctrine/Validator/Htmlcolor.php +++ b/lib/Doctrine/Validator/Htmlcolor.php @@ -33,13 +33,12 @@ class Doctrine_Validator_HtmlColor { /** - * @param Doctrine_Record $record - * @param string $key + * checks if given value is a valid html color code + * * @param mixed $value - * @param string $args * @return boolean */ - public function validate(Doctrine_Record $record, $key, $value, $args) + public function validate($value) { if ( ! preg_match("/^#{0,1}[0-9]{6}$/", $value)) { return false; diff --git a/lib/Doctrine/Validator/Ip.php b/lib/Doctrine/Validator/Ip.php index f4b10c0b6..d10fc11bf 100644 --- a/lib/Doctrine/Validator/Ip.php +++ b/lib/Doctrine/Validator/Ip.php @@ -33,13 +33,12 @@ class Doctrine_Validator_Ip { /** - * @param Doctrine_Record $record - * @param string $key + * checks if given value is valid ip address + * * @param mixed $value - * @param string $args * @return boolean */ - public function validate(Doctrine_Record $record, $key, $value, $args) + public function validate($value) { return (bool) ip2long(str_replace("\0", '', $value)); } diff --git a/lib/Doctrine/Validator/Minlength.php b/lib/Doctrine/Validator/Minlength.php index 7a3636020..999c8c1de 100644 --- a/lib/Doctrine/Validator/Minlength.php +++ b/lib/Doctrine/Validator/Minlength.php @@ -1,48 +1,50 @@ -. - */ - -/** - * Doctrine_Validator_Regexp - * - * @package Doctrine - * @category Object Relational Mapping - * @license http://www.opensource.org/licenses/lgpl-license.php LGPL - * @link www.phpdoctrine.com - * @since 1.0 - * @version $Revision$ - * @author Gijs van Dulmen - */ -class Doctrine_Validator_Minlength { - /** - * @param Doctrine_Record $record - * @param string $key - * @param mixed $value - * @param string $args - * @return boolean - */ - public function validate(Doctrine_Record $record, $key, $value, $args) { - if(isset($args) && strlen( $value ) < $args) - return false; - - return true; - } -} - +. + */ + +/** + * Doctrine_Validator_Regexp + * + * @package Doctrine + * @category Object Relational Mapping + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.phpdoctrine.com + * @since 1.0 + * @version $Revision$ + * @author Gijs van Dulmen + */ +class Doctrine_Validator_Minlength +{ + /** + * checks if given value is more length than the minimum length required + * + * @param mixed $value + * @return boolean + */ + public function validate($value) + { + if (isset($this->args) && strlen($value) < $this->args) { + return false; + } + + return true; + } +} + diff --git a/lib/Doctrine/Validator/Nospace.php b/lib/Doctrine/Validator/Nospace.php index 8fbfb6afc..ff8f72f31 100644 --- a/lib/Doctrine/Validator/Nospace.php +++ b/lib/Doctrine/Validator/Nospace.php @@ -30,17 +30,16 @@ * @version $Revision$ * @author Konsta Vesterinen */ -class Doctrine_Validator_Nospace +class Doctrine_Validator_Nospace extends Doctrine_Validator_Driver { /** - * @param Doctrine_Record $record - * @param string $key + * checks that value doesn't contain any space chars + * * @param mixed $value - * @param string $args * @return boolean */ - public function validate(Doctrine_Record $record, $key, $value, $args) + public function validate($value) { - return ($value === null || ! preg_match('/\s\t\r\n/',$value)); + return ($value === null || ! preg_match('/\s\t\r\n/', $value)); } } diff --git a/lib/Doctrine/Validator/Notblank.php b/lib/Doctrine/Validator/Notblank.php index f87d4299c..a2bc3ef9e 100644 --- a/lib/Doctrine/Validator/Notblank.php +++ b/lib/Doctrine/Validator/Notblank.php @@ -30,17 +30,17 @@ * @version $Revision$ * @author Konsta Vesterinen */ -class Doctrine_Validator_Notblank +class Doctrine_Validator_Notblank extends Doctrine_Validator_Driver { /** - * @param Doctrine_Record $record - * @param string $key + * checks that value isn't blank + * a value is blank when its either null or contains only space characters + * * @param mixed $value - * @param string $args * @return boolean */ - public function validate(Doctrine_Record $record, $key, $value, $args) + public function validate($value) { - return (trim($value) != ''); + return (trim($value) !== '' && $value !== null); } } diff --git a/lib/Doctrine/Validator/Notnull.php b/lib/Doctrine/Validator/Notnull.php index b7b131c95..376e6d20a 100644 --- a/lib/Doctrine/Validator/Notnull.php +++ b/lib/Doctrine/Validator/Notnull.php @@ -30,16 +30,16 @@ * @version $Revision$ * @author Konsta Vesterinen */ -class Doctrine_Validator_Notnull +class Doctrine_Validator_Notnull extends Doctrine_Validator_Driver { /** - * @param Doctrine_Record $record - * @param string $key + * checks that given value isn't null + * * @param mixed $value * @return boolean */ - public function validate(Doctrine_Record $record, $key, $value) + public function validate($value) { - return !is_null($value); + return ($value !== null); } } diff --git a/lib/Doctrine/Validator/Range.php b/lib/Doctrine/Validator/Range.php index b103c255c..4c429e646 100644 --- a/lib/Doctrine/Validator/Range.php +++ b/lib/Doctrine/Validator/Range.php @@ -33,18 +33,17 @@ class Doctrine_Validator_Range { /** - * @param Doctrine_Record $record - * @param string $key + * checks if value is within given range + * * @param mixed $value - * @param string $args * @return boolean */ - public function validate(Doctrine_Record $record, $key, $value, $args) + public function validate($value) { - if (isset($args[0]) && $value < $args[0]) { + if (isset($this->args[0]) && $value < $this->args[0]) { return false; } - if (isset($args[1]) && $value > $args[1]) { + if (isset($this->args[1]) && $value > $this->args[1]) { return false; } return true; diff --git a/lib/Doctrine/Validator/Regexp.php b/lib/Doctrine/Validator/Regexp.php index 18d00c264..0aca2d195 100644 --- a/lib/Doctrine/Validator/Regexp.php +++ b/lib/Doctrine/Validator/Regexp.php @@ -33,23 +33,26 @@ class Doctrine_Validator_Regexp { /** - * @param Doctrine_Record $record - * @param string $key + * checks if given value satisfies a regular expression + * * @param mixed $value - * @param string $args + * @param mixed $args * @return boolean */ - public function validate(Doctrine_Record $record, $key, $value, $args) + public function validate($value) { - if (is_array($args)) { - foreach ($args as $regexp) { - if ( ! preg_match($args, $value)) { + if ( ! isset($this->args)) { + return true; + } + if (is_array($this->args)) { + foreach ($this->args as $regexp) { + if ( ! preg_match($regexp, $value)) { return false; } } return true; } else { - if (preg_match($args, $value)) { + if (preg_match($this->args, $value)) { return true; } } diff --git a/lib/Doctrine/Validator/Unique.php b/lib/Doctrine/Validator/Unique.php index d4135b127..5e92f1eb5 100644 --- a/lib/Doctrine/Validator/Unique.php +++ b/lib/Doctrine/Validator/Unique.php @@ -33,21 +33,21 @@ class Doctrine_Validator_Unique { /** - * @param Doctrine_Record $record - * @param string $key + * checks if given value is unique + * * @param mixed $value - * @param string $args * @return boolean */ - public function validate(Doctrine_Record $record, $key, $value, $args) + public function validate($value) { - $table = $record->getTable(); + $table = $this->invoker->getTable(); $pks = $table->getIdentifier(); + if ( is_array($pks) ) { - $pks = join(',',$pks); + $pks = join(',', $pks); } - $sql = 'SELECT ' . $pks . ' FROM ' . $table->getTableName() . ' WHERE ' . $key . ' = ?'; + $sql = 'SELECT ' . $pks . ' FROM ' . $table->getTableName() . ' WHERE ' . $this->field . ' = ?'; $values = array(); $values[] = $value; @@ -55,11 +55,11 @@ class Doctrine_Validator_Unique // If the record is not new we need to add primary key checks because its ok if the // unique value already exists in the database IF the record in the database is the same // as the one that is validated here. - $state = $record->state(); + $state = $this->invoker->state(); if (! ($state == Doctrine_Record::STATE_TDIRTY || $state == Doctrine_Record::STATE_TCLEAN)) { foreach ($table->getPrimaryKeys() as $pk) { $sql .= " AND {$pk} != ?"; - $values[] = $record->$pk; + $values[] = $this->invoker->$pk; } } diff --git a/lib/Doctrine/Validator/Unsigned.php b/lib/Doctrine/Validator/Unsigned.php index 038586298..27cd5dfc4 100644 --- a/lib/Doctrine/Validator/Unsigned.php +++ b/lib/Doctrine/Validator/Unsigned.php @@ -20,7 +20,7 @@ */ /** - * Doctrine_Validator_Enum + * Doctrine_Validator_Unsigned * * @package Doctrine * @category Object Relational Mapping @@ -33,22 +33,18 @@ class Doctrine_Validator_Unsigned { /** - * @param Doctrine_Record $record - * @param string $key + * checks if given value is a valid unsigned integer + * * @param mixed $value - * @param string $args * @return boolean */ - public function validate(Doctrine_Record $record, $key, $value, $args) + public function validate($value) { $int = (int) $value; if ($int != $value || $int < 0) { return false; } - if ($int < 0) { - return false; - } return true; } } diff --git a/lib/Doctrine/Validator/Usstate.php b/lib/Doctrine/Validator/Usstate.php index 31f001994..13874d04b 100644 --- a/lib/Doctrine/Validator/Usstate.php +++ b/lib/Doctrine/Validator/Usstate.php @@ -33,72 +33,71 @@ class Doctrine_Validator_Usstate { private static $states = array ( - "AK" => true, - "AL" => true, - "AR" => true, - "AZ" => true, - "CA" => true, - "CO" => true, - "CT" => true, - "DC" => true, - "DE" => true, - "FL" => true, - "GA" => true, - "HI" => true, - "IA" => true, - "ID" => true, - "IL" => true, - "IN" => true, - "KS" => true, - "KY" => true, - "LA" => true, - "MA" => true, - "MD" => true, - "ME" => true, - "MI" => true, - "MN" => true, - "MO" => true, - "MS" => true, - "MT" => true, - "NC" => true, - "ND" => true, - "NE" => true, - "NH" => true, - "NJ" => true, - "NM" => true, - "NV" => true, - "NY" => true, - "OH" => true, - "OK" => true, - "OR" => true, - "PA" => true, - "PR" => true, - "RI" => true, - "SC" => true, - "SD" => true, - "TN" => true, - "TX" => true, - "UT" => true, - "VA" => true, - "VI" => true, - "VT" => true, - "WA" => true, - "WI" => true, - "WV" => true, - "WY" => true + 'AK' => true, + 'AL' => true, + 'AR' => true, + 'AZ' => true, + 'CA' => true, + 'CO' => true, + 'CT' => true, + 'DC' => true, + 'DE' => true, + 'FL' => true, + 'GA' => true, + 'HI' => true, + 'IA' => true, + 'ID' => true, + 'IL' => true, + 'IN' => true, + 'KS' => true, + 'KY' => true, + 'LA' => true, + 'MA' => true, + 'MD' => true, + 'ME' => true, + 'MI' => true, + 'MN' => true, + 'MO' => true, + 'MS' => true, + 'MT' => true, + 'NC' => true, + 'ND' => true, + 'NE' => true, + 'NH' => true, + 'NJ' => true, + 'NM' => true, + 'NV' => true, + 'NY' => true, + 'OH' => true, + 'OK' => true, + 'OR' => true, + 'PA' => true, + 'PR' => true, + 'RI' => true, + 'SC' => true, + 'SD' => true, + 'TN' => true, + 'TX' => true, + 'UT' => true, + 'VA' => true, + 'VI' => true, + 'VT' => true, + 'WA' => true, + 'WI' => true, + 'WV' => true, + 'WY' => true ); public function getStates() { return self::$states; } /** - * @param Doctrine_Record $record - * @param string $key - * @param mixed $value + * checks if given value is a valid US state code + * * @param string $args * @return boolean */ - public function validate(Doctrine_Record $record, $key, $value, $args) + public function validate($value) { return isset(self::$states[$value]); }