]> git.andy128k.dev Git - ipf-legacy-orm.git/commitdiff
no more error stack
authorAndrey Kutejko <andy128k@gmail.com>
Thu, 25 Jul 2013 18:06:44 +0000 (21:06 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Thu, 25 Jul 2013 18:06:44 +0000 (21:06 +0300)
ipf/orm/exception/validator.php
ipf/orm/record.php
ipf/orm/validator.php
ipf/orm/validator/errorstack.php [deleted file]

index 09028396d0496edea24a668620cc8a0df52f0ebf..597409b9d0c3f3aff3aea7deda1e605074b7148f 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 
-class IPF_ORM_Exception_Validator extends IPF_ORM_Exception implements Countable, IteratorAggregate
+class IPF_ORM_Exception_Validator extends IPF_ORM_Exception
 {
     private $invalid = array();
 
@@ -15,39 +15,16 @@ class IPF_ORM_Exception_Validator extends IPF_ORM_Exception implements Countable
         return $this->invalid;
     }
 
-    public function getIterator()
-    {
-        return new ArrayIterator($this->invalid);
-    }
-
-    public function count()
-    {
-        return count($this->invalid);
-    }
-
-    public function __toString()
-    {
-
-        return parent::__toString();
-    }
-
     private function generateMessage()
     {
         $message = "";
         foreach ($this->invalid as $record) {
             $errors = array();
-            foreach ($record->getErrorStack() as $field => $validators)
+            foreach ($record->getErrors() as $field => $validators)
                 $errors[] = 'Field "' . $field . '" failed following validators: ' . implode(', ', $validators) . '.';
             $message .= "Validaton error in class " . get_class($record) . ' (' . implode(' ', $errors) . ') ';
         }
         return $message;
     }
-
-    public function inspect($function)
-    {
-        foreach ($this->invalid as $record) {
-            call_user_func($function, $record->getErrorStack());
-        }
-    }
 }
 
index 4e730fbe3769584d52267f8b511076cb73f1d2f0..f7898a4e5c91419311e91e871d3d8c6cf91463cb 100644 (file)
@@ -14,7 +14,7 @@ abstract class IPF_ORM_Record extends IPF_ORM_Record_Abstract implements Countab
     protected $_values       = array();
     protected $_state;
     protected $_modified     = array();
-    protected $_errorStack;
+    protected $_errors = array();
     protected $_references     = array();
     protected $_pendingDeletes = array();
     protected $_custom         = array();
@@ -98,7 +98,7 @@ abstract class IPF_ORM_Record extends IPF_ORM_Record_Abstract implements Countab
     public function isValid()
     {
         // Clear the stack from any previous errors.
-        $this->getErrorStack()->clear();
+        $this->_errors = array();
 
         // Run validation process
         $validator = new IPF_ORM_Validator();
@@ -110,7 +110,17 @@ abstract class IPF_ORM_Record extends IPF_ORM_Record_Abstract implements Countab
             $this->validateOnUpdate();
         }
 
-        return $this->getErrorStack()->count() == 0 ? true : false;
+        return count($this->_errors) === 0;
+    }
+
+    public function addError($invalidFieldName, $errorCode = 'general')
+    {
+        $this->_errors[$invalidFieldName][] = $errorCode;
+    }
+
+    public function getErrors()
+    {
+        return $this->_errors;
     }
 
     protected function validate(){}
@@ -143,26 +153,6 @@ abstract class IPF_ORM_Record extends IPF_ORM_Record_Abstract implements Countab
 
     public function postInsert($event){}
 
