HomePHP Page 3 - Decoupling the Validation of Data with Bridge Classes in PHP 5
Defining a set of bridged classes - 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.
As I mentioned in the previous section, once the already familiar “BridgeDataValidator” class has been appropriately defined, the only thing that remains undone consists of creating the group of “bridged” classes. These classes are tasked with validating different types of user-supplied data. As you may guess, this set of classes will complete the implementation of the bridge pattern in PHP 5.
Having said that, let me show you how these new “bridged” classes look. Here they are:
// define 'StringValidator' class
class StringValidator{
const MIN=8;
const MAX=64;
// implement concretely 'validate()' method
public function validate($inputData,$errorMessage){
if(!$inputData||!is_string($inputData)||strlen
($inputData)<self::MIN||strlen($inputData)>self::MAX){
throw new Exception($errorMessage);
}
}
}
// define 'NumberValidator' class
class NumberValidator{
const MIN=-1000000;
const MAX=1000000;
// implement concretely 'validate()' method
public function validate($inputData,$errorMessage){
if(!$inputData||!is_numeric
($inputData)||$inputData<self::MIN||$inputData>self::MAX){
throw new Exception($errorMessage);
}
}
}
// define 'AlphabeticValidator' class
class AlphabeticValidator{
const MIN=8;
const MAX=64;
// implement concretely 'validate()' method
public function validate($inputData,$errorMessage){
if(!$inputData||!!preg_match
("/^[a-zA-Z]+$/",$inputData)||strlen($inputData)
<self::MIN||strlen($inputData)>self::MAX){
throw new Exception($errorMessage);
}
}
}
// define 'EmailValidator' class
class EmailValidator{
// implement concretely 'validate()' method
public function validate($inputData,$errorMessage){
if(!$inputData||!$this->windnsrr($inputData)){
throw new Exception($errorMessage);
}
}
// dnsrr() function for Windows systems
private function windnsrr($hostName,$recType=''){
if($hostName){
if($recType=='')$recType="MX";
exec("nslookup -type=$recType $hostName",$result);
foreach($result as $line){
if(preg_match("/^$hostName/",$line)){
return true;
}
}
return false;
}
return false;
}
}
For this example, I defined only four “bridged” classes, which come in really handy for checking different types of data, such as simple strings, numbers, alphabetic values, and finally email addresses.
In addition to the previous explanation, you should notice how each of the classes offers a concrete implementation for its “validate()” method, in order to fit the validation requirements for a particular type of data. As you can see, this group of classes now reside on a different hierarchy level, something that perfectly follows the definition of the bridge pattern. After all, creating bridge classes with PHP 5 isn’t a difficult task at all, is it?
So far, so good. Now that you hopefully learned how the four “bridged” classes that were listed previously do their business, it would be very instructive to develop a hands-on example that demonstrates how the bridge pattern can be used for performing validation on different user-supplied data.
With reference to the practical example in question, over the course of the following section I’m going to code some simple scripts which will integrate not only the relevant “BridgeDataValidator” class that was shown previously, but all the corresponding independent classes that you learned a few lines before.
In order to see how this final example will be properly developed, please click on the link shown below and keep reading.