Home arrow PHP arrow 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.

TABLE OF CONTENTS:
  1. Decoupling the Validation of Data with Bridge Classes in PHP 5
  2. Building a bridge validator class
  3. Defining a set of bridged classes
  4. Putting all the classes to work
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
January 10, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap

Dev Shed Tutorial Topics: