
Originally Posted by
McGruff
Sorry I missed this earlier: I'd go further and split filtering off from Validator. Filtering != validation.
I was a bit reluctant to do that, but I guess you're right. This is what I have now, and I guess arborint has more or less the same solution:
PHP Code:
/**
* Facade for InputValidator and InputFilterChain
*
* @author Ezku (dmnEe0@gmail.com)
* @since Jul 12, 2005
*/
class InputController
{
protected $validator = NULL;
protected $filter = NULL;
public function __construct()
{
$this->validator = new InputValidator;
$this->filter = new InputFilterChain;
}
/**
* Process input
* @param object DataObject
* @param object ILogger
* @return boolean validity
*/
public function process(DataObject $data, ILogger $logger)
{
$data = $this->filter->process($data);
$validity = $this->validator->validate($data, $logger);
return $validity;
}
public function addRule($rule)
{
$this->validator->addRule($rule);
}
public function addFilter($filter)
{
$this->filter->addFilter($filter);
}
}
PHP Code:
/**
* Validates input data
*
* @author Ezku (dmnEe0@gmail.com)
* @since Jul 11, 2005
*/
class InputValidator
{
protected $rules = NULL;
/**
* Add rule to stack
* @param object IInputRule
*/
public function addRule(IInputRule $rule)
{
$this->rules[] = $rule;
}
/**
* Match rules on DataObject, log errors to Logger
* @param object DataObject
* @param object ILogger
* @return boolean is valid
*/
public function validate(DataObject $data, ILogger $logger)
{
$errors = 0;
foreach ($this->rules as $rule)
{
$error = $rule->validate($data);
if (!empty($error))
{
$logger->log($error);
++$errors;
}
}
return ($errors == 0);
}
}
PHP Code:
/**
* Filters input data
*
* @author Ezku (dmnEe0@gmail.com)
* @since Jul 12, 2005
*/
class InputFilterChain implements IInputFilter
{
protected $filters = NULL;
/**
* Add filter to stack
* @param object IInputFilter
*/
public function addFilter(IInputFilter $filter)
{
$this->filters[] = $filter;
}
public function process(DataObject $data)
{
foreach ($this->filters as $filter)
{
$data = $filter->process($data);
}
return $data;
}
}
Someone mentioned saying you could use just a filter instead of a chain, just a rule instead of a validator etc - and I'm seeing that here now. So should IInputRule be changed to match Validator so Validator could implement it, and should InputController implement IInputRule as well? In the FlowController context this would feel natural.
Am I asking things that are already resolved? Sorry for lagging behind, as I see most of you are already at the ApplicationController implementation, full steam.
Bookmarks