]> git.andy128k.dev Git - ipf-legacy-orm.git/commitdiff
move validation methods to validator class
authorAndrey Kutejko <andy128k@gmail.com>
Thu, 25 Jul 2013 17:25:18 +0000 (20:25 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Thu, 25 Jul 2013 17:25:18 +0000 (20:25 +0300)
ipf/orm/table.php
ipf/orm/validator.php

index 9cf7cd6cc1909aec84b81503c1171fe769cd475b..9ea227d98eefc2d24c4d732a32ff6c744bc5ebaa 100644 (file)
@@ -474,7 +474,7 @@ class IPF_ORM_Table extends IPF_ORM_Configurable implements Countable
 
         foreach ($options as $k => $option) {
             if (is_numeric($k)) {
-                if ( ! empty($option)) {
+                if (!empty($option)) {
                     $options[$option] = true;
                 }
                 unset($options[$k]);
@@ -869,58 +869,6 @@ class IPF_ORM_Table extends IPF_ORM_Configurable implements Countable
         return $value;
     }
 
-    public function validateField($fieldName, $value, IPF_ORM_Record $record = null)
-    {
-        if ($record instanceof IPF_ORM_Record) {
-            $errorStack = $record->getErrorStack();
-        } else {
-            $record  = $this->create();
-            $errorStack = new IPF_ORM_Validator_ErrorStack($this->getOption('name'));
-        }
-
-        if ($value === self::$_null) {
-            $value = null;
-        } else if ($value instanceof IPF_ORM_Record) {
-            $value = $value->getIncremented();
-        }
-
-        $dataType = $this->getTypeOf($fieldName);
-
-        // Validate field type
-        if ( ! IPF_ORM_Validator::isValidType($value, $dataType)) {
-            $errorStack->add($fieldName, 'type');
-        }
-        if ($dataType == 'enum') {
-            $enumIndex = $this->enumIndex($fieldName, $value);
-            if ($enumIndex === false) {
-                $errorStack->add($fieldName, 'enum');
-            }
-        }
-
-        // Validate field length
-        if ( ! IPF_ORM_Validator::validateLength($value, $dataType, $this->getFieldLength($fieldName))) {
-            $errorStack->add($fieldName, 'length');
-        }
-
-        // Run all custom validators
-        foreach ($this->getFieldValidators($fieldName) as $validatorName => $args) {
-            if ( ! is_string($validatorName)) {
-                $validatorName = $args;
-                $args = array();
-            }
-
-            $validator = IPF_ORM_Validator::getValidator($validatorName);
-            $validator->invoker = $record;
-            $validator->field = $fieldName;
-            $validator->args = $args;
-            if ( ! $validator->validate($value)) {
-                $errorStack->add($fieldName, $validator);
-            }
-        }
-
-        return $errorStack;
-    }
-
     public function getColumnCount()
     {
         return $this->columnCount;
@@ -1154,11 +1102,6 @@ class IPF_ORM_Table extends IPF_ORM_Configurable implements Countable
         return $validators;
     }
 
-    public function getFieldLength($fieldName)
-    {
-        return $this->_columns[$this->getColumnName($fieldName)]['length'];
-    }
-
     public function getBoundQueryPart($queryPart)
     {
         if ( ! isset($this->_options['queryParts'][$queryPart])) {
index 5ad9d23e71d38566e59ef0b7f63dee7ee7234638..c2c96a58177911fd2d9d92e26d2ff02a24c99463 100644 (file)
@@ -27,11 +27,60 @@ class IPF_ORM_Validator extends IPF_ORM_Locator_Injectable
         // if record is persistent only the modified fields will be validated
         $fields = $record->exists() ? $record->getModified():$record->getData();
         foreach ($fields as $fieldName => $value) {
-            $table->validateField($fieldName, $value, $record);
+            $this->validateField($table, $fieldName, $value, $record);
         }
     }
 
-    public static function validateLength($value, $type, $maximumLength)
+    private function validateField(IPF_ORM_Table $table, $fieldName, $value, IPF_ORM_Record $record)
+    {
+        $errorStack = $record->getErrorStack();
+
+        if ($value === self::$_null) {
+            $value = null;
+        } else if ($value instanceof IPF_ORM_Record) {
+            $value = $value->getIncremented();
+        }
+
+        $dataType = $table->getTypeOf($fieldName);
+
+        // Validate field type
+        if (!IPF_ORM_Validator::isValidType($value, $dataType)) {
+            $errorStack->add($fieldName, 'type');
+        }
+
+        if ($dataType == 'enum') {
+            $enumIndex = $table->enumIndex($fieldName, $value);
+            if ($enumIndex === false) {
+                $errorStack->add($fieldName, 'enum');
+            }
+        }
+
+        // Validate field length
+        $definition = $table->getDefinitionOf($fieldName);
+        if (!$this->validateLength($value, $dataType, $definition['length'])) {
+            $errorStack->add($fieldName, 'length');
+        }
+
+        // Run all custom validators
+        foreach ($table->getFieldValidators($fieldName) as $validatorName => $args) {
+            if (!is_string($validatorName)) {
+                $validatorName = $args;
+                $args = array();
+            }
+
+            $validator = IPF_ORM_Validator::getValidator($validatorName);
+            $validator->invoker = $record;
+            $validator->field = $fieldName;
+            $validator->args = $args;
+            if (!$validator->validate($value)) {
+                $errorStack->add($fieldName, $validator);
+            }
+        }
+
+        return $errorStack;
+    }
+
+    public function validateLength($value, $type, $maximumLength)
     {
         if ($type == 'timestamp' || $type == 'integer' || $type == 'enum') {
             return true;
@@ -96,3 +145,4 @@ class IPF_ORM_Validator extends IPF_ORM_Locator_Injectable
          }
      }
 }
+