Changeset 4045

Show
Ignore:
Timestamp:
03/19/08 14:30:21 (16 months ago)
Author:
romanb
Message:

Ported validator refactorings partly from trunk.

Location:
branches/0.10
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • branches/0.10/lib/Doctrine/Query.php

    r3983 r4045  
    763763 
    764764                                // build sql expression 
     765                                if ($this->_conn instanceof Doctrine_Connection_Oracle) { 
     766                                    $tableAlias = $this->_queryComponents[$componentAlias]['table'] 
     767                                            ->getTableName(); 
     768                                } 
    765769                                $term[0] = $this->_conn->quoteIdentifier($tableAlias) 
    766770                                         . '.' 
     
    803807                                    if ($this->getType() === Doctrine_Query::SELECT) { 
    804808                                        // build sql expression 
     809                                        if ($this->_conn instanceof Doctrine_Connection_Oracle) { 
     810                                            $tableAlias = $this->_queryComponents[$componentAlias]['table'] 
     811                                                    ->getTableName(); 
     812                                        } 
    805813                                        $term[0] = $this->_conn->quoteIdentifier($tableAlias) 
    806814                                                 . '.' 
  • branches/0.10/lib/Doctrine/Relation/Association.php

    r3884 r4045  
    9191            $coll = new Doctrine_Collection($this->getTable()); 
    9292        } else { 
    93             $coll = Doctrine_Query::create()->query($this->getRelationDql(1), array($id)); 
     93            $coll = $this->getTable()->getConnection()->query($this->getRelationDql(1), array($id)); 
    9494        } 
    9595        $coll->setReference($record, $this); 
  • branches/0.10/lib/Doctrine/Table.php

    r3977 r4045  
    18581858     
    18591859    /** 
     1860     * Gets the names of all validators that are applied on a field. 
     1861     * 
     1862     * @param string  The field name. 
     1863     * @return array  The names of all validators that are applied on the specified field. 
     1864     */ 
     1865    public function getFieldValidators($fieldName) 
     1866    { 
     1867        $validators = array(); 
     1868        $columnName = $this->getColumnName($fieldName); 
     1869        // this loop is a dirty workaround to get the validators filtered out of 
     1870        // the options, since everything is squeezed together currently 
     1871        foreach ($this->_columns[$columnName] as $name => $args) { 
     1872             if (empty($name) 
     1873                    || $name == 'primary' 
     1874                    || $name == 'protected' 
     1875                    || $name == 'autoincrement' 
     1876                    || $name == 'default' 
     1877                    || $name == 'values' 
     1878                    || $name == 'sequence' 
     1879                    || $name == 'zerofill' 
     1880                    || $name == 'owner' 
     1881                    || $name == 'scale' 
     1882                    || $name == 'type' 
     1883                    || $name == 'length') { 
     1884                continue; 
     1885            } 
     1886            if (strtolower($name) === 'notnull' && isset($this->_columns[$columnName]['autoincrement'])) { 
     1887                continue; 
     1888            } 
     1889            $validators[$name] = $args; 
     1890        } 
     1891         
     1892        return $validators; 
     1893    } 
     1894     
     1895    /** 
     1896     * Gets the (maximum) length of a field. 
     1897     */ 
     1898    public function getFieldLength($fieldName) 
     1899    { 
     1900        return $this->_columns[$this->getColumnName($fieldName)]['length']; 
     1901    } 
     1902     
     1903    /** 
    18601904     * getBoundQueryPart 
    18611905     * 
  • branches/0.10/lib/Doctrine/Validator.php

    r3975 r4045  
    2222/** 
    2323 * Doctrine_Validator 
    24  * 
    25  * Doctrine_Validator performs validations in record properties 
     24 * Doctrine_Validator performs validations on record properties 
    2625 * 
    2726 * @package     Doctrine 
     
    3130 * @since       1.0 
    3231 * @version     $Revision$ 
     32 * @author      Roman Borschel <roman@code-factory.org> 
    3333 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi> 
    3434 */ 
     
    3838     * @var array $validators           an array of validator objects 
    3939     */ 
    40     private static $validators  = array(); 
     40    private static $validators = array(); 
    4141 
    4242    /** 
     
    5252            if (class_exists($class)) { 
    5353                self::$validators[$name] = new $class; 
     54            } else if (class_exists($name)) { 
     55                self::$validators[$name] = new $name; 
    5456            } else { 
    5557                throw new Doctrine_Exception("Validator named '$name' not available."); 
     
    6971    public function validateRecord(Doctrine_Record $record) 
    7072    { 
    71         $columns   = $record->getTable()->getColumns(); 
    72         $component = $record->getTable()->getComponentName(); 
     73        $table = $record->getTable(); 
     74        $columns   = $table->getColumns(); 
     75        $component = $table->getComponentName(); 
    7376 
    7477        $errorStack = $record->getErrorStack(); 
     
    7679        // if record is transient all fields will be validated 
    7780        // if record is persistent only the modified fields will be validated 
    78         $data = ($record->exists()) ? $record->getModified() : $record->getData(); 
    79  
    80         $err      = array(); 
    81         foreach ($data as $key => $value) { 
     81        $fields = ($record->exists()) ? $record->getModified() : $record->getData(); 
     82        $err = array(); 
     83        foreach ($fields as $fieldName => $value) { 
    8284            if ($value === self::$_null) { 
    8385                $value = null; 
     
    8587                $value = $value->getIncremented(); 
    8688            } 
    87  
    88             $column = $columns[$record->getTable()->getColumnName($key)]; 
    89  
    90             if ($column['type'] == 'enum') { 
    91                 $value = $record->getTable()->enumIndex($key, $value); 
    92  
    93                 if ($value === false) { 
    94                     $errorStack->add($key, 'enum'); 
    95                     continue; 
    96                 } 
    97             } 
    98  
    99             if ($record->getTable()->getAttribute(Doctrine::ATTR_VALIDATE) & Doctrine::VALIDATE_LENGTHS) { 
    100                 if ( ! $this->validateLength($column, $key, $value)) { 
    101                     $errorStack->add($key, 'length'); 
    102  
    103                     continue; 
    104                 } 
    105             } 
    106  
    107             foreach ($column as $name => $args) { 
    108                 if (empty($name) 
    109                     || $name == 'primary' 
    110                     || $name == 'protected' 
    111                     || $name == 'autoincrement' 
    112                     || $name == 'default' 
    113                     || $name == 'values' 
    114                     || $name == 'sequence' 
    115                     || $name == 'zerofill' 
    116                     || $name == 'owner' 
    117                     || $name == 'scale') { 
    118                     continue; 
    119                 } 
    120  
    121                 if (strtolower($name) === 'notnull' && isset($column['autoincrement'])) { 
    122                     continue; 
    123                 } 
    124  
    125                 if (strtolower($name) == 'length') { 
    126                     if ( ! ($record->getTable()->getAttribute(Doctrine::ATTR_VALIDATE) & Doctrine::VALIDATE_LENGTHS)) { 
    127                         if ( ! $this->validateLength($column, $key, $value)) { 
    128                             $errorStack->add($key, 'length'); 
    129                         } 
     89             
     90            $dataType = $table->getTypeOf($fieldName); 
     91 
     92            // Validate field type, if type validation is enabled 
     93            if ($table->getAttribute(Doctrine::ATTR_VALIDATE) & Doctrine::VALIDATE_TYPES) { 
     94                if ( ! self::isValidType($value, $dataType)) { 
     95                    $errorStack->add($fieldName, 'type'); 
     96                } 
     97                if ($dataType == 'enum') { 
     98                    $enumIndex = $table->enumIndex($fieldName, $value); 
     99                    if ($enumIndex === false) { 
     100                        $errorStack->add($fieldName, 'enum'); 
    130101                    } 
    131                     continue; 
    132                 } 
    133  
    134                 if (strtolower($name) == 'type') { 
    135                     if ( ! ($record->getTable()->getAttribute(Doctrine::ATTR_VALIDATE) & Doctrine::VALIDATE_TYPES)) { 
    136                         if ( ! self::isValidType($value, $column['type'])) { 
    137                             $errorStack->add($key, 'type'); 
    138                         } 
    139                     } 
    140                     continue; 
    141                 } 
    142  
    143                 $validator = self::getValidator($name); 
     102                } 
     103            } 
     104             
     105            // Validate field length, if length validation is enabled 
     106            if ($table->getAttribute(Doctrine::ATTR_VALIDATE) & Doctrine::VALIDATE_LENGTHS) { 
     107                if ( ! $this->validateLength($value, $dataType, $table->getFieldLength($fieldName))) { 
     108                    $errorStack->add($fieldName, 'length'); 
     109                } 
     110            } 
     111 
     112            // Run all custom validators 
     113            foreach ($table->getFieldValidators($fieldName) as $validatorName => $args) { 
     114                if ( ! is_string($validatorName)) { 
     115                    $validatorName = $args; 
     116                    $args = array(); 
     117                } 
     118                $validator = self::getValidator($validatorName); 
    144119                $validator->invoker = $record; 
    145                 $validator->field   = $key; 
    146                 $validator->args    = $args; 
    147  
     120                $validator->field = $fieldName; 
     121                $validator->args = $args; 
    148122                if ( ! $validator->validate($value)) { 
    149                     $errorStack->add($key, $name); 
    150  
    151                     //$err[$key] = 'not valid'; 
    152  
    153                     // errors found quit validation looping for this column 
    154                     //break; 
    155                 } 
    156             } 
    157  
    158             if ($record->getTable()->getAttribute(Doctrine::ATTR_VALIDATE) & Doctrine::VALIDATE_TYPES) { 
    159                 if ( ! self::isValidType($value, $column['type'])) { 
    160                     $errorStack->add($key, 'type'); 
    161                     continue; 
     123                    $errorStack->add($fieldName, $validatorName); 
    162124                } 
    163125            } 
     
    168130     * Validates the length of a field. 
    169131     */ 
    170     private function validateLength($column, $key, $value) 
    171     { 
    172         if ($column['type'] == 'timestamp' || $column['type'] == 'integer' ||  
    173                 $column['type'] == 'enum') { 
     132    private function validateLength($value, $type, $maximumLength) 
     133    { 
     134        if ($type == 'timestamp' || $type == 'integer' || $type == 'enum') { 
    174135            return true; 
    175         } else if ($column['type'] == 'array' || $column['type'] == 'object') { 
     136        } else if ($type == 'array' || $type == 'object') { 
    176137            $length = strlen(serialize($value)); 
    177138        } else { 
    178139            $length = strlen($value); 
    179140        } 
    180  
    181         if ($length > $column['length']) { 
     141        if ($length > $maximumLength) { 
    182142            return false; 
    183143        } 
     
    193153    { 
    194154        return (count($this->stack) > 0); 
    195     } 
    196  
    197     /** 
    198      * phpType 
    199      * converts a doctrine type to native php type 
    200      * 
    201      * @param $portableType     portable doctrine type 
    202      * @return string 
    203      *//* 
    204     public static function phpType($portableType) 
    205     { 
    206         switch ($portableType) { 
    207             case 'enum': 
    208                 return 'integer'; 
    209             case 'blob': 
    210             case 'clob': 
    211             case 'mbstring': 
    212             case 'timestamp': 
    213             case 'date': 
    214             case 'gzip': 
    215                 return 'string'; 
    216                 break; 
    217             default: 
    218                 return $portableType; 
    219         } 
    220     }*/ 
    221     /** 
    222      * returns whether or not the given variable is 
    223      * valid type 
    224      * 
    225      * @param mixed $var 
    226      * @param string $type 
    227      * @return boolean 
    228      */ 
    229      /* 
    230     public static function isValidType($var, $type) 
    231     { 
    232         if ($type == 'boolean') { 
    233             return true; 
    234         } 
    235  
    236         $looseType = self::gettype($var); 
    237         $type      = self::phpType($type); 
    238  
    239         switch ($looseType) { 
    240             case 'float': 
    241             case 'double': 
    242             case 'integer': 
    243                 if ($type == 'string' || $type == 'float') { 
    244                     return true; 
    245                 } 
    246             case 'string': 
    247             case 'array': 
    248             case 'object': 
    249                 return ($type === $looseType); 
    250                 break; 
    251             case 'NULL': 
    252                 return true; 
    253                 break; 
    254         } 
    255     }*/ 
    256      
     155    }     
    257156     
    258157    /** 
     
    302201         } 
    303202     } 
    304      
    305      
    306     /** 
    307      * returns the type of loosely typed variable 
    308      * 
    309      * @param mixed $var 
    310      * @return string 
    311      *//* 
    312     public static function gettype($var) 
    313     { 
    314         $type = gettype($var); 
    315         switch ($type) { 
    316             case 'string': 
    317                 if (preg_match("/^[0-9]+$/",$var)) { 
    318                     return 'integer'; 
    319                 } elseif (is_numeric($var)) { 
    320                     return 'float'; 
    321                 } else { 
    322                     return $type; 
    323                 } 
    324                 break; 
    325             default: 
    326                 return $type; 
    327         } 
    328     }*/ 
    329203} 
  • branches/0.10/tests/ValidatorTestCase.php

    r3884 r4045  
    113113    } 
    114114 
     115    /*public function testDecimalType() 
     116    { 
     117        $validDecimals = array('99.22', 99999.3); 
     118        foreach ($validDecimals as $value) { 
     119            $this->assertTrue(Doctrine_Validator::isValidType($value, 'decimal')); 
     120        } 
     121         
     122        $invalidDecimals = array('decimal', '99999999', '99999999.3', '0.0002', 0.001); 
     123        foreach ($invalidDecimals as $value) { 
     124            $this->assertFalse(Doctrine_Validator::isValidType($value, 'decimal')); 
     125        } 
     126         
     127    }*/ 
    115128 
    116129    public function testValidate2()  
     
    140153    public function testValidate()  
    141154    { 
     155        $this->manager->setAttribute(Doctrine::ATTR_VALIDATE, Doctrine::VALIDATE_ALL); 
    142156        $user = $this->connection->getTable('User')->find(4); 
    143157 
     
    172186 
    173187        $this->assertTrue(in_array('unique', $stack['address'])); 
     188        $this->manager->setAttribute(Doctrine::ATTR_VALIDATE, Doctrine::VALIDATE_NONE); 
    174189    } 
    175190 
     
    383398            $s = $dve->getInvalidRecords(); 
    384399            $this->assertEqual(1, count($dve->getInvalidRecords())); 
     400            $invalids = $dve->getInvalidRecords(); 
     401            //var_dump($invalids[0]->getErrorStack()); 
     402            //echo "<br/><br/>"; 
     403            //var_dump($invalids[1]->getErrorStack()); 
    385404            $stack = $client->ValidatorTest_AddressModel[0]->getErrorStack(); 
    386405