From 91a4ec8c75e82e0fdb10f922a387a422debdbabd Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Thu, 25 Jul 2013 20:25:18 +0300 Subject: [PATCH] move validation methods to validator class --- ipf/orm/table.php | 59 +------------------------------------------ ipf/orm/validator.php | 54 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 60 deletions(-) diff --git a/ipf/orm/table.php b/ipf/orm/table.php index 9cf7cd6..9ea227d 100644 --- a/ipf/orm/table.php +++ b/ipf/orm/table.php @@ -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])) { diff --git a/ipf/orm/validator.php b/ipf/orm/validator.php index 5ad9d23..c2c96a5 100644 --- a/ipf/orm/validator.php +++ b/ipf/orm/validator.php @@ -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 } } } + -- 2.49.0