Home arrow PHP arrow Page 2 - Checking Integers with the Strategy Design Pattern

Review: implementing the Strategy design pattern to validate input data - PHP

If you’re a PHP developer who wants to learn how to implement the Strategy design pattern in a real-world project, then you might want to take a look at this article series. Over the course of its six tutorials, you’ll walk through the development of a modular and flexible application, which will use this powerful pattern to validate different types of data, including integer and float numbers, email addresses and URLs.

TABLE OF CONTENTS:
  1. Checking Integers with the Strategy Design Pattern
  2. Review: implementing the Strategy design pattern to validate input data
  3. Creating an abstract validation class
  4. Validating integer numbers
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
April 20, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I said in the introduction, in the previous part of the series I defined a basic form helper class for accepting different strategy objects from the outside via its “addValidator()” method. Just in case you still haven’t seen the definition of this helper, below I reintroduced its source code, so you can analyze it and understand its driving logic. Here it is: 

(FormHelper.php)

<?php

class FormHelper

{

    protected $_validators = array();

    protected $_errors = array();

   

    // add a validator

    public function addValidator(AbstractValidator $validator)

    {

        $this->_validators[] = $validator;

        return $this;

    }

   

    // get all the validators

    public function getValidators()

    {

        return !empty($this->_validators) ? $this->_validators : null;

    }

     

    // validate inputted data

    public function validate()

    {

        $validators = $this->getValidators();

        if (null !== $validators)

        {

            foreach ($validators as $validator)

            {

                if (!$validator->validate())

                {

                    $this->_errors[] = $validator->getFormattedError();

                }

            } 

        }

        return empty($this->_errors) ? true : false;

    }

   

    // get validation errors as an array

    public function getErrors()

    {

        return $this->_errors;

    }

    

    // get validation errors as a string

    public function getErrorString()

    {

        $errors = $this->getErrors();

        return !empty($errors) ? implode('', $errors) : '';

    }

   

    // clear state of the form helper

    public function clear()

    {

        $this->_validators = array();

        $this->_errors = array();

    }  

}

Even though the form helper class defines only a few simple methods, the implementations of “addValidator()” and “validate()” are clear enough to demonstrate the logic behind the Strategy pattern. As you can see, instead of being one helper tasked with validating the supplied data, this responsibility has been delegated to the injected validator objects. This permits us to win a battle in two fronts: first, Composition is widely favored over Inheritance, and second, each validation process has been encapsulated into an isolated class (remember the sacred OOP principle that says “Encapsulate the concept that varies"?).

Okay, now that you’re familiar with the inner workings of the above helper class, it’s time to start defining the validators. As they will share a lot of common functionality, they will be derived from a generic abstract parent.

In the following segment I’m going to build this abstract validator class, so if you want to learn more details regarding this process, click on the link below and keep reading.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: