From 8f0323f3179892756c669af1fdff423311d11eac Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Thu, 25 Jul 2013 21:06:44 +0300 Subject: [PATCH] no more error stack --- ipf/orm/exception/validator.php | 27 +---------- ipf/orm/record.php | 38 ++++++--------- ipf/orm/validator.php | 12 ++--- ipf/orm/validator/errorstack.php | 79 -------------------------------- 4 files changed, 20 insertions(+), 136 deletions(-) delete mode 100644 ipf/orm/validator/errorstack.php diff --git a/ipf/orm/exception/validator.php b/ipf/orm/exception/validator.php index 0902839..597409b 100644 --- a/ipf/orm/exception/validator.php +++ b/ipf/orm/exception/validator.php @@ -1,6 +1,6 @@ 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()); - } - } } diff --git a/ipf/orm/record.php b/ipf/orm/record.php index 4e730fb..f7898a4 100644 --- a/ipf/orm/record.php +++ b/ipf/orm/record.php @@ -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(); diff --git a/ipf/orm/validator.php b/ipf/orm/validator.php index c2c96a5..497678e 100644 --- a/ipf/orm/validator.php +++ b/ipf/orm/validator.php @@ -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 index 2ee12d0..0000000 --- a/ipf/orm/validator/errorstack.php +++ /dev/null @@ -1,79 +0,0 @@ -_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 -- 2.49.0