A few notes from my personal gripes about what InputFilter is and does.
InputFilteris a terrible name as the class mixes sanitation and validation. Rename to something far more appropriate (e.g.InputManager?).- Data should only be processed, not stored - remove
setData()
For example, if both raw and sanitized data are required:
$sanitizedData = $this->inputManager->sanitize($data->toArray());
$validationResult = $this->inputManager->validate($sanitizedData);
if ($validationResult->isValid()) {
// hooray!
} else {
$messages = $validationResult->getMessages();
}Also, if all we need is the result:
$validationResult = $this->inputManager->sanitizeAndValidate($data->toArray());
if ($validationResult->isValid()) {
// hooray!
} else {
$messages = $validationResult->getMessages();
}
Just some quick notes:
Inputbeing aValidator+Filter(both optional)InputFiltershould be renamed toInputCollection, and should simply act as anInputon anyarray|Traversable. That naming also clears up its role.Inputmay be required or not could probably land in theInputCollection, since the input itself doesn't deal with cases where no value at all is passed in.ValidationResult. That can encapsulate a lot of logic that is currently mixed up with inputs/validators/filters and may as well handle translations or such. It should probably be aFilterResultthough, since it doesn't just include validation results, but also original and filtered data. It also allows persistence of validation results.InputstatelessfilterAndValidateis a code smell (just a note: there's anAndin your method name, nuff said). Instead, some way of deciding whether filtering or validation goes first should be defined. That's an implementation detail of the filter.