<?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();
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());
- }
- }
}
protected $_values = array();
protected $_state;
protected $_modified = array();
- protected $_errorStack;
+ protected $_errors = array();
protected $_references = array();
protected $_pendingDeletes = array();
protected $_custom = array();
public function isValid()
{
// Clear the stack from any previous errors.
- $this->getErrorStack()->clear();
+ $this->_errors = array();
// Run validation process
$validator = new IPF_ORM_Validator();
$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(){}
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()) {
unset($vars['_references']);
unset($vars['_table']);
- unset($vars['_errorStack']);
+ unset($vars['_errors']);
unset($vars['_filter']);
$name = $this->_table->getIdentifier();
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) {
// 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
$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)
+++ /dev/null
-<?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