I'm working on a module that downloads a file saves it then validates if the file contents are valid.
For simplicity lets assume that the file consists of 3 segments: Header, Body & Footer.
Now here is the design I thought of:
class File{
private $header;
private $body;
private $footer;
function __construct($pathToFile){
$this->header= new Header($this->getFileHeader());
$this->body= new Header($this->getFileBody());
$this->footer= new Header($this->getFileFooter());
}
/* getFileHeader(), getFileBody(), getFileFooter() are private functions of the File class
that basically reads the contents of the corresponding segment in the file. */
function isValid(){
if(!$this->header->isValid()) return false;
if(!$this->body->isValid()) return false;
if(!$this->footer->isValid()) return false;
}
}
Header Segment class(I'll only show this one as the Body segment and Footer segment have similar structure)
class Header{
private $content;
private $validators=array();
function __construct($content){
$this->content= $content;
$this->validators= array(new NotEmptyValidator($content), new AllNumbersValidator($content));
//for simplicity the validators will be only 2 and set in the constructor.
}
function isValid(){
foreach($this->validators as $validator){
if(!$validator->isValid()) return false;
}
return true;
}
}
Finally the Validator class:
class NotEmptyValidator{
private $content;
function __construct($content){
$this->content= $content;
}
function isValid(){
return (empty($this->content));
}
}
Questions:
- Overall does this look like a good object oriented design? Can it be improved?
- The
Header
class has an array of validators. I'm not sure if this is the best approach as I borrowed this idea from Zend framework form validators. Is there a better implementation? - Am I over delegating the task of validation?
Update:
I've simplified the design and striped many details to focus mainly on the validation process.
Another point The file I'm trying to validate is an EDI FACT file, so I suppose the file downloaded will have to be parsed first then validated.
Maybe reading the file contents in the File class is a bad idea and I should create a class Parser
that takes the downloaded file and then create the File
object itself then comes the validation.