-    public function getErrorStack(){
-        if ( ! $this->_errorStack) {
-            $this->_errorStack = new IPF_ORM_Validator_ErrorStack(get_class($this));
-        }
-
-        return $this->_errorStack;
-    }
-
-    public function errorStack($stack = null)
-    {
-        if ($stack !== null) {
-            if ( ! ($stack instanceof IPF_ORM_Validator_ErrorStack)) {
-               throw new IPF_ORM_Exception('Argument should be an instance of IPF_ORM_Validator_ErrorStack.');
-            }
-            $this->_errorStack = $stack;
-        } else {
-            return $this->getErrorStack();
-        }
-    }
-
     public function assignDefaultValues($overwrite = false)
     {
         if ( ! $this->_table->hasDefaultValues()) {
@@ -249,7 +239,7 @@ abstract class IPF_ORM_Record extends IPF_ORM_Record_Abstract implements Countab
 
         unset($vars['_references']);
         unset($vars['_table']);
-        unset($vars['_errorStack']);
+        unset($vars['_errors']);
         unset($vars['_filter']);
 
         $name = $this->_table->getIdentifier();
index c2c96a58177911fd2d9d92e26d2ff02a24c99463..497678e674b6138483bc890f211a787dae8400c8 100644 (file)
@@ -33,8 +33,6 @@ class IPF_ORM_Validator extends IPF_ORM_Locator_Injectable
 
     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) {
@@ -45,20 +43,20 @@ class IPF_ORM_Validator extends IPF_ORM_Locator_Injectable
 
         // Validate field type
         if (!IPF_ORM_Validator::isValidType($value, $dataType)) {
-            $errorStack->add($fieldName, 'type');
+            $record->addError($fieldName, 'type');
         }
 
         if ($dataType == 'enum') {
             $enumIndex = $table->enumIndex($fieldName, $value);
             if ($enumIndex === false) {
-                $errorStack->add($fieldName, 'enum');
+                $record->addError($fieldName, 'enum');
             }
         }
 
         // Validate field length
         $definition = $table->getDefinitionOf($fieldName);
         if (!$this->validateLength($value, $dataType, $definition['length'])) {
-            $errorStack->add($fieldName, 'length');
+            $record->addError($fieldName, 'length');
         }
 
         // Run all custom validators
@@ -73,11 +71,9 @@ class IPF_ORM_Validator extends IPF_ORM_Locator_Injectable
             $validator->field = $fieldName;
             $validator->args = $args;
             if (!$validator->validate($value)) {
-                $errorStack->add($fieldName, $validator);
+                $record->addError($fieldName, $validatorName);
             }
         }
-
-        return $errorStack;
     }
 
     public function validateLength($value, $type, $maximumLength)
diff --git a/ipf/orm/validator/errorstack.php b/ipf/orm/validator/errorstack.php
deleted file mode 100644 (file)
index 2ee12d0..0000000
+++ /dev/null
@@ -1,79 +0,0 @@
-<?php
-
-class IPF_ORM_Validator_ErrorStack extends IPF_ORM_Access implements Countable, IteratorAggregate
-{
-    protected $_errors = array();
-    protected $_validators = array();
-
-    protected $_className;
-
-    public function __construct($className)
-    {
-        $this->_className = $className;
-    }
-
-    public function add($invalidFieldName, $errorCode = 'general')
-    {
-        // FIXME: In the future the error stack should contain nothing but validator objects
-        if (is_object($errorCode) && strpos(get_class($errorCode), 'IPF_ORM_Validator_') !== false) {
-            $validator = $errorCode;
-            $this->_validators[$invalidFieldName][] = $validator;
-            $className = get_class($errorCode);
-            $errorCode = strtolower(substr($className, strlen('IPF_ORM_Validator_'), strlen($className)));
-        }
-
-        $this->_errors[$invalidFieldName][] = $errorCode;
-    }
-
-    public function remove($fieldName)
-    {
-        if (isset($this->_errors[$fieldName])) {
-            unset($this->_errors[$fieldName]);
-        }
-    }
-
-    public function get($fieldName)
-    {
-        return isset($this->_errors[$fieldName]) ? $this->_errors[$fieldName] : null;
-    }
-
-    public function set($fieldName, $errorCode)
-    {
-        $this->add($fieldName, $errorCode);
-    }
-
-    public function contains($fieldName)
-    {
-        return array_key_exists($fieldName, $this->_errors);
-    }
-
-    public function clear()
-    {
-        $this->_errors = array();
-    }
-
-    public function getIterator()
-    {
-        return new ArrayIterator($this->_errors);
-    }
-
-    public function toArray()
-    {
-        return $this->_errors;
-    }
-
-    public function count()
-    {
-        return count($this->_errors);
-    }
-
-    public function getClassname()
-    {
-        return $this->_className;
-    }
-
-    public function getValidators()
-    {
-        return $this->_validators;
-    }
-}
\ No newline at end of file