Commit 70ff3b26 authored by zYne's avatar zYne

refactored Validator API

parent d8ac77d5
......@@ -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;
};
}
}
}
......@@ -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]);
}
......
......@@ -31,17 +31,15 @@
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
*/
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);
}
......
......@@ -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;
}
......
......@@ -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;
......
......@@ -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);
}
}
......@@ -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...
*
......
......@@ -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;
......
......@@ -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));
}
......
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* 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 <gijs@vandulmen.net>
*/
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;
}
}
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* 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 <gijs@vandulmen.net>
*/
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;
}
}
......@@ -30,17 +30,16 @@
* @version $Revision$
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
*/
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));
}
}
......@@ -30,17 +30,17 @@
* @version $Revision$
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
*/
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);
}
}
......@@ -30,16 +30,16 @@
* @version $Revision$
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
*/
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);
}
}
......@@ -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;
......
......@@ -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;
}
}
......
......@@ -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;
}
}
......
......@@ -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;
}
}
......@@ -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]);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment