HomePHP Page 2 - Decoupling the Validation of Data with Bridge Classes in PHP 5
Building a bridge validator class - PHP
Are you interested in expanding your existing knowledge of different structural design patterns with PHP 5? Then look no further because you’ve come to the right place. Welcome to the second installment of the series “Using Bridge Classes with PHP 5.” This group of three articles walks you through the basics of how to apply the bridge pattern in PHP-based applications, and it also teaches you how to use bridge objects in real world development environments.
The first step involved in this new implementation of the bridge pattern will rely on building a specific class in such a way that it can define the generic structure of an expansible data validator. As you’ll recall from the concepts deployed in the first article, this data checking class will behave like a bridge for others, where the validation of specific data will be concretely implemented.
That said, here is the corresponding signature for this new bridge class, which I creatively called “BridgeDataValidator.” Take a look at its source code, please:
// define
'BridgeDataValidator' class
class BridgeDataValidator{
private $inputData;
private $errorMessage;
private $valCommand;
private $dataValidator;
public function __construct($inputData,$errorMessage,
$valCommand){
if(!$inputData||!$errorMessage||!$valCommand){
throw new Exception
('Invalid validation parameters');
}
$this->inputData=$inputData;
$this->errorMessage=$errorMessage;
if($valCommand=='string'){
$this->dataValidator=new StringValidator();
}
elseif($valCommand=='number'){
$this->dataValidator=new NumberValidator();
}
elseif($valCommand=='alpha'){
$this->dataValidator=new AlphabeticValidator();
}
else{
$this->dataValidator=new EmailValidator();
}
}
public function validate(){
$this->dataValidator->validate
($this->inputData,$this->errorMessage);
}
}
In this concrete case, the above bridge class presents a “validate()” method which defines generically the way that user-supplied data should be validated. With reference to this, you must notice how a $valCommand parameter is passed to the corresponding constructor to determine programmatically which object should be used for checking a particular user entry.
For this class specifically, I defined only four classes that check the validity of specific data, including generic strings, numbers, alphabetic values and email addresses. This feature can be easily modified to aggregate even more classes.
At this stage, it’s clear to see how the previously defined bridge class has been completely decoupled from its implementation, since the validation process is performed by the respective “bridged” objects. Simple and efficient, right?
Now that I have mentioned “bridged” objects, in the following section I’ll show you all the signatures for their corresponding classes. In this way you can understand how the “validate()” method that you learned before is defined concretely by these classes.
To see how the mentioned classes will be coded, click on the link that appears below and keep reading.