PhpSnip.com

User Stats

Filter

A simple class that lets you use multiple functions to create custom filters.

Info

 Download  View Source (print view)
 Rating : 4.7  Views : 274

Source Code ( 230 lines )

class filter
{
    var $functions;
    var $separator;

    function filter ()
    {
        $this->functions = func_get_args ();

        is_array ($this->functions)
            or die ('<b>Filter Class Error Message:</b><br>
            <br> You must provide at least one argument for
            the filter class constructor.<br><br> <font
            color="#ffcc00">For example:</font> $my_filter
            = new filter ("trim")<br> For more complete
            examples, please see the documentation included
            with this class.<br>');

        foreach ($this->functions as $function)
          {
            function_exists ($function)
               or die ("<b>Filter Class Error Message:</b>
               <br><br> The function named <b>$function</b>
               is not defined.");

            @ $function ('');
            ! isset ($php_errormsg)
                or die ("<b>Filter Class Error Message:</b>
                <br><br> There was a problem with the
                function named <b>$function</b>, that you
                passed to the constructor.<br>When I tested
                the function to check if it would accept one
                argument, it returned this error:<br>
                $php_errormsg");
          }

        reset ($this->functions);

        $this->set_separator ();
    }

    function apply ($data)
    {
        if ( ! is_array ($data))
          {
            foreach ($this->functions as $function)
                $data = $function ($data);
          }
        return $data;

        foreach ($data as $key => $value)
            foreach ($this->functions as $function)
                $data[$key] = $function ($value);
        return $data;
    }

    function output ($data)
    {
        $data = $this->apply ($data);
        print is_array ($data)
              ? implode ($this->separator, $data) : $data;
        return true;
    }

    function set_separator ($separator = ' ')
    {
        $this->separator = $separator;
        return true;
    }
}

/*
DESCRIPTION
============================================================
Filter:
A simple class that lets you create custom filters from 
multiple functions and apply these filters to scalar 
variables or arrays.
============================================================

CREDITS
============================================================
Author: J. A. Greant ( zak@nucleus.com )
Version 1: May 27, 2000
============================================================

NOTICES
============================================================
If you:
    Modify this code and want to share your changes :)
    Find a bug
    Have other questions or comments

Please write:
    Zak Greant (zak@nucleus.com)
============================================================

USAGE EXAMPLES
============================================================
// Make sure to include the filter class in your source code 
include ('/path/to/filter.inc');

// Create a new filter
$my_filter = new filter ('trim', 'strrev', 'ucwords');

// Set up some sample data
$original = " Hello Jimmy  n";

// Apply the filter to your sample data
$modified = $my_filter->apply ($original);

// Output the filtered data
$my_filter->output ($original);
============================================================

USAGE NOTES
============================================================
The filter class has a default constructor and three
methods.

The filter Constructor
----------------------
The constructor accepts one or more arguments.  Each 
argument should be a string that is the name of a valid 
function.  The functions that you pass to the constructor 
should accept a single scalar as an argument.  it is best to
use functions like trim or addslashes that perform simple 
modifications on string data and return the modified data.
The constructor checks the function names passed to it to 
ensure that the function exists and can accept a single
scalar variable as an argument.  If either of these tests
fails, the script trying to instantiate the class will 
exit.  An error message indicating the nature of the problem
will be displayed.
If no errors are generated, then the function names will be
stored by class for later use by the apply and output 
methods.

Example: $my_filter = new filter ('trim', 'htmlentities');

The apply Method
----------------
Apply accepts a single scalar or array as an argument.  The 
method operates by applying each function stored by the 
class to the argument that was passed to it.  The functions
will be applied to the argument in the order that they were
passed to the constructor.

Example:
    $my_filter = new filter ('trim', 'reverse', 'ucwords');
    $original = " Hello Jimmy  n";
    $modified = $my_filter->apply ($original);
    
    $modified will now contain: Ymmij Olleh

The output Method
-----------------
The output method works very much like apply.  The major
differences are that output prints the modified value 
instead of returning it - and - if an array is passed to 
this method, then it is joined into a string before being
printed.  Each element in the array will be separated by 
a single space.

Examples:
    $my_filter = new filter ('trim', 'reverse', 'ucwords');
    $original = " Hello Jimmy  n";
    $my_filter->output ($original); 
    // The above line will print: Ymmij Olleh

    $array = array ('Hello','Jimmy');
    $my_filter->output ($array);
    // The above line will print: Ymmij Olleh

The set_separator Method
------------------------
This method allows you to set the character (or characters)
that separate array elements when an array is printed out 
with the output method.  The default character is a single
space.

Example:
    $array = array ('Hello','Jimmy');
    $my_filter->output ($array);
    // The above line will print: Ymmij Olleh

    $my_filter->set_separator ('&');
    $my_filter->output ($array);
    // The above line will print: Ymmij&Olleh
============================================================

!!WARNINGS!!
============================================================
The functions that you use to create your new filter MUST:
    Accept only **one** argument.

The functions that you use to create your new filter SHOULD:
    Accept a string as their argument.
    Return a string.

error_tracking should be set to On in your php.ini file
============================================================

COPYRIGHT
============================================================
Copyright (c) 2000 J. A. Greant ( zak@nucleus.com )
All rights reserved.

This library is free software; you can redistribute it
and/or modify it under the terms of the GNU Lesser General
Public License as published by the Free Software Foundation;
either version 2.1 of the License, or (at your option) any
later version.

This library is distributed in the hope that it will be
useful,but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the GNU Lesser General Public License for more
details.

You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to
the:
    Free Software Foundation, Inc.
    59 Temple Place, Suite 330
    Boston, MA
    02111-1307 USA
    http://www.gnu.com/
============================================================
*/

Search

Subscribe

  Rss Feeds

Sponsors

Advertise