PHP
  Home arrow PHP arrow Page 3 - Decoupling the Validation of Data with Bridge Classes in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Decoupling the Validation of Data with Bridge Classes in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2007-01-10


    Table of Contents:
  • Decoupling the Validation of Data with Bridge Classes in PHP 5
  • Building a bridge validator class
  • Defining a set of bridged classes
  • Putting all the classes to work

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Decoupling the Validation of Data with Bridge Classes in PHP 5 - Defining a set of bridged classes
    ( Page 3 of 4 )

    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.



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

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek