Validation
A set of validation classes which can be
chained to rule set.
- supported data types
+ Array (coming soon)
+ Bool (coming soon)
+ Common
+ Date
+ Float (coming soon)
+ Object
+ Resource
+ String
- Vendor
+ ISBN
- Rule
The idea behind.
A value has to pass a chain of validations. Once a value passed all validations
it seems to be valid. These validation chains could be combinated to
complex rule sets which each incoming data has to pass.
A first sample:
$valid = Validation_DataType_String::getInstance();
$valid->setValue("3827370191");
$valid->chain(
"string",
"Validation_DataType_String::validateISBN13"
);
On a higher abstraction level you can design a rule class which takes
an atomic validation set to compute multiple validation on a single value.
class Validation_Rule_ISBN {
private $value;
private $validationObject;
public function __construct($value) {
$this->value = $value;
}
public function firstLevel() {
if(!$this->validationObject instanceof Validation_DataType_String) {
$this->validationObject = Validation_DataType_String::getInstance();
}
$this->validationObject->setValue(array($this->value,10));
$this->validationObject->chain(
"Validation_DataType_String::validateLengthEqual"
);
return $this;
}
public function secondLevel(){
if(!$this->validationObject instanceof Validation_DataType_String) {
$this->validationObject = Validation_DataType_String::getInstance();
}
$this->validationObject->setValue($this->value);
$this->validationObject->chain(
"string",
"Validation_DataType_String::validateISBN10"
);
return $this;
}
}
Thus makes your code more readable. No big twisted if/else source anymore.
Just capsule you validation logic in such rule classes and maintaining
of bigger projects validations will come easy.
$rule = new Validation_Rule_ISBN("3827370191");
if($rule->firstLevel()->secondLevel()->isValid()){
echo "succeed";
} else {
echo "failed";
}
or even shorter:
if($Validation_Rule_ISBN::getInstance("3827370191")->firstLevel()->secondLevel()->isValid()){
echo "succeed";
} else {
echo "failed";
}
These classes are still not complete. If you want some enhancements, then just
added your check to one of the DataType classes and it will magically work
within the set of rules.
|