PHP
  Home arrow PHP arrow Page 2 - 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 - Building a bridge validator class
    ( Page 2 of 4 )

    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
     

       

    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