HomePHP Page 4 - Decoupling the Validation of Data with Bridge Classes in PHP 5
Putting all the classes to work - 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 you’ll recall from the section that you just read, in this final part of this article I’m going to set up a practical example that shows how to use all the classes that were previously created to validate different types of user-supplied data.
Indeed, this sounds really interesting and hopefully the code samples that you’ll see in a moment will help you grasp more easily how the bridge pattern can be implemented with a few simple PHP 5 classes.
Having said that, here is the corresponding set of try-catch blocks, which demonstrate the correct usage of each of the data validation classes that were built in previous sections, and the “BridgeDataValidator” class as well. Have a look at these examples, please:
// example to validate strings
try{
// instantiate 'BridgeDataValidator' object
$bdatVal=new BridgeDataValidator(array(1,2,3,4),
'The supplied input data must be a string!','string');
// validate string data
$bdatVal->validate();
/*
displays the following:
The supplied input data must be a string!
*/
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
// example to validate numbers
try{
// instantiate 'BridgeDataValidator' object
$bdatVal=new BridgeDataValidator('This is not a number',
'The supplied input data must be a number!','number');
// validate numeric data
$bdatVal->validate();
/*
displays the following:
The supplied input data must be a number!
*/
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
// example to validate alphabetic values
try{
// instantiate 'BridgeDataValidator' object
$bdatVal=new BridgeDataValidator(1234,'The supplied input data
must be an alphabetic value!','alpha');
// validate alphabetic data
$bdatVal->validate();
/*
displays the following:
The supplied input data must be an alphabetic value!
*/
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
// example to validate an email address
try{
// instantiate 'BridgeDataValidator' object
$bdatVal=new BridgeDataValidator('user@fakedomain.com',
'The supplied input data must be a valid email address!','email');
// validate email address
$bdatVal->validate();
/*
displays the following:
The supplied input data must be a valid email address!
*/
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
All right, I think that the group of code samples that were shown above are more than enough for demonstrating the correct implementation of the bridge design pattern in PHP 5. In addition, you should notice how in each case I provided the “BridgeDataValidator” object with an invalid input parameter, a condition that obviously throws the corresponding exception and also makes the script display the respective error message.
As I have said with many other articles that I wrote for the prestigious Developer Shed network, don’t hesitate to introduce your own modifications to all the sample classes that were shown here. This will help you to acquire a more intimate knowledge of how the bridge pattern works. It’s a really educational experience, trust me!
Final thoughts
Unfortunately, we’ve come to the end of this article. However, I hope this tutorial has been instructive for you, specifically with reference to applying the bridge pattern as part of a more complex data validation application. As you saw, the process is really straightforward and also educational.
However, this journey is not over yet. In the last part of the series, I’m going to teach you how to use the mentioned pattern to build an abstraction layer for working with MySQL. See you in the final article!